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