-->
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: Newbie with question about one-to-many associations
PostPosted: Sat May 26, 2007 4:37 pm 
Newbie

Joined: Tue Dec 05, 2006 7:40 am
Posts: 14
Hi all,

I was trying run examples from hibernate tutorial

http://www.hibernate.org/hib_docs/v3/reference/en/html/associations.html

7.2.3. one to many

my class map configuration:



Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping 3.0 DTD//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="tutorial.Person">
   
      <id name="id" column="personId">
         <generator class="native" />
      </id>
      
      <set name="address">
         <key column="personId" not-null="true"   />
         <one-to-many class="tutorial.Address"  />
      </set>
      
   </class>
   <class name="tutorial.Address">
      <id name="id" column="addressId">
         <generator class="native" />
      </id>
   </class>
</hibernate-mapping>   


Code:
package tutorial;
import java.util.*;
public class Person {
   
   private int id;
   private String name;
   
   private Set address = new HashSet(); 
   
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public Set getAddress() {
      return address;
   }
   public void setAddress(Set address) {
      this.address = address;
   }
   
   
   
   
   
}

Code:
package tutorial;

public class Address {
   
   private int id;
   private String street;
   private Person person;
   
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public String getStreet() {
      return street;
   }
   public void setStreet(String street) {
      this.street = street;
   }
   public Person getPerson() {
      return person;
   }
   public void setPerson(Person person) {
      this.person = person;
   }
   
   
   
   
}


and here my main class:

Code:
package tutorial;

import org.hibernate.*;
import util.*;

public class Associations {

   public static void main(String[] args) {
      
      Associations associations = new Associations();
      //associations.manyToOne();
      //associations.onetoone2();
      associations.onetomany();
   }
   
   public void onetomany() {
                  
      Session session = HibernateUtil.getSessionFactory().getCurrentSession();                        
      
      session.beginTransaction();
      
      Address a     = new Address();
      Person person = new Person();
      
      session.save(person); // first save person !?
      
      person.getAddress().add(a);  //second address
      a.setPerson(person); //set person in address
      
      session.save(a); // third save address
                  
      session.getTransaction().commit();      
   }
   

   
}



Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Address a = new Address();
Person person = new Person();
session.save(person); // first save person !?
person.getAddress().add(a); //second address
a.setPerson(person); //set person in address
session.save(a); // third save address
session.getTransaction().commit();


the sql:

Hibernate: insert into Person (personId) values (null)
Hibernate: insert into Address (personId, addressId) values (?, null)
Hibernate: update Address set personId=? where addressId=?

it is correct ? the code works but I don't understand !

I guess the "correct" should be :

...
Address a = new Address();
Person person = new Person();
person.getAddress().add(a);
session.save(a); // it save person and address! (with two insert statements)
...


why it don't works this way ?

thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Sat May 26, 2007 10:14 pm 
Senior
Senior

Joined: Sat Aug 19, 2006 6:31 pm
Posts: 139
You'll need to specify the cascading behavior for the address one-many relationship for the User.

Here's some info about it.

http://www.hibernate.org/hib_docs/v3/re ... transitive

You need it specified at least for "persist"

_________________
Don't forget to rate the reply if it helps..:)

Budyanto


Top
 Profile  
 
 Post subject: Re: Newbie with question about one-to-many associations
PostPosted: Sat May 26, 2007 10:32 pm 
Beginner
Beginner

Joined: Wed Apr 18, 2007 6:17 pm
Posts: 49
Location: Dominican Republic
Hello ualex, when mapping a collection with hibernate you have to think about two thinks cascade and inverse

I just gonna explain the inverse because it's a little confusing,

the inverse it's when the cascade will be triggered. "When" meaning that
you could do "programatically" write

Code:
Person p=new Person();
Address a=new Address();
//Set the values for p and a here
p.getAddress().add(a);
a.setPerson(p);


Hibernate must know what will be the trigger than activates the cascade
when you say p.getAddres().add(a) or when you say a.setPerson(p). If you set inverse to true then hibernate will ignore the
a.setPerson(p) trigger and it will only gonna persist "a". The trigger to Create the association between "a" and "p" will be p.getAddress().add(a).

I suggest you to add a method to the Person class, something like this

Code:
public void addAddress(Address address)
{
    getAddress().add(address);
    address.setPerson(this);
}


and add to your mapping files, this

Code:
    <set name="address" inverse="true" cascade="all">
        <key column="personId" not-null="true" />
        <one-to-many class="tutorial.Address"  />
    </set>


Regards,

I hope this help you, and it does please rate the post.


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.