-->
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.  [ 3 posts ] 
Author Message
 Post subject: Mapping question
PostPosted: Sun Dec 11, 2005 8:25 pm 
Newbie

Joined: Sun Dec 11, 2005 8:19 pm
Posts: 2
Location: Chicago
Hey all, i am very new to hibernate so please bear with me. I have a case where lets say I have a person object, very generic. In the person object, there is an id (number), name (map to varchar), age (map to number), blah, blah, then a parent variable that is another Person object. So each Person class has a parent attribute which is a Person object. Lets say in my db, i have 2 tables. 1 called people, 2nd called parents. In the parents table i have only two columns, 1 for parent_id and 2nd for child_id. They both map to the People table using the person_id. How the heck to I map this using hibernate? Please help!!!!

Jason


Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject: for example like this
PostPosted: Mon Dec 12, 2005 4:13 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Code:
<hibernate-mapping package="hib.plays">

  <class table="pc_person" name="pc.Person" lazy="false">
    <id name="id">

      <generator class="assigned"/>
    </id>
     <property name="name"/>
     <bag name="parents" table="pc_parents" >
         <key column="child_id"/>
         <many-to-many class="pc.Person" column="parent_id"/>
     </bag>

  </class>


Code:

public void test( Session s ) throws Exception{
    Person p = new Person();
    p.setId( "parent1" );
    p.setName( "pname1" );
    s.save( p );

    Person p2 = new Person();
    p2.setId( "parent2" );
    p2.setName( "pname2" );
    s.save( p2 );

    Person child = new Person();
    child.setId("child1");
    child.setName("child1");
    child.getParents().add( p );
    child.getParents().add( p2 );
    s.save( child );

  }


The DB content is:

hbtest=> select * from pc_parents ;
child_id | parent_id
----------+-----------
child1 | parent1
child1 | parent2
(2 rows)

hbtest=> select * from pc_person ;
id | name
---------+--------
parent1 | pname1
parent2 | pname2
child1 | child1
(3 rows)

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 12, 2005 7:17 pm 
Newbie

Joined: Sun Dec 11, 2005 8:19 pm
Posts: 2
Location: Chicago
Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.