-->
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: bidirectional one-to-many fetch problem
PostPosted: Mon Jan 24, 2011 11:28 am 
Newbie

Joined: Mon Jan 24, 2011 11:11 am
Posts: 2
Hi, I am trying to create a bidirectional one-to-many relationship between Person and Address tables.
One Address can be shared among multiple persons.

Table Structure:
Person

Code:
+-----------+------------+------+-----+---------+-------+
| Field     | Type       | Null | Key | Default | Extra |
+-----------+------------+------+-----+---------+-------+
| personId  | bigint(20) | NO   | PRI | NULL    |       |
| addressId | bigint(20) | NO   |     | NULL    |       |
+-----------+------------+------+-----+---------+-------+


Code:
+---------+--------------+------+-----+---------+-------+
| Field   | Type         | Null | Key | Default | Extra |
+---------+--------------+------+-----+---------+-------+
| id      | bigint(20)   | NO   | PRI | NULL    |       |
| address | varchar(200) | YES  |     | NULL    |       |
+---------+--------------+------+-----+---------+-------+


I am trying to fetch the person object by calling a get(Person.class, 1) on session object.

Code:
      Session s = sessionFactory.openSession();
      s.beginTransaction();
      Person person = (Person)s.get(Person.class,1);
      System.out.println(person);




I get the following exception. Is it that hibernate does not allow to fetch information in this manner ?

Code:
Hibernate:
    select
        person0_.personId as personId1_0_,
        person0_.addressId as addressId1_0_
    from
        Person person0_
    where
        person0_.personId=?
Hibernate:
    select
        address0_.id as id2_0_,
        address0_.address as address2_0_
    from
        Address address0_
    where
        address0_.id=?
Hibernate:
    select
        persons0_.addressId as addressId2_1_,
        persons0_.personId as personId1_,
        persons0_.personId as personId1_0_,
        persons0_.addressId as addressId1_0_
    from
        Person persons0_
    where
        persons0_.addressId=?
Exception in thread "main" java.lang.StackOverflowError
   at java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:44)
   at java.lang.StringBuilder.<init>(StringBuilder.java:92)
   at com.test.app.Address.toString(Address.java:16)
   at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:197)
   at com.test.app.Address_$$_javassist_1.toString(Address_$$_javassist_1.java)
   at java.lang.String.valueOf(String.java:2826)
   at java.lang.StringBuilder.append(StringBuilder.java:115)
   at com.test.app.Person.toString(Person.java:35)
   at java.lang.String.valueOf(String.java:2826)
   at java.lang.StringBuilder.append(StringBuilder.java:115)
   at java.util.AbstractCollection.toString(AbstractCollection.java:422)
   at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:333)
   at java.lang.String.valueOf(String.java:2826)
   at java.lang.StringBuilder.append(StringBuilder.java:115)
   at com.test.app.Address.toString(Address.java:17)
   at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:197)
   at com.test.app.Address_$$_javassist_1.toString(Address_$$_javassist_1.java)
   at java.lang.String.valueOf(String.java:2826)
   at java.lang.StringBuilder.append(StringBuilder.java:115)
   at com.test.app.Person.toString(Person.java:35)
   at java.lang.String.valueOf(String.java:2826)
   at java.lang.StringBuilder.append(StringBuilder.java:115)
   at java.util.AbstractCollection.toString(AbstractCollection.java:422)




Below are the mapping files.
Code:
<hibernate-mapping package="com.test.app">
   <class name="Person" table="Person" lazy="false">
      <id name="personId" column="personId"/>
      <!-- <property name="addressId" column="addressId"/> -->
      <many-to-one name="address"
                   column="addressId" not-null="true"/>
   </class>
   <class name="Address" table="Address">
      <id name="id" column="id"/>
      <property name="addressLine" column="address"/>
      <set name="persons" inverse="true" cascade="all">
         <key column="addressId"/>
         <one-to-many class="Person"/>
      </set>
   </class>
</hibernate-mapping>


Person & Address class
Code:
public class Person {
   private Integer personId;
   private Address address;

   public Integer getPersonId() {
      return personId;
   }

   public void setPersonId(Integer personId) {
      this.personId = personId;
   }

   public Address getAddress() {
      return address;
   }

   public void setAddress(Address address) {
      this.address = address;
   }

   @Override
   public String toString() {
      return "Person [personId=" + personId
            + ", address=" + address + "]";
   }

}

public class Address {
   private Integer id;
   private String addressLine;
   Set<Person> persons;

   public Integer getId() {
      return id;
   }

   @Override
   public String toString() {
      return "Address [id=" + id + ", addressLine=" + addressLine
            + ", persons=" + persons + "]";
   }

   public void setId(Integer id) {
      this.id = id;
   }

   public String getAddressLine() {
      return addressLine;
   }

   public void setAddressLine(String addressLine) {
      this.addressLine = addressLine;
   }

   public Set<Person> getPersons() {
      return persons;
   }

   public void setPersons(Set<Person> persons) {
      this.persons = persons;
   }

}


Regards

Anish


Top
 Profile  
 
 Post subject: Re: bidirectional one-to-many fetch problem
PostPosted: Mon Jan 24, 2011 3:40 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Your toString() implementations is most likely creating an infinite loop since the Person.toString() is printing the address and the Address.toString() is printing all persons. I would be very careful with navigating associations in toString(), equals() and hashCode() methods since that is likely to cause unwanted side-effects.


Top
 Profile  
 
 Post subject: Re: bidirectional one-to-many fetch problem
PostPosted: Mon Jan 24, 2011 11:33 pm 
Newbie

Joined: Mon Jan 24, 2011 11:11 am
Posts: 2
Thank you.


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.