-->
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: Enumerated(String) Primary Key fetched as byte[]
PostPosted: Mon Nov 16, 2015 11:44 am 
Newbie

Joined: Mon Nov 16, 2015 11:30 am
Posts: 2
I created HHH-10262 with a testcase but am also posting here in case I can get some support or a solution.

We're migrating an app from EE6 to EE7 and Glassfish/Payara 4.1 mandates us to update Hibernate to a JPA2-compliant implementation. We still had Hibernate 3.6.10 previously and we used the good old Criteria API. We also have EJB3 and CDI in our legacy code, and the database is Derby 10.10 (dialect TenSeven used). the JDK is Oracle's v1.8.0_60.

So we were compelled to bump to 4.3.11, and, facing the issue below, we tried with 5.0.2 without success.
One of the entities has an Enum(String) PK. It used to work well with Hibernate 3.6 as per ANN-744:

Code:
@Entity
@Table(name="status")
public class Status {
private Organ organ;
private boolean active;

public Status() {}
public Status(Organ organ, boolean active) { this.organ = organ; this.active = active; }

@Id
@GeneratedValue(generator="assignedGen")
@GenericGenerator(name="assignedGen", strategy="assigned")
@Enumerated(EnumType.STRING)
@Column(name="organ", length=30, nullable=false)
public Organ getOrgan() { return organ; }

@Basic
@Column(name="active", nullable=false)
public boolean isActive() { return active; }
// and setters too
}


The enum itself being as simple an enum can be::
Code:
public enum Organ { AAAA, BBBB, CCCC; }


If I generate the DDL scripts, I get this, which is perfectly OK:
Code:
create table status (organ varchar(30) not null, active boolean not null, primary key (organ));

Now, when I launch the application, as soon as it fetches entities from this table, this exception is thrown:

Code:
Caused by: org.apache.derby.client.am.ColumnTypeConversionException: An attempt was made to get a data value of type 'byte[]' from a data value of type 'VARCHAR'.
   at org.apache.derby.client.am.Cursor.coercionError(Unknown Source)
at org.apache.derby.client.am.Cursor.getBytes(Unknown Source)
... 124 more

2015-11-09T01:03:54.138+0100|Severe: javax.ejb.EJBException
at com.sun.ejb.containers.EJBContainerTransactionManager.processSystemException(EJBContainerTransactionManager.java:750)
at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:700)
at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:505)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4566)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2074)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2044)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:220)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:88)
at com.sun.proxy.$Proxy244.getAllStatus(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.jboss.weld.util.reflection.Reflections.invokeAndUnwrap(Reflections.java:434)
at org.jboss.weld.bean.proxy.EnterpriseBeanProxyMethodHandler.invoke(EnterpriseBeanProxyMethodHandler.java:127)
at org.jboss.weld.bean.proxy.EnterpriseTargetBeanInstance.invoke(EnterpriseTargetBeanInstance.java:56)
at org.jboss.weld.bean.proxy.InjectionPointPropagatingEnterpriseTargetBeanInstance.invoke(InjectionPointPropagatingEnterpriseTargetBeanInstance.java:67)
at org.jboss.weld.bean.proxy.ProxyMethodHandler.invoke(ProxyMethodHandler.java:100)
at com.xxxxxxxxxxxxxxx.v2.core.common.CommonManager$712301675$Proxy$_$$_Weld$EnterpriseProxy$.getAllStatus(Unknown Source)
at com.xxxxxxxxxxxxxxx.v2.site.framework.ApplicationManager.reloadStatus(ApplicationManager.java:209)

Caused by: org.hibernate.exception.DataException: could not execute query
at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:52)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.loader.Loader.doList(Loader.java:2614)
at org.hibernate.loader.Loader.doList(Loader.java:2594)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2423)
at org.hibernate.loader.Loader.list(Loader.java:2418)
at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109)
at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1705)
at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:363)


