These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Parent Child Mapping
PostPosted: Sun Jun 29, 2008 2:34 am 
Newbie

Joined: Thu Jun 19, 2008 10:02 pm
Posts: 4
I am new with Hibernate so sorry if this is stupid.

Consider this:

Code:
class Parent
{
long id;
List<Child> children = new List<Child>();
}
class Child
{
long id;
long parentId;
}


In database, Child table has a column parent_id with foreign key to Parent table.

In the Hibernate documentation, I generally find that examples are given when Child class actually has a reference of Parent class. But in my case, I only have the parentId.

So how do I map the parentId field of Child? Will using many-to-one work?

Thanks

P.S. I know i can just try and see but I dont have any developer tools/software. I am just designing a system.
Code:
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 30, 2008 2:25 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
Quote:
But in my case, I only have the parentId.


Why wouldn't you do it the normal ORM way - that is: representing the database foreign key with a Java object reference? What you are doing is directly map a foreign key to Java. You then have to manually resolve your "parentId". In ORM this is done by the ORM provider, i.e. Hibernate and the foreign key is normally hidden to the Java world - it is represented as an object reference.

_________________
Carlo
-----------------------------------------------------------
please don't forget to rate if this post helped you


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 30, 2008 4:33 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
I am just designing a system

Why should you write code then?
If you need to evaluate your model you should use some tool, I would recommend the hibernate tools (are free and the best); you'll see how your DB schema is mapped to Java entities; as carlolf said you'll not have to bother with ids in your Java code (you can if you want it, but you don't have to), so you bring the model to a more object-oriented way of doing things for your developers.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 30, 2008 7:43 am 
Newbie

Joined: Thu Jun 19, 2008 10:02 pm
Posts: 4
I would have changed the class if it was in my hands.
But it is not. The client has provided the class definitions and they cannot change :(


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 30, 2008 7:44 am 
Newbie

Joined: Thu Jun 19, 2008 10:02 pm
Posts: 4
I would have changed the class if it was in my hands.
But it is not. The client has provided the class definitions and they cannot change :(


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.