Dear all,
I get an ObjectNotFoundException when loading a subclass with a Long as primary key.
I have the following class and subclasses
Code:
public class Project {
Long id;
String name;
String client;
// getter and setters
}
public class ProjectHR extends Project {
String responsible;
// getter and setters
}
public class ProjectIT extends Project {
String tech;
// getter and setters
}
I need to load one of the subclasses, not the parent.
So, session.load(ProjectHR.class, new Long(2)) or session.load(ProjectIT.class, new Long(2)),
but not session.load(Project.class, new Long(2)).
The strange thing is that it works fine when I declare the PK as String and use uuid.hex as my id generator.
Code:
// when using uuid.hex
session.load( Project.class, "ff80808208436787010843678bb80001" ); // works fine
session.load( ProjectHR.class, "ff80808208436787010843678bb80001" ) ; // works fine
// when using java.lang.Long
session.load( Project.class, new Long(2) ); // works fine
session.load( ProjectHR.class, new Long(2) ) ; // throw ObjectNotFoundException
It also works fine if I execute a general query first:
Code:
Query query = session.query(" FROM ProjectHR ");
query.list();
session.load(ProjectHR.class, new Long(2));
// this will work
// It seems that the class is loaded on the cache if a query it first.
Is that a normal behaviour?
Why it works for String and not for Long?
Is there a work around for that problem?
Hibernate version: 2.1.8
Mapping documents: Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="be.cavalcanti.Project" table="project" dynamic-update="false"
dynamic-insert="false" select-before-update="false">
<id name="id" column="id" type="java.lang.Long" unsaved-value="null">
<generator class="native"/>
</id>
<!--<id name="id" column="id" type="java.lang.String" unsaved-value="null">
<generator class="uuid.hex"/>
</id>-->
<property name="client" type="java.lang.String" update="true" insert="true"
access="property" column="client"/>
<property name="name" type="java.lang.String" update="true" insert="true" access="property"
column="name"/>
<joined-subclass name="be.cavalcanti.ProjectIT" table="project_it"
dynamic-update="false" dynamic-insert="false">
<key column="parentid"/>
<property name="tech" type="java.lang.String" />
</joined-subclass>
<joined-subclass name="be.cavalcanti.ProjectHR" table="project_hr"
dynamic-update="false" dynamic-insert="false">
<key column="parentid"/>
<property name="responsible" type="java.lang.String" />
</joined-subclass>
</class>
</hibernate-mapping>
Full stack trace of any exception that occurs:
Exception in thread "main" net.sf.hibernate.ObjectNotFoundException: No row with the given identifier exists: 2, of class: be.cavalcanti.ProjectHR
at net.sf.hibernate.ObjectNotFoundException.throwIfNull(ObjectNotFoundException.java:24)
at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1931)
at be.cavalcanti.App.main(App.java:41)
Java Result: 1
Name and version of the database you are using:
MySQL 4.1