Needless to say I also tried to remove the @GeneratedValue/@GenericGenerator lines to no avail.
Unless this behavior is mandated by the JPA spec, it looks to me we have a regression here.


Top
 Profile  
 
 Post subject: Re: Enumerated(String) Primary Key fetched as byte[]
PostPosted: Sat Dec 05, 2015 5:22 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
Using Enum as a Primary Key is not allowed by JPA:

Quote:
2.1.4 Primary Keys and Entity Identity

The primary key (or field or property of a composite primary key) should be one of the following types: any Java primitive type; any primitive wrapper type; java.lang.String; java.util.Date; java.sql.Date. In general, however, approximate numeric types (e.g., floating point types) should never be used in primary keys. Entities whose primary keys use types other than these will not be portable.


So I think you should use the associated identifier and reconstruct the Enum afterwards:

Code:
@Entity
@Table(name="status")
public class Status {

   private String id;

   @Id
   @Column(name="organ", length=30, nullable=false)
   public String getId() { return id; }
   
   @Transient
   public Organ getOrgan() {
      return Organ.valueOf(id);
   }

   ...
}


Top
 Profile  
 
 Post subject: Re: Enumerated(String) Primary Key fetched as byte[]
PostPosted: Sat Jan 02, 2016 5:42 pm 
Newbie

Joined: Mon Nov 16, 2015 11:30 am
Posts: 2
Hello
Thanks for your reply.
Yes indeed, I realized that.
I took another approach and just added an ID to the table.

Code:
  @javax.persistence.Id
  @GeneratedValue(
          strategy = GenerationType.AUTO
  )
  public Long getId() {
    return id;
  }

  @Enumerated(EnumType.STRING)
  @Column(name="organ", length=30, nullable=false)
    public Organ getOrgan() {
    return organ;
  }
 
  @Basic
  @Column(name="active", nullable=false)
  public boolean isActive() {
    return active;
  }


However, the result was the same.
So I thought maybe the issue was related to Derby. I quickly installed a MySQL instance, and ran it. Which led me there (that is, in the same issue):

Code:
[2016-01-02T22:32:25.205+0100] [Payara 4.1] [INFO] [] [] [tid: _ThreadID=121 _ThreadName=Thread-8] [timeMillis: 1451770345205] [levelValue: 800] [[
  Hibernate: select this_.id as id1_0_0_, this_.active as active2_0_0_, this_.organ as organ3_0_0_ from status this_]]

[2016-01-02T22:32:25.208+0100] [Payara 4.1] [WARN] [] [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] [tid: _ThreadID=121 _ThreadName=__ejb-thread-pool2] [timeMillis: 1451770345208] [levelValue: 900] [[
  SQL Error: 20000, SQLState: 22005]]

[2016-01-02T22:32:25.209+0100] [Payara 4.1] [ERROR] [] [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] [tid: _ThreadID=121 _ThreadName=__ejb-thread-pool2] [timeMillis: 1451770345209] [levelValue: 1000] [[
  An attempt was made to get a data value of type 'byte[]' from a data value of type 'VARCHAR'.]]

[2016-01-02T22:32:25.211+0100] [Payara 4.1] [WARNING] [AS-EJB-00056] [javax.enterprise.ejb.container] [tid: _ThreadID=121 _ThreadName=__ejb-thread-pool2] [timeMillis: 1451770345211] [levelValue: 900] [[
  A system exception occurred during an invocation on EJB StatusScheduler, method: public void com.whatever.ejb.StatusScheduler.testHHH10262()]]

