Hibernate version:
3.0
Code between sessionFactory.openSession() and session.close():
springframewok
Name and version of the database you are using:
postgresql 8.0
Hi
I am new in spring and hibernate, every thing works fine until I start with one-to-one relation, my unit tests start to failed, i think that I need change something in *.hbm.xml file but I try different options but didn't get satisfied result. Here is my error message during performing unit test:
Code:
Hibernate: insert into members (address_id, contact_id, member_id) values (?, ?, ?)
Hibernate: update addresses set city=?, state=?, zip=?, country=?, phone=?, fax=? where address_id=?
2006-08-11 19:40:26 org.hibernate.LazyInitializationException <init>
SEVERE: could not initialize proxy - the owning Session was closed
org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:53)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:84)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:134)
at org.noip.jorg.pms.manager.AddressImpl$$EnhancerByCGLIB$$30e7c4a8.toString(<generated>)
at java.lang.String.valueOf(String.java:2577)
at java.lang.StringBuffer.append(StringBuffer.java:220)
at junit.framework.Assert.format(Assert.java:289)
at junit.framework.Assert.failNotEquals(Assert.java:282)
at junit.framework.Assert.assertEquals(Assert.java:64)
at junit.framework.Assert.assertEquals(Assert.java:71)
at org.noip.jorg.pms.manager.AddressImplTest.testSave(AddressImplTest.java:77)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
My address.javaCode:
@Configurable("templateAddress")
public class AddressImpl implements Address {
private Integer id;
private String city;
private String state;
private String zip;
private String country;
private String phone;
private String fax;
private static AddressDao dao;
private Member member;
public void save(){
dao.save(this);
}
public void delete(){
dao.delete(this);
}
public AddressDao getDao() {
return dao;
}
public void setDao(AddressDao dao) {
this.dao = dao;
}
//setters and getters
}
My Address.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sf.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.noip.jorg.pms.manager.AddressImpl" table="addresses" lazy="true">
<id name="id" column="address_id" type="java.lang.Integer" unsaved-value="0">
<generator class="native"></generator>
</id>
<property name="city" column="city" type="java.lang.String"></property>
<property name="state" column="state" type="java.lang.String"></property>
<property name="zip" column="zip" type="java.lang.String"></property>
<property name="country" column="country" type="java.lang.String"></property>
<property name="phone" column="phone" type="java.lang.String"></property>
<property name="fax" column="fax" type="java.lang.String"></property>
<one-to-one name="member" class="org.noip.jorg.pms.manager.MemberImpl" property-ref="address"></one-to-one>
<one-to-one name="customer" class="org.noip.jorg.pms.manager.CustomerImpl" property-ref="address"></one-to-one>
</class>
</hibernate-mapping>
My MemberImpl.java
Code:
@Configurable("templateMember")
public class MemberImpl implements Member {
private Integer id;
private Contact contact;
private Address address;
private static MemberDao dao;
private Set<Task> tasks = new TreeSet<Task>();
private Set<Group> groups = new HashSet<Group>();
public void addToGroup(Group group){
if(group == null)
throw new InvalidAttributeValueException("Group can't be null");
if(groups.contains(group))
throw new InvalidAttributeValueException("User already belongs to this group");
this.groups.add(group);
}
public void removeFromGroup(Group group){
if(group == null)
throw new InvalidAttributeValueException("Null group!!");
if(!groups.contains(group))
throw new InvalidAttributeValueException("Member don't belong to this group!!");
this.groups.remove(group);
}
public void save(){
dao.save(this);
}
public void delete(){
dao.delete(this);
}
public MemberDao getDao() {
return dao;
}
public void setDao(MemberDao dao) {
this.dao = dao;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Contact getContact() {
return contact;
}
public void setContact(Contact contact) {
this.contact = contact;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Set<Task> getTasks() {
return tasks;
}
public void setTasks(Set<Task> tasks) {
this.tasks = tasks;
}
public Set<Group> getGroups() {
return groups;
}
public void setGroups(Set<Group> groups) {
this.groups = groups;
}
}
[B} MemberImpl.hbm.xml
[/B]
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sf.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="org.noip.jorg.pms.manager.MemberImpl" table="members">
<id name="id" column="member_id" type="java.lang.Integer" unsaved-value="0">
<generator class="native"></generator>
</id>
<set name="groups" table="members_groups">
<key column="member_id"></key>
<many-to-many column="group_id" class="org.noip.jorg.pms.manager.GroupImpl"></many-to-many>
</set>
<many-to-one name="address" class="org.noip.jorg.pms.manager.AddressImpl" column="address_id" unique="true" cascade="save-update"></many-to-one>
<many-to-one name="contact" class="org.noip.jorg.pms.manager.ContactImpl" column="contact_id" unique="true" cascade="save-update"></many-to-one>
<set name="tasks" table="members_tasks">
<key column="member_id"></key>
<many-to-many column="task_id" class="org.noip.jorg.pms.project.Task"></many-to-many>
</set>
</class>
</hibernate-mapping>
AddressImplTest.java where I get error message
Code:
package org.noip.jorg.pms.manager;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import junit.framework.TestCase;
public class AddressImplTest extends TestCase {
public static ClassPathXmlApplicationContext appContext = null;
private Address address, address1, address2;
private Members members;
private Member member, member1, member2;
protected void setUp() throws Exception {
if (appContext == null)
appContext = new ClassPathXmlApplicationContext("dataAccessContext.xml");
members = new MembersImpl();
members.deleteAll();
member = members.create();
member1 = members.create();
member2 = members.create();
address = new AddressImpl();
address.setCity("Rydułtowy");
address.setCountry("Poland");
address.setFax("032-45678");
address.setPhone("032-4568-767");
address.setState("Sląskie");
address.setZip("44-280");
address.save();
address1 = new AddressImpl();
address1.setCity("Rybnik");
address1.setCountry("Poland");
address1.setFax("032-49678");
address1.setPhone("032-4968-767");
address1.setState("Sląskie");
address1.setZip("44-220");
address1.save();
address2 = new AddressImpl();
address2.setCity("Essen");
address2.setCountry("Deutschland");
address2.setFax("0048-49678");
address2.setPhone("0048-4968-767");
address2.setState("Essen Ost");
address2.setZip("22-333");
address2.save();
member.setAddress(address);
address.setMember(member);
address.save();
member.save();
member1.setAddress(address1);
address1.setMember(member1);
address1.save();
member1.save();
member2.setAddress(address2);
address2.setMember(member2);
address2.save();
member2.save();
}
/*
* Test method for 'org.noip.jorg.pms.manager.AddressImpl.save()'
*/
public void testSave() {
assertEquals(member.getAddress(), members.find(member.getId()).getAddress());
assertEquals(member1.getAddress(), members.find(member1.getId()).getAddress());
assertEquals(member2.getAddress(), members.find(member2.getId()).getAddress());
}
/*
* Test method for 'orgname.noip.jorg.pms.manager.AddressImpl.delete()'
*/
public void testDelete() {
address.delete();
assertEquals(null, members.find(member.getId()).getAddress());
address1.delete();
assertEquals(null, members.find(member1.getId()).getAddress());
address2.delete();
assertEquals(null, members.find(member2.getId()).getAddress());
}
}
this code is a part of project management system for student organization develop by students
http://www.aegee.gliwice.pl/jorg/, i will be thankfully for any response