Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Composite ID properties not populated when class defined
PostPosted: Thu Jan 09, 2014 6:18 am 
Newbie

Joined: Thu Jun 14, 2012 11:00 am
Posts: 5
When defining a composite ID using hbm.xml, if I define the "class" attribute of the "composite-id" node for a class, the properties are not populated with the values retrieved from the database when a collection of entities are returned.

Code:
...     
<class name="com.acme.orders.entity.CustomerInventory" table="CUSTINVENTORY">

        <composite-id class="com.acme.orders.entity.CustomerInventoryPK">
            <key-property name="custId" type="integer" column="CUSTOMERID" access="field"/>
            <key-property name="id" type="java.lang.Long" column="ID" access="field"/>
        </composite-id>
   
        <version name="version" access="field" column="VERSION" type="integer"/>
        <property name="quantity" type="integer" column="QUANTITY"  access="field"/>
...


The config above causes a CustomerInventory object to have 0 and NULL values for the custId and id respectively

Code:
        customerInventory.id == null;
        customerInventory.custId == 0;


If I do not define the class name for the composite ID, the properties are populated with the correct values retrieved from the database;

Code:
...     
<class name="com.acme.orders.entity.CustomerInventory" table="CUSTINVENTORY">

        <composite-id>
            <key-property name="custId" type="integer" column="CUSTOMERID" access="field"/>
            <key-property name="id" type="java.lang.Long" column="ID" access="field"/>
        </composite-id>
   
        <version name="version" access="field" column="VERSION" type="integer"/>
        <property name="quantity" type="integer" column="QUANTITY"  access="field"/>
...


The above config populates a CustomerInventory object correctly;

Code:
        customerInventory.id == 27;
        customerInventory.custId == 1;


Is there a setting or something missing from my hbm.xml config that causes this behaviour? I need to be able to define the composite id class for other aspects of the application to function correctly.

Thanks

John


Top
 Profile  
 
 Post subject: Re: Composite ID properties not populated when class defined
PostPosted: Thu Jan 09, 2014 1:00 pm 
Newbie

Joined: Thu Jun 14, 2012 11:00 am
Posts: 5
In addition to the observations above, if i try the following setting setting;

Quote:
<composite-id class="com.acme.CustomerInventoryPK" mapped="true">


the following error is thrown;

Quote:
java.lang.IllegalArgumentException: expecting IdClass mapping
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:103)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_45]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_45]
at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_45]
at org.jboss.threads.JBossThread.run(JBossThread.java:122)
Caused by: java.lang.IllegalArgumentException: expecting IdClass mapping
at org.hibernate.ejb.metamodel.AttributeFactory$3.resolveMember(AttributeFactory.java:943)
at org.hibernate.ejb.metamodel.AttributeFactory$5.resolveMember(AttributeFactory.java:1001)
at org.hibernate.ejb.metamodel.AttributeFactory.determineAttributeMetadata(AttributeFactory.java:444)
at org.hibernate.ejb.metamodel.AttributeFactory.buildIdAttribute(AttributeFactory.java:148)
at org.hibernate.ejb.metamodel.MetadataContext.buildIdClassAttributes(MetadataContext.java:335)
at org.hibernate.ejb.metamodel.MetadataContext.applyIdMetadata(MetadataContext.java:271)
at org.hibernate.ejb.metamodel.MetadataContext.wrapUp(MetadataContext.java:192)
at org.hibernate.ejb.metamodel.MetamodelImpl.buildMetamodel(MetamodelImpl.java:83)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:106)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:914)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:899)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:76)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.createContainerEntityManagerFactory(PersistenceUnitServiceImpl.java:200)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl.access$600(PersistenceUnitServiceImpl.java:57)
at org.jboss.as.jpa.service.PersistenceUnitServiceImpl$1.run(PersistenceUnitServiceImpl.java:99)
... 4 more


