Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / JavaBeans / January 2004

Tip: Looking for answers? Try searching our database.

CMP EJB: How to retrive foreign key values from a 1-N relation?

Thread view: 
Eric Ford - 16 Jan 2004 09:52 GMT
Need help in order to retrive the foreign key values from a 1-N relation.
I have 2 tables, User and Friend, where a User can have many Friends.
The User table has id as PK, while the Friend table has id as PK and userId
as FK.
The issiue is that I need to pick out instances of Friend that match a
certain userid, so I somehow need to reference the foreign key column.
Corresponding to the 2 tables, I have a UserBean and a FriendBean, both of
which are CMP's. I'm also using X-Doclet to generate interfaces, etc.

So far I've been able to get ahold of instances of FriendBean, but these do
not include the FK-column. I've also tried using a finder-method in the
UserBean, along these lines:

@ejb.finder
signature = "Integer findByFriendId
( java.lang.Integer userid ) "
query = "SELECT OBJECT(a) FROM User a WHERE a.Friend.id = ?1"
result-type-mapping = "Local"

This didn't work either. Can anyone help me out on this one?
Ashton - 16 Jan 2004 14:14 GMT
> The User table has id as PK, while the Friend table has id as PK and userId
> as FK.

This sounds like you're on the right track. Here's some working
code...maybe you can spot a difference.

The application's about sports; I want to find all the teams in a League
and, for any Team, find the league it belongs to.  It's just a
bi-directional one-to-many relationship.

In LeagueBean.java:
   /**
    *  Teams belonging to the league
    *
    * @ejb:relation name="League-Team"
    *           role-name="League-contains-Team"
    */
   public abstract Collection getTeams();
   public abstract void setTeams(Collection t);

In TeamBean.java:
   /**
    *  League for the team
    *
    * @ejb:relation name="League-Team"
    *           role-name="Team-belongs-to-league"
    *
    * @jboss:relation related-pk-field="id"
    *               fk-column="LeagueId"
    */
   public abstract LeagueEJBLocal getLeague();
   public abstract void setLeague(LeagueEJBLocal l);

The following finder in TeamBean.java then works...
* @ejb:finder signature="java.util.Collection
findByLeague(java.lang.Integer lid)"
*             query="SELECT OBJECT(t) FROM Team t where t.league.id =
?1"
*             result-type-mapping="Remote"


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.