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"