-->
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: one-to-one mapping question
PostPosted: Thu Jan 06, 2005 8:35 pm 
Newbie

Joined: Wed Jan 05, 2005 4:59 pm
Posts: 15
Hibernate version:2.1.7

Name and version of the database you are using:oracle 10g

Code snippet from chapter 6 page 222(Hibernate in Action)

Code:
   Address addess = new Address();
   address.setStreet("666 Toorak Road");
   address.setCity("Toorak");
   address.setZipCode("3000");

  Transaction tx = session.beginTransaction();
  User user = (User)session.get(User.class, userId);
  address.setUser(user);
  user.setBillingAddress(address);
  tx.commit();


For one-to-one mapping do I have to have a user saved to the database first before I do any address association?

or can I create the user-address association in memory first and then save the user and hibernate will take care of the foriegn key references for me?

For example:

If I do the following does hibernate automatically do the insert the address first and then the user?

Code:
   User user = new User();
   user.setName("Blah");
   Address addess = new Address();
   address.setStreet("666 Toorak Road");
   address.setCity("Toorak");
   address.setZipCode("3000");
   user.setAddress(address);

   Transaction tx = session.beginTransaction();
   //Does the following line of code takes care of address save?
   session.save(user);
   tx.commit();


or the above is not done by hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 07, 2005 6:23 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Of course you can do this, just set up your cascading correctly.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 07, 2005 2:55 pm 
Newbie

Joined: Wed Jan 05, 2005 4:59 pm
Posts: 15
Hibernate rocks!!

I tried after carefully following the chapter, it worked!

Thanks,
Rumpa


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 07, 2005 3:10 pm 
Newbie

Joined: Wed Jan 05, 2005 4:59 pm
Posts: 15
For somebody out there facing similar problem:
mapping files

User.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
       "-//Hibernate/Hibernate Mapping DTD//EN"
       "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="com.exp.model">
   <class name="User" table="TEST_USER">
      <id name="userId" column="USERID">
         <generator class="increment"/>
      </id>
      <property name="fullName" column="FULLNAME" type="string" />
      <property name="userName" column="USERNAME" type="string" />
      <property name="password" column="PASSWORD" type="string" />
      <many-to-one name="billingAddress"
                   class="Address"
                   column="BILLING_ADDRESS_ID"
                   cascade="all"
                   unique="true"/>
   </class>
</hibernate-mapping>


Address.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
       "-//Hibernate/Hibernate Mapping DTD//EN"
       "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="com.exp.model">
   <class name="Address" table="TEST_ADDRESS">
      <id name="addressId" column="ADDRESSID">
         <generator class="increment"/>
      </id>
      <property name="street" column="STREET" type="string" />
      <property name="zipCode" column="ZIPCODE" type="string" />
      <property name="city" column="CITY" type="string" />
   </class>
</hibernate-mapping>


Code to test whether it added the address first then the user
Code:
public class UserProcessor {

    public static void main(String[] args) throws Exception {
        addNewUser();
    }

    private static void addNewUser() throws Exception {
        User user = new User();
        user.setFullName("Tina Roy");
        user.setPassword("password");
        user.setUserName("troy");

        Address address = new Address();
        address.setCity("Solana Beach");
        address.setStreet("180 Rios Avenue");
        address.setZipCode("123456");
        user.setBillingAddress(address);

        Session session = HibernateUtil.getSession();
        Transaction tx = session.beginTransaction();
        session.save(user);
        tx.commit();
        session.close();
    }
}


generated sql in the log:

Hibernate: insert into TEST_ADDRESS (STREET, ZIPCODE, CITY, ADDRESSID) values (?, ?, ?, ?)
Hibernate: insert into TEST_USER (FULLNAME, USERNAME, PASSWORD, BILLING_ADDRESS_ID, USERID) values (?, ?, ?, ?, ?)


Top
 Profile  
 
 Post subject: relationship creation Changed in Hibernate 3?
PostPosted: Sun Jan 09, 2005 7:37 am 
Newbie

Joined: Tue Sep 14, 2004 11:18 am
Posts: 10
I tried the example above and it did not work for me (using Hibernate 3 Beta). I got an "constraint violation".
It does work, however, if I first save the address and only afterwards the user.


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.