aspose file tools*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes Quick question on hierarchical Entity Relationships.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "Quick question on hierarchical Entity Relationships...." Watch "Quick question on hierarchical Entity Relationships...." New topic
Author

Quick question on hierarchical Entity Relationships....

Ann Basso
Ranch Hand

Joined: Jul 26, 2007
Posts: 48
I am wondering whether EJB 3.0 supports hierarchical relationship in Entities. For example, you have an Employee table having EmpID and ManagerID columns, where ManagerID contains an employeeid of the manager of that employee.

How can we use 3.0 entities to model such relationship? Any idea?
Andor Nemeth
Greenhorn

Joined: Jul 06, 2007
Posts: 16
Originally posted by Ann Basso:
I am wondering whether EJB 3.0 supports hierarchical relationship in Entities. For example, you have an Employee table having EmpID and ManagerID columns, where ManagerID contains an employeeid of the manager of that employee.

How can we use 3.0 entities to model such relationship? Any idea?


Hi Ann,

i think this code represents the statements above:



I've omitted getter/setters and used public instance variables to focus on the problem.

Correct me if i was wrong!

Greetings,
Andor N�meth


SCJP 5<br />SCBCD 5 (next target)
Ann Basso
Ranch Hand

Joined: Jul 26, 2007
Posts: 48
Yes, that is what I was thinking but I don't know whether that will work. For example, if you want to get all the employees that are managed by an employee, how will you get that?
[ August 09, 2007: Message edited by: Ann Basso ]
Andor Nemeth
Greenhorn

Joined: Jul 06, 2007
Posts: 16
Originally posted by Ann Basso:
Yes, that is what I was thinking but I don't know whether that will work. For example, if you want to get all the employees that are managed by an employee, how will you get that?

[ August 09, 2007: Message edited by: Ann Basso ]


The code example I've gave was for a many-to-one unidirectional relationship.
This way you can't determine directly the employees that are managed by an employee("manager").

For this you'll need obiviously a many-to-one bidirectional relationship:




Then I've created two methods in a session bean to play with this Employee class a bit:



Then I've created a client to locate my remote session bean and to call these two methods above!
After calling these methods this was the output on my JBoss console:

19:50:42,931 INFO [STDOUT] F�n�kJ�zsi's staff:
19:50:42,941 INFO [STDOUT] Munk�sB�la1
19:50:42,941 INFO [STDOUT] Munk�sB�la2


So, this is the soulution! It did work with this Employee class and with these annotations.


Greetings,
Andor N�meth

p.s.
It did take some time to accomplish this, because I run to this bug:
http://forum.java.sun.com/thread.jspa?threadID=5137725&tstart=60

[ August 09, 2007: Message edited by: Andor Nemeth ]
Ann Basso
Ranch Hand

Joined: Jul 26, 2007
Posts: 48
That's amazing. Thanks a lot. I will also try to run in my app server.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Quick question on hierarchical Entity Relationships....
 
Similar Threads
Doubt in Entity Relationship Persistence
JPA ManyToOne Mapping Question
OneToMany Unidirectional strange behavior ?
Hibernate association mapping
EJB3 Entity Relationship