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: Can't load joined-subclass when identifier is Long
PostPosted: Mon Dec 19, 2005 10:45 am 
Newbie

Joined: Mon Dec 19, 2005 9:17 am
Posts: 2
Location: Leuven, Belgium
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


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 19, 2005 12:43 pm 
Beginner
Beginner

Joined: Thu Sep 01, 2005 7:43 am
Posts: 31
Location: León (Spain)
What values are stored in the DB when the session.load fails? The values of id column in "project" table are the same as parentid column in "project_hr" table?

Bye.

_________________
Please rate...

Expert on Hibernate errors... I've had them all... :P


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 19, 2005 12:52 pm 
Newbie

Joined: Mon Dec 19, 2005 9:17 am
Posts: 2
Location: Leuven, Belgium
Yes, they are the same. I mean "project_hr" has one row with id 2 and "project" has three rows, with IDs from 1 to 3.

By calling session.load(ProjectHR.class, new Long(2));
I expected to get a ProjectHR that correspond to the join from "project_hr" and "project" where ID = 2.

What I don't understand is why it works when I replace the PK by String and use the uuid.hex generator.

Thanks in advance


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.