The entity is annotated with javax.persistence.IdClass, but I have redefined the Entity configuration using hbm, and it does not appear to be mapping the composite ID correctly.


Top
 Profile  
 
 Post subject: Re: Composite ID properties not populated when class defined
PostPosted: Tue Sep 19, 2017 12:22 pm 
Newbie

Joined: Tue Sep 19, 2017 12:14 pm
Posts: 2
I've reproduced this issue with Hibernate 5.2.11.Final. I've made a minimal reproduction here: https://github.com/kmorozov/hibernate-i ... -test-case.
It does not look like I can edit the issue. Is there someway to gain Jira permissions? Otherwise, could someone update the affects version on the ticket?

Thanks!


Top
 Profile  
 
 Post subject: Re: Composite ID properties not populated when class defined
PostPosted: Tue Sep 19, 2017 2:25 pm 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1582
Location: Romania
Thanks. I see your comment on Jira, so you could edit it.

As a workaround, you can try with JPA Annotation mappings. The old HBM mappings might be deprecated and removed in a future version, so it's better to migrate to annotations or JPA XML mappings anyway.

_________________
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: Composite ID properties not populated when class defined
PostPosted: Tue Sep 19, 2017 4:06 pm 
Newbie

Joined: Tue Sep 19, 2017 12:14 pm
Posts: 2
vlad wrote:
Thanks. I see your comment on Jira, so you could edit it.


I only have the comment button in the action bar. Though I can create new issues.

vlad wrote:
As a workaround, you can try with JPA Annotation mappings. The old HBM mappings might be deprecated and removed in a future version, so it's better to migrate to annotations or JPA XML mappings anyway.


I surely hope not. HBM mappings serve a slightly different purpose from the JPA annotations. The HBM mappings let you decouple your database mapping and your model beans. This way, you don't have annotations from hibernate, json mappings, and Mongo cluttering up the same *.java file. Jackson, for example, has a neat mix-in system where you can define its annotation on a different class and then map them to your bean. This way you can keep your business logic separate from your mappings.

I don't see very much documentation on JPA XML. I don't think that it supports all the features of HBM.

Before you deprecate hbm.xml, you guys should add mix-ins :)


Top
 Profile  
 
 Post subject: Re: Composite ID properties not populated when class defined
PostPosted: Wed Sep 20, 2017 1:02 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1582
Location: Romania
Quote:
Before you deprecate hbm.xml, you guys should add mix-ins :)


That's the plan. In 6.0, there will be a new parser and a new SQM model which will allow us to provide a way to customize the JPA XML mapping as well. Once we have that, we can add the Hibernate-specific features, and, from that moment on, there is no need for HBM anymore since it will just complicate maintenance.

Meanwhile, you can try with the JPA orm.xml since it supports composite identifiers. You don't need to switch entirely, just provide this particular entity mapping through JPA orm.xml.

_________________
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: Composite ID properties not populated when class defined
PostPosted: Wed Oct 11, 2017 4:39 am 
Newbie

Joined: Wed Oct 11, 2017 4:31 am
Posts: 1
Hi,

I am facing the same issue, however I am upgrading a legacy system and changing all the entity mappings to annotation or JPA XML based would be more dangerous and the overall decision to upgrade the system might be worthless. The mappings are a big octopus that I am not sure I can translate easily and 1:1 to annotations.

Is there any other workaround on this, other than changing the mappings to annotations? Or speciifying the JPA XML entities?

In my opinion while this is currently a stock feature of hibernate to use hbm.xml mappings, and not supporting composite-id keys is a drawback.

Best Regards,
Mihai Ratiu


Top
 Profile  
 
 Post subject: Re: Composite ID properties not populated when class defined
PostPosted: Thu Oct 12, 2017 5:00 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1582
Location: Romania
It's an issue indeed. But there are many other issues which are to be fixed and have a higher priority.

If it's really a big problem for your project, then send us a Pull Request with a fix proposal and I'll review it. That's the true spirit of open-source software.

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


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