-->
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.  [ 5 posts ] 
Author Message
 Post subject: foreign key - what mapping to use
PostPosted: Fri Feb 17, 2006 10:40 am 
Newbie

Joined: Fri Feb 17, 2006 10:31 am
Posts: 6
Hi,

Probably a very trivial one in the Hibernate world. I am a newbie. I have 3 tables which are linked as

A.id = B.id , B.ref=C.ref. My goal is to get the data from all these tables together and put into a single Java data structure.

I am unable to figure how to have a mapping. Should i put them into separate classes and have one-to-one mapping.

I have reading/trying for 2 days and am still not clear.

Would appreciate any pointers/ help.

Thanks!!!


Top
 Profile  
 
 Post subject: Found a way - a doubt still
PostPosted: Fri Feb 17, 2006 1:39 pm 
Newbie

Joined: Fri Feb 17, 2006 10:31 am
Posts: 6
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


Top
 Profile  
 
 Post subject: Found a way - a doubt still
PostPosted: Fri Feb 17, 2006 1:40 pm 
Newbie

Joined: Fri Feb 17, 2006 10:31 am
Posts: 6
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 17, 2006 2:11 pm 
Newbie

Joined: Fri Feb 17, 2006 9:10 am
Posts: 4
Code:
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class


This will help you. Hibernate is trying to get a value in the msgId of the EchoMessage class, but the class that returns is not what Hibernate was expecting.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 18, 2006 12:57 am 
Beginner
Beginner

Joined: Mon Jan 30, 2006 2:28 am
Posts: 47
Location: INDIA
So u are using 3 tables each table should have it's own mapping files(.hbm.xml) file. And its setter and getter file.

for eg:

DB TABLE NAME : UserLogin (username,password)
So I need UserLogin.hbm.xml file
and UserLogin.java file It look like

setter and getter methods of all fields.

See your exception:

I msgId is missing in your Java file. So check that. and add setter and getter method for msgId.

Thank You. Further Query ask me with out hesitation.

_________________
A.Edward Durai
"The things which are impossible with men are possible with God."


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