-->
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.  [ 4 posts ] 
Author Message
 Post subject: non-primary-key attribute 'id' mapped to primary-key
PostPosted: Sun Sep 02, 2007 2:53 pm 
Newbie

Joined: Thu Aug 30, 2007 10:41 am
Posts: 5
Hi!

I got an entity which has a non-primary-key attribute named 'id' (I know that's a crap, but can't change this).
The primary key id is named 'guid'.
Whenever I use a query I'm searching for the attribute 'id', e.g.
Code:
from TestUser u where u.id=:id and u.name=:name;

the parameter 'id' is not mapped to 'id' but to 'guid' (see generated SQL below), which results in a org.hibernate.TypeMismatchException, as 'id' is a String and guid is a Long.

Is there something like 'id' defaults to the primary key column no matter what mapping you got in Hibernate?!

I'm stuck with this... Is it a bug?


Hibernate version:
3

Mapping documents:
Code:
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
public class TestUser {

   private long guid;
   
   private String id;
   
   private String name;

   @Id
   public long getGuid() {
      return guid;
   }

   public void setGuid(long guid) {
      this.guid = guid;
   }

   public String getId() {
      return id;
   }

   public void setId(String id) {
      this.id = id;
   }

   public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }
   
}


Code between sessionFactory.openSession() and session.close():

Code:
Object getUser(String id, String name) {
      return em.createQuery("from TestUser u where u.id=:id and u.name=:name").
      setParameter("id", id).
      setParameter("name", name).
      getResultList();

Full stack trace of any exception that occurs:
Code:
java.lang.reflect.UndeclaredThrowableException
   at $Proxy30.getUser(Unknown Source)
   at org.wh.test.model.UserTest.test(UserTest.java:26)
Caused by: com.giag.fo.folib.exception.FOServiceException: An unexpected error occurred while executing the business method.
   at com.giag.fo.core.interceptor.LoggerInterceptor.ejbTrace(LoggerInterceptor.java:108)
   at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:118)
   at org.jboss.ejb3.interceptor.EJB3InterceptorsInterceptor.invoke(EJB3InterceptorsInterceptor.java:63)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:54)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
   at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:193)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:62)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.aspects.security.AuthenticationInterceptor.invoke(AuthenticationInterceptor.java:77)
   at org.jboss.ejb3.security.Ejb3AuthenticationInterceptor.invoke(Ejb3AuthenticationInterceptor.java:105)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:46)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(AsynchronousInterceptor.java:106)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:101)
   at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:214)
   at org.jboss.ejb3.stateless.StatelessContainer.localInvoke(StatelessContainer.java:184)
   at org.jboss.ejb3.stateless.StatelessLocalProxy.invoke(StatelessLocalProxy.java:81)
   ... 23 more
Caused by: java.lang.IllegalArgumentException: org.hibernate.TypeMismatchException: named parameter [id] not of expected type; expected = class java.lang.Long; but was =java.lang.String
   at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:70)
   at org.wh.test.ejb.UserServiceBean.getUser(UserServiceBean.java:26)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:121)
   at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:110)
   at org.jboss.ejb3.interceptor.InvocationContextImpl.proceed(InvocationContextImpl.java:166)
   at com.giag.fo.core.interceptor.LoggerInterceptor.ejbTrace(LoggerInterceptor.java:101)
   ... 51 more
Caused by: org.hibernate.TypeMismatchException: named parameter [id] not of expected type; expected = class java.lang.Long; but was =java.lang.String
   at org.hibernate.loader.hql.QueryLoader.bindNamedParameters(QueryLoader.java:520)
   at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1576)
   at org.hibernate.loader.Loader.doQuery(Loader.java:661)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
   at org.hibernate.loader.Loader.doList(Loader.java:2144)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2028)
   at org.hibernate.loader.Loader.list(Loader.java:2023)
   at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:393)
   at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
   at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
   at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
   at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
   at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:64)

Name and version of the database you are using:
Oracle XE 10g

The generated SQL (show_sql=true):
Code:
select testuser0_.guid as guid0_, testuser0_.name as name0_, testuser0_.id as id0_ from TestUser testuser0_ where testuser0_.guid=? and testuser0_.name=?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 03, 2007 4:03 am 
Regular
Regular

Joined: Thu Oct 19, 2006 12:07 pm
Posts: 75
Yes, "id" is automatically replaced with the name of the primary key property. It is a (documented) "feature".

I don't know if there is a workaround, other than changing your POJO and mapping to use a different name, like :

<property name="myId" column="ID" />

(I'm guess the database column name is "ID")

OK, it is documented here : 14.5. Refering to identifier property

Hmm, is says this translation works only if there is no non-identifier property named "id".
Strange.

Maybe you discovered a bug.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 03, 2007 7:14 am 
Newbie

Joined: Thu Aug 30, 2007 10:41 am
Posts: 5
Thanks a lot!
Due to the Hibernate doc that is definetely a bug, so I created a JIRA issue for it

http://opensource.atlassian.com/project ... e/HHH-2823


Top
 Profile  
 
 Post subject: Re: non-primary-key attribute 'id' mapped to primary-key
PostPosted: Thu Jan 13, 2011 1:18 pm 
Newbie

Joined: Wed Jan 05, 2011 1:12 pm
Posts: 3
Hi,

I want to generate a guid for a non primary key field. Is it possible? i am using reverse engineering from Hibernate tools. Inorder to do it do i need to modify my mapping file and entity classes.

My Entity class:

public String getGuid() {
return this.guid;
}

public void setGuid(String guid) {
this.guid = guid;
}

My mapping:

<property generated="never" lazy="false" name="guid" type="string">
<column length="36" name="Guid" not-null="true"/>
</property>



Could I use a generator class here to generate it automatically even if it is not a primary key column?


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