I used the example
ECHO_AUTHOR table has AUTH_ID (PKEY), NAME, ADDRESS
ECHO_MESSAGE table has MSG_ID(PKEY), AUTH_ID(FKEY), MSG
With below mapping for EchoAuthor
Code:
<hibernate-mapping>
<class name="EchoAuthor" table="ECHO_AUTHOR" schema="SCOTT">
<id name="authId" type="java.lang.Long">
<column name="AUTH_ID" precision="12" scale="0" />
<generator class="assigned"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="40" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" length="40" />
</property>
<set name="messages" table="ECHO_MESSAGE" cascade="all" >
<key column="AUTH_ID"/>
<one-to-many class="EchoMessage"/>
</set>
</class>
</hibernate-mapping>
This works fine.
But I want a one-to-one mapping. So i changed it as :
Code:
<hibernate-mapping>
<class name="EchoAuthor" table="ECHO_AUTHOR" schema="SCOTT">
<id name="authId" type="java.lang.Long">
<column name="AUTH_ID" precision="12" scale="0" />
<generator class="assigned"></generator>
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="40" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" length="40" />
</property>
<one-to-one name="authId"
class="EchoMessage"
cascade="all" />
</class>
</hibernate-mapping>
And in EchoAuthor.java i have an object of EchoMessage. It throws the below exception
Code:
ERROR [main] (BasicPropertyAccessor.java:115) - IllegalArgumentException in class: co.uk.raileurope.hibernate.demo.EchoMessage, getter method of property: msgId
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of co.uk.raileurope.hibernate.demo.EchoMessage.msgId
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:119)
at org.hibernate.tuple.AbstractTuplizer.getIdentifier(AbstractTuplizer.java:103)
at org.hibernate.persister.entity.BasicEntityPersister.getIdentifier(BasicEntityPersister.java:2944)
at org.hibernate.persister.entity.BasicEntityPersister.isTransient(BasicEntityPersister.java:2705)
at org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181)
at org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:409)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:82)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468)
at org.hibernate.engine.Cascades$5.cascade(Cascades.java:154)
at org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:771)
at org.hibernate.engine.Cascades.cascade(Cascades.java:720)
at org.hibernate.engine.Cascades.cascade(Cascades.java:847)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:363)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:265)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:481)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:476)
at co.uk.raileurope.hibernate.demo.services.EchoService.addMessageToAuthor(EchoService.java:37)
at co.uk.raileurope.hibernate.demo.EchoTest.main(EchoTest.java:40)
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:324)
at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:105)
... 25 more
Save failed.
Can anyone please help?
Thanks,
uma