• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

one to many hibernate

 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I have some basic mapping problem in hibernate.


In one to many mapping one parent can have multiple children.But can same children be repeated for different parent??.

For example, I have one table called Region which is master table, it has child table called country, one region can have multiple countries, like,

Asia --> India, Sri Lanka, Bangladesh..e.t.c
North America ---> USA, Canada.. e.t.c.

My query is can India be repeated for other region say South Asia.I know this is many to many relationship, but if I map relationship between region and country as one to many, and if I add same country for different region, will it create any problem.

I tried an example with save functionality it worked, but I am not sure how it effects in multi-threaded environment.





 
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think its possible only if you dont have a unique constraint on the column in the child table.
Take for example, in your case, the child table is Country...
In that case, i would expect, the countryName in country to be unique, So, you cannot have a country in two regions...because they should be two different records in the Country table, one having regionId corresponding to Asia and other having regionId corresponding to south Asia.
However, if you dont have the countryName as unique, you can have a country belonging to two regions.

Personally, i think countryName in country should be unique, so you cannot have a country in two regions.
But again that depends on your DB design.
Hibernate is just a wrapper over your DB, it modeles your DB...
If your DB design allows something, Hibernate (should) also allow it..

 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply,

I don't have any unique constraints on country name column. My question is if country name is repeating it is virtually becomes many to many relationship, will hibernate have any problem in treating that as one to many relationship.

In our application we have similar kind of mapping, in child table data is repeating as there is no unique constraint, the problem is it works fine as long as it is in local environment, but will give some problems occasionally in live environment.
So I am wondering whether the problem is wrong mapping, or is it OK.

Any replies will be appreciated


 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

My question is if country name is repeating it is virtually becomes many to many relationship



This is not correct...it remains a one to many relationship.
Think of it this way...
In your Country table, you have two records, one for countryName India with region Asia, and the other for countryName India with region South Asia.
These two records are different records in the table and have different Primary Keys....
and each of these two records refer to only one record in Region table, the first record refers to record in Region having Asia and the second corresponds to South Asia.
So, it remains a one - to many relationship.

it works fine as long as it is in local environment, but will give some problems occasionally in live environment.



I am not sure what problem it should give, probably you should elaborate on what problem it gives.

So I am wondering whether the problem is wrong mapping, or is it OK.


I think the hibernate mapping should be fine, if your business requirements and DB allow a countryName to be repeated in the Country table.
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

In our application we have a parent table called ProcessCompliants and child table called BookingStatus, the relationship is one to many, the entries in bookingstatus table can be repeated for different processcompliants.

When we try to save(creation) processcompliants with some bookingstatus values in the set of processcompliants object, with cascade enabled we get exceptions occasionally, and this behaviour is not repeated in local system.

Exceptuions are like.

collection was processed twice by flush

flush during cascade is dangerous



 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure as to why it should give problems only in multithreaded environment...
probably you are trying to set a collection of BookingStatus with two different ProcessComplaints..that's the only thing i can read from the exception that you have posted...
probably you could put a sample of your code so that i can look through it and see what could be the problem..
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Please find the sample code below.


Service Layer Code



in costructBookingstatusSet() method




dao layer code




SessionProvider code



 
Rahul Babbar
Ranch Hand
Posts: 210
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok..
I would prefer if you use a normal HashSet and not LinkedHashSet.....is there some specific reason you are using LinkedHashSet and not HashSet.
This could be the problem..
Apart from this, i dont see much of a problem..
However, i am still at odds to find out why should it give problem only in multithreaded environment...as you said.
Still, try with HashSet, if possible, and see if the problem is resolved.
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Thanks for the suggestion,

I will try with the HashSet. I need to deploy the code and see if it solves the problem or not. but Reaaly not sure, why it occurs at multi-threaded environment.

Thanks for reply.
 
reply
    Bookmark Topic Watch Topic
  • New Topic