-->
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.  [ 4 posts ] 
Author Message
 Post subject: Dependency Object mapping
PostPosted: Fri Dec 07, 2007 4:49 am 
Newbie

Joined: Fri Dec 07, 2007 4:37 am
Posts: 2
Hi I am using hibernate3 to develop my application
and i have a situation where i have the dependency objects to be saved
My Code is as follows

package org.un.desa.cims.iCso;

import java.util.Set;

public class MyTest {
private int id;
private String myName;
private MyTestInfo myTestInfo;
private Set<MySubTest> mySubTest;

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMyName() {
return myName;
}
public void setMyName(String myName) {
this.myName = myName;
}
public MyTestInfo getMyTestInfo() {
return myTestInfo;
}
public void setMyTestInfo(MyTestInfo myTestInfo) {
this.myTestInfo = myTestInfo;
}
public Set<MySubTest> getMySubTest() {
return mySubTest;
}
public void setMySubTest(Set<MySubTest> mySubTest) {
this.mySubTest = mySubTest;
}
}
class MyTestInfo{
private int testId;
private String info;

public int getTestId() {
return testId;
}
public void setTestId(int testId) {
this.testId = testId;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}

class MySubTest {
private int testId;
private String xtra;

public int getTestId() {
return testId;
}
public void setTestId(int testId) {
this.testId = testId;
}
public String getXtra() {
return xtra;
}
public void setXtra(String xtra) {
this.xtra= xtra;
}
}

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class name="org.un.desa.cims.iCso.MyTest" table="MyTest">
<id name="id" type="int">
<generator class="native"/>
</id>

<property name="myName"/>
<one-to-one name="myTestInfo" class="org.un.desa.cims.iCso.MyTestInfo" cascade="all"/>
<set name="mySubTest" table="MySubTest" cascade="all">
<key column="testId" />
<one-to-many class="org.un.desa.cims.iCso.MySubTest"/>
</set>
</class>

<class name="org.un.desa.cims.iCso.MyTestInfo" table="MyTestInfo">
<id name="testId" column="testId">
<generator class="foreign">
<param name="property">testId</param>
</generator>
</id>
<property name="info" />
</class>

<class name="org.un.desa.cims.iCso.MySubTest" table="MySubTest">
<id name="testId" column="testId">
<generator class="foreign">
<param name="property">testId</param>
</generator>
</id>
<property name="xtra" />
</class>
</hibernate-mapping>



can somebody help me for this i can not have auto incremental id in my MySubTest and MyTestInfo tables
but this is not working


Last edited by avya on Sat Dec 08, 2007 6:27 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 07, 2007 10:39 am 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
You could set Cascade to ALL (see documentation how exactly to do that) on the reference mappings. This will cascade any updates and the like to dependent object.

I don't understand the part about not being able to use auto id. So a little more explanation on your side would be necessary.

regards
Jens

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 08, 2007 6:23 am 
Newbie

Joined: Fri Dec 07, 2007 4:37 am
Posts: 2
It is because my table is on-to-many dependent so i dont want to keep extra primary key for extended table which are MyTestInfo and MySubTest
so i will just refer the data with foreign key
cascade is already set it is giving me following error

org.springframework.orm.hibernate3.HibernateSystemException: Unable to resolve property: testId; nested exception is org.hibernate.HibernateException: Unable to resolve property: testId
Caused by: org.hibernate.HibernateException: Unable to resolve property: testId
at org.hibernate.tuple.entity.EntityMetamodel.getPropertyIndex(EntityMetamodel.java:357)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValue(AbstractEntityTuplizer.java:286)
at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValue(AbstractEntityPersister.java:3519)
at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:39)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:98)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:98)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$1.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:437)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:326)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:180)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:186)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:175)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:98)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:495)
at org.springframework.orm.hibernate3.HibernateTemplate$16.doInHibernate(HibernateTemplate.java:679)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:362)
at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:676)
at org.un.desa.cims.iCso.MyTestDaoHibernate.saveMyTest(MyTestDaoHibernate.java:38)
at org.un.desa.cims.iCso.MyTestDaoTest.testAddAndUpdateMyTest(MyTestDaoTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 10, 2007 4:45 pm 
Expert
Expert

Joined: Thu Jul 05, 2007 9:38 am
Posts: 287
You know, if you get an exception, and want help with it, it helps to actually tell people that you got an exception.


Try replacing:
Code:
<generator class="foreign">
<param name="property">testId</param>
</generator>


with

Code:
<generator class="foreign">
<param name="property">id</param>
</generator>


It is supposed to refere to the attribute on the other side of the relation wich is "id" im MyTest.

regards
Jens

_________________
Please rate useful posts.


Schauderhaft: Softwaredevelopment, Projectmanagement, Qualitymanagement and all things "schauderhaft"


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