[2016-01-02T22:32:25.212+0100] [Payara 4.1] [WARNING] [] [javax.enterprise.ejb.container] [tid: _ThreadID=121 _ThreadName=__ejb-thread-pool2] [timeMillis: 1451770345212] [levelValue: 900] [[
 
javax.ejb.EJBException
   at com.sun.ejb.containers.EJBContainerTransactionManager.processSystemException(EJBContainerTransactionManager.java:750)
   at com.sun.ejb.containers.EJBContainerTransactionManager.completeNewTx(EJBContainerTransactionManager.java:700)
   at com.sun.ejb.containers.EJBContainerTransactionManager.postInvokeTx(EJBContainerTransactionManager.java:505)
   at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4566)
   at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2074)
   at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2044)
   at com.sun.ejb.containers.BaseContainer.callEJBTimeout(BaseContainer.java:4080)
   at com.sun.ejb.containers.EJBTimerService.deliverTimeout(EJBTimerService.java:1200)
   at com.sun.ejb.containers.EJBTimerService.access$000(EJBTimerService.java:89)
   at com.sun.ejb.containers.EJBTimerService$TaskExpiredWork.run(EJBTimerService.java:1919)
   at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
   at java.util.concurrent.FutureTask.run(FutureTask.java:266)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
   at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.exception.DataException: could not execute query
   at org.hibernate.exception.internal.SQLExceptionTypeDelegate.convert(SQLExceptionTypeDelegate.java:52)
   at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
   at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
   at org.hibernate.loader.Loader.doList(Loader.java:2614)
   at org.hibernate.loader.Loader.doList(Loader.java:2594)
   at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2423)
   at org.hibernate.loader.Loader.list(Loader.java:2418)
   at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109)
   at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1716)
   at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:363)
   at com.whatever.ejb.StatusScheduler.testHHH10262(StatusScheduler.java:25)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:497)
   at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
   at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
   at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4786)
   at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:656)
   at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:824)
   at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
   at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:73)
   at org.jboss.weld.ejb.SessionBeanInterceptor.aroundInvoke(SessionBeanInterceptor.java:52)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:497)
   at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
   at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:823)
   at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:608)
   at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
   at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundTimeout(SystemInterceptorProxy.java:145)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
   at java.lang.reflect.Method.invoke(Method.java:497)
   at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
   at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:823)
   at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
   at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4758)
   at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4746)
   at com.sun.ejb.containers.BaseContainer.callEJBTimeout(BaseContainer.java:4051)
   ... 8 more
Caused by: java.sql.SQLDataException: An attempt was made to get a data value of type 'byte[]' from a data value of type 'VARCHAR'.
   at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
   at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
   at org.apache.derby.client.am.ResultSet.getBytes(Unknown Source)
   at org.apache.derby.client.am.ResultSet.getBytes(Unknown Source)
   at com.sun.gjc.spi.base.ResultSetWrapper.getBytes(ResultSetWrapper.java:556)
   at org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor$2.doExtract(VarbinaryTypeDescriptor.java:53)
   at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:47)
   at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:241)
   at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:237)
   at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:227)
   at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:303)
   at org.hibernate.persister.entity.AbstractEntityPersister.hydrate(AbstractEntityPersister.java:2726)
   at org.hibernate.loader.Loader.loadFromResultSet(Loader.java:1728)
   at org.hibernate.loader.Loader.instanceNotYetLoaded(Loader.java:1654)
   at org.hibernate.loader.Loader.getRow(Loader.java:1543)
   at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:727)
   at org.hibernate.loader.Loader.processResultSet(Loader.java:972)
   at org.hibernate.loader.Loader.doQuery(Loader.java:930)
   at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:336)
   at org.hibernate.loader.Loader.doList(Loader.java:2611)
   ... 46 more
Caused by: org.apache.derby.client.am.ColumnTypeConversionException: An attempt was made to get a data value of type 'byte[]' from a data value of type 'VARCHAR'.
   at org.apache.derby.client.am.Cursor.coercionError(Unknown Source)
   at org.apache.derby.client.am.Cursor.getBytes(Unknown Source)
   ... 64 more
]]



I was pretty deperate until I realized the at org.apache.derby.client...
DERBY ??? the lib is not even in my pom anymore. So I'll keep on searching what kind of stupid thing I may have done.


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.