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=?