File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Hibernate problem: One-To-Many association Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate problem: One-To-Many association" Watch "Hibernate problem: One-To-Many association" New topic
Author

Hibernate problem: One-To-Many association

Eduards Sizovs
Greenhorn

Joined: Apr 25, 2009
Posts: 7
Hi there!

I have a problem with Hibernate:
- Two classes - Company and CompanyContact have One-To-Many relation. When persisting a Company with some Contacts set, Hibernate generates
INSERT statement which does not have FOREIGN KEY set in Contacts. Unfortunately, I cannot spot why and hope for Your assistance,

Mapping for CompanyContact



Mapping for Company



SQL



Thank you,
Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
Your companycontact hbm should have a relationship to Company hbm,
like


and your CompanyContact Pojo should have a
private Company company;
and getters and setters for it.


Rahul Babbar
Eduards Sizovs
Greenhorn

Joined: Apr 25, 2009
Posts: 7
Thanks for response,

In this case, it requires manual setting of CompanyID property, otherwise it inserts null.

Is it possible for Hibernate to determine Foreign Key for associated entities from their parent?
Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
It will set companyId as null in the CompanyContact table if any of the following holds true.
1. One do not have an assocation, but only have a column in the CompanyContact and never sets that property.
2. If one has an association, but is not doing something like "companyContact.setCompany(contact);"
3. If one has both an association as well as setting the company in companyContact as above, but one is not setting the companyId in company and the id generation in Company hbm is defined as "assigned".
Rahul Babbar
Ranch Hand

Joined: Jun 28, 2008
Posts: 210
Eduards Sizovs wrote:
Is it possible for Hibernate to determine Foreign Key for associated entities from their parent?


I dont think so,
since the foreign key exists in the child table and not in the parent table, you need to have the association in the child hbm atleast(which was missing from your original set of hbms)
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Hibernate problem: One-To-Many association
 
Similar Threads
Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
Association references unmapped class exception with hibernate
Hibernte will delete rows from a table, but it won't insert rows
error in running HQL Query
Hibernate Mapping (Many-To-One Association). Problem. This forum is my last hope.