-->
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.  [ 5 posts ] 
Author Message
 Post subject: [BUG?] Interpretation Criteria Error with a field named ID
PostPosted: Thu Aug 25, 2005 4:39 am 
Beginner
Beginner

Joined: Tue Jul 05, 2005 4:44 am
Posts: 40
Location: Paris, France
Hello,

I have an error when I use a criteria with a field named id (witch is not the primary key)

I think my error is to name a simple field id and the primary key dbId. So, when I use a criteria on the field id, Hibernate interprets this like the primary key and tries to cast the value id as a long Value (see SQL log to the bad hibernate interpretention)

I rename my field id to identification and It works :)
So, i think with criteria hibernate, it 's impossible to use a field named id witch is not a primary key. I don't know if it is a bug or not.

Criteria Uses
Code:
                Criteria criteria = HibernateUtil.getSession().createCriteria(Identification.class);
                criteria.add(Expression.eq("id", id));
                criteria.add(Expression.eq("codingScheme", codingScheme));
                criteria.add(Expression.eq("role", role));
                List list = criteria.list();


Hibernate version: Hibernate 3.05

Classe Identification.java:
Code:
public class Identification implements Serializable {
   /**
    *
    */
   private static final long serialVersionUID = -3195622901824120023L;
   /**
    *
    */
   private String codingScheme;
   /**
    *
    */
   private String id;
   /**
    *
    */
   private String role;
   /**
    * DataBaseId : identification technique de la classe
    */
   private Long dbId;      

   [...]   
}



Mapping documents:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM
                        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="fr.rte.etso.model.persistance">
<class name="Identification" table="IDENTIFICATION">
        <id name="dbId" column="IDENTIFICATION_DB_ID">
                <generator class="sequence">
                        <param name="sequence">IDENTIFICATION_SEQ</param>
                </generator>
        </id>
        <property         name="codingScheme"
                                column="IDENTIFICATION_CODING_SCHEME"       
                                length="3"
                                not-null="true" unique-key="actorKey">
        </property>
        <property         name="id"
                                column="IDENTIFICATION_ID"                               
                                not-null="true"
                                length="35"
                                unique-key="actorKey">
        </property>       
        <property name="role"                       
                                column="IDENTIFICATION_ACTOR_ROLE"                       
                                not-null="true"
                                unique-key="actorKey"
                                length="3">
        </property>
</class>       
</hibernate-mapping>


Full stack trace of any exception that occurs:
Code:
java.lang.ClassCastException
        at org.hibernate.type.LongType.set(LongType.java:40)
        at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:62)
        at org.hibernate.type.NullableType.nullSafeSet(NullableType.java:44)
        at org.hibernate.loader.Loader.bindPositionalParameters(Loader.java:1115)
        at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1177)
        at org.hibernate.loader.Loader.doQuery(Loader.java:390)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:218)
        at org.hibernate.loader.Loader.doList(Loader.java:1593)
        at org.hibernate.loader.Loader.list(Loader.java:1577)
        at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:111)
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1322)
        at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:300)
        at fr.test.dao.hibernate.ActorIdentificationDaoHibernate.retrieveActorId(ActorIdentificationDaoHibernate.java:43)


Name and version of the database you are using: Oracle 9.2

The generated SQL (show_sql=true):
Code:
18:35:29,078 DEBUG SQL: select this_.IDENTIFICATION_DB_ID as IDENTIFI1_0_, this_.IDENTIFICATION_CODING_SCHEME as IDENTIFI2_0_0_, this_.IDENTIFICATION_ID as IDENTIFI3_0_0_, this_.IDENTIFICATION_ACTOR_ROLE as IDENTIFI4_0_0_ from IDENTIFICATION this_ where this_.IDENTIFICATION_DB_ID=? and this_.IDENTIFICATION_CODING_SCHEME=? and this_.IDENTIFICATION_ACTOR_ROLE=?
18:35:29,078 DEBUG AbstractBatcher: preparing statement
18:35:29,078 DEBUG LongType: binding '1dscwsqsE--ddd-Q' to parameter: 1

_________________
Fred


Top
 Profile  
 
 Post subject: id
PostPosted: Thu Aug 25, 2005 11:55 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Yes, H treats id in a special way.

I would not call it a bug, it is a feature becuse if ve look at the hbm file then we see that the file does not have 'property' mapping for ID field, it is mapped as 'id' hence the special treatment.

For example to search for a match agains part of composite key we would use HQL syntax like this:
from O where o.id.somePart = ?

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: Re: id
PostPosted: Mon Dec 05, 2005 1:34 pm 
Beginner
Beginner

Joined: Wed Dec 31, 2003 1:40 pm
Posts: 25
kgignatyev wrote:
Yes, H treats id in a special way.
I would not call it a bug, it is a feature


I think it's an unfortunate design decision made in the past. It is a bug for me, a showstopping bug at that. My object model is derived from a standard, I can'd choose a different name. And there "id" is used for a property which is not the same as the Hibernate id. I hacked Hibernate source code to get around this annoyance, but I need to get this resolved in Hibernate properly.

I have filed a bug report with a suggested fix. See http://opensource2.atlassian.com/projects/hibernate/browse/HB-1434. Unfortunately someone there simply closed it and marked it as "duplicate" without ever linking to the earlier bug whose duplicate it supposedly is.

I think this is one of the few instances where Hibernate is flawed in its design. This feature would have been entirely unnecessary, because all id columns have a property name of their own, so why is making this a reserved word an advantage? :(


Top
 Profile  
 
 Post subject: Re: id
PostPosted: Mon Dec 05, 2005 1:37 pm 
Beginner
Beginner

Joined: Wed Dec 31, 2003 1:40 pm
Posts: 25
gschadow wrote:
kgignatyev wrote:
Yes, H treats id in a special way.
I have filed a bug report with a suggested fix. See WRONG URL.


OOPS I wish I could edit my message. The URL I gave was wrong. I meant http://opensource2.atlassian.com/projects/hibernate/browse/HHH-1127


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 06, 2005 4:33 am 
Beginner
Beginner

Joined: Tue Jul 05, 2005 4:44 am
Posts: 40
Location: Paris, France
Sorry kgignatyev, I have forgot to answer to your message !!

I understand It could not be referenced like bug but I think it must be present in Hibernate Documentation.

Before I have post the message, I have search about my error but I have not find it.

Thx for your help

_________________
Fred


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.