-->
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.  [ 1 post ] 
Author Message
 Post subject: Foreign key referencing nonprimary keys
PostPosted: Mon Mar 29, 2010 5:19 pm 
Newbie

Joined: Mon Mar 29, 2010 5:14 pm
Posts: 1
I have a scenario where the foreignkey in the child table references a part of primarykey in the parent table which is also unique. This is an existing database.

I remapped the unique primary key part and used the property-ref attribute to reference the remapped property.

I am getting the following error.

29-Mar-2010 11:24: AM. ERROR BasicPropertyAccessor.get:191 - IllegalArgumentException in class: com.test.model.StudentId, getter method of property: stateId
Exception in thread "main" org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.test.model.StudentId.stateId
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:195)
at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValue(AbstractComponentTuplizer.java:87)
at org.hibernate.tuple.component.AbstractComponentTuplizer.getPropertyValues(AbstractComponentTuplizer.java:93)
at org.hibernate.tuple.component.PojoComponentTuplizer.getPropertyValues(PojoComponentTuplizer.java:109)
at org.hibernate.type.ComponentType.getPropertyValues(ComponentType.java:376)
at org.hibernate.type.ComponentType.getHashCode(ComponentType.java:207)
at org.hibernate.engine.EntityKey.generateHashCode(EntityKey.java:126)
at org.hibernate.engine.EntityKey.<init>(EntityKey.java:70)
at org.hibernate.engine.StatefulPersistenceContext.getCollectionOwner(StatefulPersistenceContext.java:701)
at org.hibernate.loader.Loader.readCollectionElement(Loader.java:1016)
at org.hibernate.loader.Loader.readCollectionElements(Loader.java:669)
at org.hibernate.loader.Loader.getRowFromResultSet(Loader.java:614)
at org.hibernate.loader.Loader.doQuery(Loader.java:724)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.loadCollection(Loader.java:2019)
at org.hibernate.loader.collection.CollectionLoader.initialize(CollectionLoader.java:59)
at org.hibernate.persister.collection.AbstractCollectionPersister.initialize(AbstractCollectionPersister.java:587)
at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:83)
at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1744)
at org.hibernate.collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:366)
at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:108)
at org.hibernate.collection.PersistentSet.iterator(PersistentSet.java:186)
at com.test.Test.main(Test.java:26)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:169)
... 22 more


Hibernate version : 3.3.1 GA

Reference Book :
Book : Java Persistance with Hibernate (i.e. Revised Edition of Hibernate In Action)
ISBN : 1-932394-88-5

Source xml for Mapping:

<hibernate-mapping>
<class name="com.test.model.Student" table="STUDENT" schema="APP" entity-name="student">

<composite-id name="id" class="com.test.model.StudentId">
<key-property name="stateId" type="string">
<column name="SSN" length="10" />
</key-property>
<key-property name="rollNumber" type="string">
<column name="RNUM" length="10" />
</key-property>
</composite-id>
<!-- Remapped part of primary key -->
<property name="roll" type="string" update="false" insert="false" unique="true">
<column name="RNUM" length="20"/>
</property>
<property name="fName" type="string">
<column name="FNM" length="20" />
</property>
<property name="lName" type="string">
<column name="LNM" length="20" />
</property>
<set name="progressCards">
<key column="RNUM" property-ref="roll"/>
<one-to-many entity-name="progresscard" />
</set>
</class>
<class name="com.test.model.ProgressCard" table="PROGRESSCARD" schema="APP" entity-name="progresscard">

<id name="examCode" type="string">
<column name="TESTNM" length="10" />
<generator class="assigned" />
</id>
<property name="rollNumber" type="string">
<column name="RNUM" length="10" />
</property>
<property name="score1" type="java.lang.Integer">
<column name="MARX1" />
</property>
<property name="score2" type="java.lang.Integer">
<column name="MARX2" />
</property>
</class>
</hibernate-mapping>[/b]

Thanks & Regards,
Srinivas Arava.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.