Hi.
My Hibernateversion 3.2.6.ga
I have a problem with Objectidenty, have read a article at OnJava.com "Don't Let Hibernate Steal Your Identity" and some Threads at this Forum.
Now i have refactored my Application like the suggestion at OnJava. The result is, i can't load my traversal Hierarchie of region and get a
If i debug my JUnit, i see, the parentId of my loaded region is not set with the databasevalue.
Exception as following:
Code:
DEBUG: de.myApp.bean.Region - getParentList()de.myApp.bean.Region@cfb11f[name=Deutschland,leftValue=<null>,rightValue=<null>]
ERROR: org.hibernate.LazyInitializationException - could not initialize proxy - no Session
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:57)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:111)
at org.hibernate.proxy.pojo.cglib.CGLIBLazyInitializer.invoke(CGLIBLazyInitializer.java:150)
at de.myApp.bean.Region$$EnhancerByCGLIB$$9e36b7cd.hashCode(<generated>)
at de.myApp.bean.Region.internalGetParentList(Region.java:82)
at de.myApp.bean.Region.getParentList(Region.java:65)
at de.myApp.test.service.RegionServiceDaoTest.testLoadRegionById(RegionServiceDaoTest.java:30)
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:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76)
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:120)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
my Hibernateconfig:
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse - Hibernate Tools
-->
<hibernate-mapping>
<class name="de.myApp.bean.Region" table="region">
<id name="oid" type="java.lang.String">
<column name="oid" />
<generator class="assigned" />
</id>
<version name="version" column="VERSION"
unsaved-value="null" />
<property name="name" type="java.lang.String">
<column name="NAME" length="155" not-null="true" />
</property>
<property name="leftValue" type="java.lang.Integer">
<column name="leftvalue" />
</property>
<property name="rightValue" type="java.lang.Integer">
<column name="rightvalue" />
</property>
<many-to-one name="parent" class="de.myApp.bean.Region" >
<column name="parent_id" />
</many-to-one>
..
..
my AbstractPersistenzobject:
Code:
public abstract class AbstractPersistentObject implements PersistentObject {
private static final Log LOGGER = LogFactory.getLog(AbstractPersistentObject.class);
private String oid = IdGenerator.createId();;
private Integer version;
public final String getOid() {
return oid;
}
public final void setOid(String oid) {
this.oid = oid;
}
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || !(o instanceof PersistentObject)) {
return false;
}
PersistentObject other = (PersistentObject) o;
// if the id is missing, return false
if (oid == null)
return false;
// equivalence by id
return oid.equals(other.getOid());
}
public int hashCode() {
if (oid != null) {
return oid.hashCode();
} else {
return super.hashCode();
}
}
public String toString() {
return this.getClass().getName() + "[id=" + oid + "]";
}
public Integer getVersion() {
return version;
}
public void setVersion(Integer version) {
this.version = version;
}
PersitenzObject
Code:
public interface PersistentObject {
public String getOid();
public void setOid(String oid);
public Integer getVersion();
public void setVersion(Integer version);
}
my Bean region
Code:
public class Region extends AbstractPersistentObject implements java.io.Serializable, PreorderModifiedTreeTraversal {
public Region(){
super();
}
public static final String ROOTREGION_ID = "1";
static final long serialVersionUID = 4086461538561865983L;
public final static Log LOGGER = LogFactory.getLog(Region.class);
private String name;
private Long anzahlitems;
private Region parent;
..
..
..
some more attributes and the getter/setter.
dao:
Code:
public Region findByName(String regionName) {
Criteria myCriteria=null;
if (StringUtils.isNotBlank(regionName)) {
myCriteria = getSession().createCriteria(Region.class);
myCriteria.add(Restrictions.sqlRestriction("name = upper(?)", regionName, Hibernate.STRING));
}
List retval = myCriteria.list();
if (retval.size()>0) {
return (Region)retval.get(0);
} else {
return findRootRegion();
}
}
my Junit:
Code:
public void testLoadRegionById() throws Exception{
regionDAO = (RegionDAO)applicationContext.getBean("regionDAO");
Region region = regionService.getByName("Deutschland");
Collection parents = region.getParentList();
int i = 0;
}
I believe my problem is the implementation of equals and hashcode within my abstract class. But i cannot see the mistake.
If i load the first region with name "Deutschland" in the debugmode i see, the parent_id of the parent (Europa with oid=1) isn't set and has the default value of the IdGenerator.
I can not understand why hibernate sets not the parent_id.
Thanks for your help.
alex