Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Null @Id attributes in instrumented entity class
PostPosted: Tue Sep 13, 2016 12:03 pm 
Newbie

Joined: Tue Sep 13, 2016 11:24 am
Posts: 10
Hello,

I have an application in the following runtime env stack: Hibernate 5.2.2 (JPA 2.1), Spring 4.3.2, Java 8 (1.8.0_102), Weblogic 12.1.3 application server, Oracle 11 db.
I tried compile time instrumentation of my Hibernate persistence classes as indicated in (https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#BytecodeEnhancement) using hibernate-enhance-maven-plugin and setting :
Code:
                        <failOnError>true</failOnError>
                        <enableLazyInitialization>true</enableLazyInitialization>


I build the logic by specifying among the other goals: org.hibernate.orm.tooling:hibernate-enhance-maven-plugin:enhance. At Maven build time the goal is executed and it indicates in the console output that my Hibernate configured classes are successfully instrumented.

However at runtime I have 2 issues:
1 - the persistent entity attributes annotated with @Id are null, following an Hibernate a HQL NamedQuery execution. This entity is only queried through Hibernate. The CUD is done outside the application.
2 - for an entities hierarchy (superclass marked as @Inheritance(strategy = InheritanceType.SINGLE_TABLE)) only the superclass gets instrumented. The subclasses don't get instrumented, I checked with a Java decompiler(the setters/getters are not replaced with the interceptor calls). However at Maven build, when it executes the enhance goal, it displays that these subclasses were successfully instrumented.

Some more details about entity configuration from point 1: there is a superclass-subclasses hierarchy using @Inheritance(strategy = InheritanceType.SINGLE_TABLE))

Code:
@NamedQueries({

      @NamedQuery(name = MyAbstractClass.myQuery, query = "select b " + "from MyAbstractClass b "
                  + "where b.id1 = :" + Params.id1 + " order by  b.aDate"),      
      
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "ID1", discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorOptions(force=true)
@Table(name = "MY_TABLE")
public abstract class MyAbstractClass {

   public static final String myQuery = "MyAbstractClass.myQuery";
   
   
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "ID1")
   private Long id1;

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "ID2")
   private Long id2;

   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   @Column(name = "ID3")
   private Long id3;

   @Temporal(TemporalType.DATE)
   @Column(name = "A_DATE")
   private Date aDate;

   @Column(name = "A_STRING")
   private String aString;
   
   @Column(name="A_NUMBER")
   private Long nNumber
   
   //getters and setters for all properties above.
}


Each class is also annotated (@Entity, @Immutable, @DiscriminatorValue("123456") ) and it adds some other simple attributes (String,Boolean annotated with @Column).

When I run the MyAbstractClass.myQuery the returned entity has all populated attributes except the id1, id2, id3 which contain null. I checked in the db and it is not true. Moreover the query is based on id1 (see where clause).

I tried removing @GeneratedValue(strategy = GenerationType.IDENTITY) but nothing changes

Am I missing something / doing something wrong?
Is it any workaround for the 2 issues exposed above?


Thanks in advance


Top
 Profile  
 
 Post subject: Re: null @Id marked attributes of instrumented entity classes
PostPosted: Tue Sep 13, 2016 1:40 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1263
In a relational database, only a column can be set as IDENTITY. That's how auto-generated IDENTITY values work. Sequences allow you to have multiple columns that are populated by auto-generated values. However, Hibernate does not support multiple @GeneratedValue on same entity.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: null @Id marked attributes of instrumented entity classes
PostPosted: Wed Sep 14, 2016 8:47 am 
Newbie

Joined: Tue Sep 13, 2016 11:24 am
Posts: 10
Hi Vlad,

Indeed by applying your correction both issues are solved.

Thanks a lot for the help!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.