Changing Id type

41 views
Skip to first unread message

andreu...@taskize.co.uk

unread,
Mar 9, 2016, 6:35:22 AM3/9/16
to Sculptor Generator
Hi,

I am trying to change the id type to one of our classes. 

I tried changing the sculptor generator properties as:

id.type=CustomId

javaType.IDTYPE=com.company.repository.api.CustomId

I had only added the id.type one but it was not working (the JpaRepository classes still had Long as second generic). I read in this forum about setting the second variable and it seemed to make things better but it is still not working properly.

I believe it is due to the id field missing the Type annotation. I have to say we use this CustomId type in other fields and it works. We have set the properties

javaType.CustomId=com.company.repository.api.CustomId
db.postgresql.type.CustomId=BIGINT
hibernateType.CustomId=com.company.repository.postgres.CustomIdUserType

and I can see the annotations in the fields

@Column(name = "FIELD_ID")
@Type(type = "com.company.repository.postgres.CustomIdUserType")
private CustomId fieldId;

but for the id we only have

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "entityIdGen")
@GenericGenerator(name = "entityIdGen", strategy = "com.company.repository.api.ProductIdGenerator", parameters = { @Parameter(name = "sequence_name", value = "entity_sequence") })
@Column(name = "ID")
private CustomId id;

The exception I am getting is:

....

Caused by: org.hibernate.type.SerializationException: could not deserialize

        at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:262)

        at org.hibernate.internal.util.SerializationHelper.deserialize(SerializationHelper.java:306)

        at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.fromBytes(SerializableTypeDescriptor.java:155)

        at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:130)

        at org.hibernate.type.descriptor.java.SerializableTypeDescriptor.wrap(SerializableTypeDescriptor.java:44)

        at org.hibernate.type.descriptor.sql.VarbinaryTypeDescriptor$2.doExtract(VarbinaryTypeDescriptor.java:70)

        at org.hibernate.type.descriptor.sql.BasicExtractor.extract(BasicExtractor.java:64)

        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:267)

        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:263)

        at org.hibernate.type.AbstractStandardBasicType.nullSafeGet(AbstractStandardBasicType.java:253)

        at org.hibernate.type.AbstractStandardBasicType.hydrate(AbstractStandardBasicType.java:338)

        at org.hibernate.loader.Loader.extractKeysFromResultSet(Loader.java:785)

        at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:721)

        at org.hibernate.loader.Loader.processResultSet(Loader.java:953)

        at org.hibernate.loader.Loader.doQuery(Loader.java:921)

        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)

        at org.hibernate.loader.Loader.doList(Loader.java:2554)

        at org.hibernate.loader.Loader.doList(Loader.java:2540)

        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2370)

        at org.hibernate.loader.Loader.list(Loader.java:2365)

        at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:497)

        at org.hibernate.hql.internal.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:387)

        at org.hibernate.engine.query.spi.HQLQueryPlan.performList(HQLQueryPlan.java:236)

        at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1300)

        at org.hibernate.internal.QueryImpl.list(QueryImpl.java:103)

        at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:573)

        at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:495)

        at org.hibernate.jpa.criteria.compile.CriteriaQueryTypeQueryAdapter.getSingleResult(CriteriaQueryTypeQueryAdapter.java:71)

        at org.springframework.data.jpa.repository.query.JpaQueryExecution$SingleEntityExecution.doExecute(JpaQueryExecution.java:206)

        at org.springframework.data.jpa.repository.query.JpaQueryExecution.execute(JpaQueryExecution.java:78)

        at org.springframework.data.jpa.repository.query.AbstractJpaQuery.doExecute(AbstractJpaQuery.java:100)

        at org.springframework.data.jpa.repository.query.AbstractJpaQuery.execute(AbstractJpaQuery.java:91)

        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:462)

        at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:440)

        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)

        at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)

        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)

        at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)

        at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)

        at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)

        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)

        at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)

        ... 80 more

Caused by: java.io.EOFException

        at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2328)

        at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2797)

        at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:802)

        at java.io.ObjectInputStream.<init>(ObjectInputStream.java:299)

        at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:328)

        at org.hibernate.internal.util.SerializationHelper$CustomObjectInputStream.<init>(SerializationHelper.java:318)

        at org.hibernate.internal.util.SerializationHelper.doDeserialize(SerializationHelper.java:237)

pavel.tavoda

unread,
Mar 9, 2016, 8:56:25 AM3/9/16
to Sculptor Generator
It looks like problem with your customID type not with what we generate.
However surrogate key (id) is strongly recommended anyway ;-).

/Pavel

andreu...@taskize.co.uk

unread,
Mar 9, 2016, 9:48:58 AM3/9/16
to Sculptor Generator
Could you give me an example of an implementation of a CustomId that works? We are already using that CustomId type in other fields and it works, it is only when trying to change the id to it that the app breaks. 
I did not fully understand your comment on surrogate key, I am not trying to remove the id, I am just trying to change the type of it from long to CustomId.

Cheers

Torsten Juergeleit

unread,
Mar 9, 2016, 4:34:03 PM3/9/16
to Sculptor Generator
I did not fully understand your comment on surrogate key,

The surrogate key "id" is a built-in attribute of the generated persistent domain objects holding the primary key. The attributes DSL datatype is defined via the generator property "id.type" (default "IDTYPE").
The corresponding java type mapping is defind via the generator property "javaType.<DSL type>", e.g. "javaType.IDTYPE".

To define the mapping of these DSL types like "IDTYPE" to a corresponding database type the generator property "db.<database product>.type.IDTYPE" is used.
For mapping complex Java types to a database representation you need a corresponding Hibernate User Type definition, here "hibernateType.IDTYPE=com.company.repository.postgres.CustomIdUserType".

So you end up with the following generator properties:

id.type=IDTYPE // Default
javaType.IDTYPE=com.company.repository.api.CustomId   // mapping to Java type
hibernateType.IDTYPE=com.company.repository.postgres.CustomIdUserType
db.postgresql.type.IDTYPE=<database representation>    // mapping to database type for DDL


To use "CustomId" a an custom DSL type in your models (as an alias for "IDTYPE") define the following additional generator properties:

javaType.CustomId=com.company.repository.api.CustomId
hibernateType.CustomId=com.company.repository.postgres.CustomIdUserType 
db.postgresql.type.CustomId=<database representation>


Could you give me an example of an implementation of a CustomId that works?

andreu...@taskize.co.uk

unread,
Mar 18, 2016, 9:37:22 AM3/18/16
to Sculptor Generator
Hi,

we ended up fixing the issue ourselves with an extend. I can send the code to anyone interested, but the simple solution is to add something like this:
@ChainOverride
public class DomainObjectAttributeAnnotationTmplOverride extends DomainObjectAttributeAnnotationTmpl {

@Inject extension DbHelper dbHelper
@Inject extension Helper helper
@Inject extension Properties properties

override String idAnnotations(Attribute it) {
'''
@javax.persistence.Id
@org.hibernate.annotations.Type(type = "com.company.repository.postgres.CustomIdUserType")
@javax.persistence.Column(name="«it.getDatabaseName()»")
'''
}
}

Cheers
Reply all
Reply to author
Forward
0 new messages