-->
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: One-to-One Relation mapping with foreign key association
PostPosted: Thu Apr 22, 2004 5:59 am 
Newbie

Joined: Mon Jan 12, 2004 7:05 am
Posts: 16
i have defined a one-to-one relationship between person and address with foreign key association , but in the database there is no address_id inserted with person, no person_id inserted with address either.

Code:
                           Configuration config = new Configuration().configure();
      SessionFactory sf = config.buildSessionFactory();
      Session se = sf.openSession();

      Address adr =
         new Address(
            "1To1AddressTest",
            "1",
            new Integer(60599),
            "Frankfurt",
            "Germany");
      se.save(adr);

      Person p = new Person();
      Name pn = new Name("1To1PersonTest", "test");
      p.setName(pn);
      se.save(p);
      
      adr.setPerson(p);
      se.update(adr);
   
      se.connection().commit();
      se.close();
      se.flush();




Code:
the mapping is following

<class name="test.Person" table="PERSON">
      <id name="id" type="long">
         <column name="PERSON_ID" not-null="true" />
         <generator class="native" />
      </id>
      <component name="name" class="test.Name">
         <property name="forename">
            <column name="forename" not-null="false" />
         </property>
         <property name="surname">
            <column name="surname" not-null="false" />
         </property>
      </component>
      <many-to-one name="cat" class="test.Cat" column="CAT_ID" unique="true" cascade="save-update" />
      <list name="sizes" table="PERSON_SIZES">
         <key column="PERSON_ID" />
         <index column="I" />
         <element column="sizes" type="string" />
      </list>
      <list name="phones" table="PERSON_PHONES">
         <key column="PERSON_ID" />
         <index column="I" />
         <composite-element name="phones" class="test.Phone">
            <property name="prefix">
               <column name="prefix" not-null="false" />
            </property>
            <property name="number">
               <column name="number" not-null="false" />
            </property>
         </composite-element>
      </list>
      <many-to-one name="address" class="test.Address" column="ADDRESS_ID" unique="true" cascade="save-update"/>
      </class>


<class name="test.Address" table="ADDRESS">
         <id name="id" type="long">
            <column name="ADDRESS_ID" not-null="true" />
            <generator class="native" />
         </id>
         <property name="street">
            <column name="street" not-null="false" />
         </property>
         <property name="number">
            <column name="number" not-null="false" />
         </property>
         <property name="zip">
            <column name="zip" not-null="false" />
         </property>
         <property name="city">
            <column name="city" not-null="false" />
         </property>
         <property name="country">
            <column name="country" not-null="false" />
         </property>
         <many-to-one name="person" class="test.Person" column="PERSON_ID" unique="true" cascade="save-update"/>
      </class>


Can someone help me? i don't want to use primary key association.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 22, 2004 4:05 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Why not using <one-to-one>

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 23, 2004 5:51 am 
Newbie

Joined: Mon Jan 12, 2004 7:05 am
Posts: 16
I am using aspectJ to insert the hibernate database accessing, and generate the mapping automatic with reflection.
Code:
Address adr = new Address( "1To1Address",
                             "1",
                new Integer(60599),
                "Frankfurt",
                "Germany");

before the initialization the mapping will be generated, after the initialization it will be inserted in the database automatic with the following aspect.
Code:
        try {
   if ( session == null ){ buildNewSession(); }
               
                tx = session.beginTransaction();
   if (action.equalsIgnoreCase("save")) {
       session.save(o);
   } else if (action.equalsIgnoreCase("update")) {
      session.update(o);
                } else if (action.equalsIgnoreCase("delete")) {
      session.delete(o);
   }
                tx.commit();
        } catch (Exception e) {
   if (tx != null)
   tx.rollback();
   e.printStackTrace();
       }

if i use one-to-one primary association , during the initialization i do not know the person id, and i must insert the address in the database. so i think i use the many-to-one relation, and after adr.setPerson(p), save with session.update(adr) the person_id. but the update do not add the person_id to the address table.


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.