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.  [ 2 posts ] 
Author Message
 Post subject: Composite Foreign Key referencing Nonprimary Keys Issue
PostPosted: Mon Aug 08, 2016 2:50 pm 

Joined: Mon Aug 08, 2016 2:38 pm
Posts: 1
We are trying to migrate a project from hibernate 3 to hibernate 5. We have a legacy database that contains composite foreign key referencing non primary keys. To best explain the problem, I take the example from http://learningviacode.blogspot.com/201 ... ncing.html

Here is the mapping file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<hibernate-mapping package="com.ec.composite_foreign_non_primary">
<class name="Student" table="STUDENT">
<id name="id" type="integer" column="ID">
<generator class="native" />
<properties name="StudentKey" unique="true">
<property name="name" column="STUDENT_NAME" />
<property name="schoolName" column="SCHOOL_NAME" />
<set name="vehicles" cascade="all" inverse="true">
<key property-ref="StudentKey" >
<column name="STUDENT_NAME"/>
<column name="SCHOOL_NAME"/>
<one-to-many class="Vehicle" />

It works with Hibernate 3.3. With hibernate 5, it complains StudentKey not found.

Stack Trace:
Caused by: org.hibernate.MappingException: property [StudentKey] not found on entity [com.ec.composite_foreign_non_primary.Student]
at org.hibernate.mapping.PersistentClass.getRecursiveProperty(PersistentClass.java:425)
at org.hibernate.boot.model.source.internal.hbm.ModelBinder$AbstractPluralAttributeSecondPass.bindCollectionKey(ModelBinder.java:3266)
at org.hibernate.boot.model.source.internal.hbm.ModelBinder$AbstractPluralAttributeSecondPass.doSecondPass(ModelBinder.java:3086)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1621)
at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1589)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:278)
at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:83)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:418)
at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:87)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:692)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)

Does Hibernate 5 support at all this kind of mapping for legacy database? Your insight will be appreciated.

 Post subject: Re: Composite Foreign Key referencing Nonprimary Keys Issue
PostPosted: Tue Aug 09, 2016 1:20 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
It supports Composite Keys and more advanced mappings than 3.3. What you might want to try is to use Annotations for the entities with Composite Keys, as explained in this blog post. Annotations are better supported in 5.x than legacy HBM mappings.

Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.