-->
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: xml mapping one-to-many on non-primary key columns
PostPosted: Mon Jun 06, 2016 7:58 am 
Newbie

Joined: Mon Jun 06, 2016 7:56 am
Posts: 2
We have a requirement where in we need to use cfg xml instead of annotations for ORM.

We are using hibernate 3 and are stuck on one of the annotations

in TableA:

Code:
@ManyToOne(fetch = FetchType.LAZY,optional=false)
@JoinColumn(name="column1",referencedColumnName="coumn1b")
TableB attributeb;


in TableB:

Code:
@OneToMany(fetch = FetchType.LAZY,mappedBy="attributeb")
private Set<TableA> attributeA;


I have tried doing below in mappings

for TableA

Code:
<many-to-one name="attributeb"
property-ref="column1b"
column="column1"
class="TableB"
lazy="no-proxy"
>



for TableB

Code:
<set name="attributeA"
table="tableA" lazy="no-proxy" inverse="true">
<key>
<column name="column1b" not-null="true"/>
</key>
<one-to-many class="TableA"/>
</Set>



But it's not working. Column1b is not the primary key for TableB, It has a composite primary key. So we want to have a many to one relation on a non-primary column, but its always trying to join to primary key and gives error that not enough attributes passed as primary key.

Could any one suggest correct mapping in XML for such annotations?


Top
 Profile  
 
 Post subject: Re: xml mapping one-to-many on non-primary key columns
PostPosted: Mon Jun 06, 2016 9:09 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
The many-to-one property-ref must reference a property, not a column.
As suggested in this SO answer, you should add a column1b property in the many-to-one class and a mapping like this:

Code:
<property name="column1b" column="column1b"/>

<many-to-one name="attributeb"
property-ref="column1b"
column="column1"
class="TableB"
lazy="no-proxy"
>


Top
 Profile  
 
 Post subject: Re: xml mapping one-to-many on non-primary key columns
PostPosted: Mon Jun 06, 2016 9:18 am 
Newbie

Joined: Mon Jun 06, 2016 7:56 am
Posts: 2
Hi

I get an error as
Caused by: org.hibernate.PropertyNotFoundException: field [column1b] not found on TableA
at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:182) ~[hibernate3.jar:3.6.10.Final]
at org.hibernate.property.DirectPropertyAccessor.getField(DirectPropertyAccessor.java:174) ~[hibernate3.jar:3.6.10.Final]

I have tried something as
<many-to-one name="attributeb"
property-ref="column1b"
fetch="select"
class="TableB"
lazy="true">
<column name="column1" />
</many-to-one>

This works as far as lazy load. But once i move to eager and load TableB, i get error .

For one to many the mapping i am using is
<set name="attributeA" cascade="all" inverse="true" >
<key column="column1" property-ref="column1b" />
<one-to-many class="TableA" />
</set>

I am thinking may be the one-to-many mapping is wrong.


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.