• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

using 2 @OnetoMany annoations in one Entity Class

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could someone please suggest me a solution for this situation?
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Joe carco
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using Spring+Hibernate+JPA in order to implement the feature.
 
Ew. You guys are ugly with a capital UG. Here, maybe this tiny ad can help:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic