aspose file tools*
The moose likes Object Relational Mapping and the fly likes using 2 @OnetoMany annoations in one Entity Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "using 2 @OnetoMany annoations in one Entity Class" Watch "using 2 @OnetoMany annoations in one Entity Class" New topic
Author

using 2 @OnetoMany annoations in one Entity Class

Seetharaman Venkat
Greenhorn

Joined: Nov 07, 2010
Posts: 16
I have the following issue while implementing 2 onetomany association while loading an entity



Initially I had an issue of using "Collection" (for both variables) for @OnetoMany associations, hibernate threw an stack trace, then i decided to use "Set" for one, and "Collection" for another and solved the issue.

Now here is the scenario, while retriving the actuall values of: C has a collection of size 2, and B has a set of size 3 (cross verified it from the Database). But when I try to print the values of A, the 2 values contained in C are printed out properly, but each value in B is printed out 2 times, resulting in 6 outputs, but the actual should have been 3.

So if the actual values contained by C=x, and B=y, the number of values B prints after an iteration is x*y (i.e. y values are printed x times).

have any of you faced this issue before, or is this a bug? Is there a diffenet way to solve this issue?

Let me know!!!
Seetharaman Venkat
Greenhorn

Joined: Nov 07, 2010
Posts: 16
Could someone please suggest me a solution for this situation?
Joe carco
Ranch Hand

Joined: Apr 14, 2009
Posts: 82
Initially I had an issue of using "Collection" (for both variables) for @OnetoMany associations, hibernate threw an stack trace, then i decided to use "Set" for one, and "Collection" for another and solved the issue.


Thats correct, as far as I know Hibernate wont let you eagerly fetch two collections. You could try lazy fetching the both collections in one entity. That should work.

Now here is the scenario, while retriving the actuall values of: C has a collection of size 2, and B has a set of size 3 (cross verified it from the Database). But when I try to print the values of A, the 2 values contained in C are printed out properly, but each value in B is printed out 2 times, resulting in 6 outputs, but the actual should have been 3.


Are the relationsships between A and B and A and C unidirectional or do you have a corresponding ManyToOne on the B and C side?

I have never come across bevhaviour like that. Maybe if you could post the mapping code of B and C?
Seetharaman Venkat
Greenhorn

Joined: Nov 07, 2010
Posts: 16
Joe carco
Ranch Hand

Joined: Apr 14, 2009
Posts: 82
Please correct me if I'm wrong but your entities and mappings look a lot like you use some kind of tool to reverse engineer a Database.
What tool did you use?
What persistence framework did you use? Hibernate, EclipseLink .... ?
Reverse engineered DB tables often produce horrible code and mappings. Sometimes they dont even work.
OneToMany Unidirectional associations arent very common, they dont alway map very well to database tables and usually require join tables when forward engineering.

When starting from scratch I prefer to write an object-orientated business model, then turn them into EJB Entities and then forward engineer my database. Once everything is up and running I fine tune the my database tables, setting index columns etc.
Seetharaman Venkat
Greenhorn

Joined: Nov 07, 2010
Posts: 16
I am using Spring+Hibernate+JPA in order to implement the feature.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using 2 @OnetoMany annoations in one Entity Class