-->
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.  [ 2 posts ] 
Author Message
 Post subject: Problem with one-to-many mapping
PostPosted: Thu Jun 28, 2007 3:32 am 
Newbie

Joined: Thu May 18, 2006 6:36 am
Posts: 11
Location: Bangalore
Hi,

I am trying the one-to-many relationship in hibernate and am getting the following error ,

Quote:
[6/28/07 12:36:50:704 IST] 13cc2959 AbstractFlush E org.hibernate.event.def.AbstractFlushingEventListener Could not synchronize database state with session
[6/28/07 12:36:50:766 IST] 13cc2959 AbstractFlush E org.hibernate.event.def.AbstractFlushingEventListener TRAS0014I: The following exception was logged org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.coalesce.vo.COLTask
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:78)
at org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:755)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1143)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:26)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:387)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:368)
at org.springframework.orm.hibernate3.HibernateTemplate.saveOrUpdate(HibernateTemplate.java:681)


I have a two table Projects and Tasks and I need a relation ship between the two,

Quote:
<class name="com.coalesce.vo.COLProject" table="col_projects" lazy="true">
<id column="projectid" name="projectId" type="java.lang.Long">
<generator class="sequence">
<param name="sequence">col_projects_seq</param>
</generator>
</id>

<property column="project_name" name="projectName" type="java.lang.String" />
<property column="project_code" name="projectCode" type="java.lang.String"/>
<property column="created_on" name="createdOn" type="timestamp" />
<property column="updated_on" name="updatedOn" type="timestamp"/>
<property column="created_by" name="createdBy" type="java.lang.String" />
<property column="updated_by" name="updateBy" type="java.lang.String"/>

<set name="tasks">
<key column="projectid" not-null="true"/>
<one-to-many class="com.coalesce.vo.COLTask"/>
</set>

</class>


Task

Quote:
<class name="com.coalesce.vo.COLTask" table="col_tasks">
<id column="task_id" name="taskId" type="java.lang.Long">
<generator class="sequence">
<param name="sequence">col_tasks_seq</param>
</generator>
</id>

<property column="task_name" name="taskName" type="java.lang.String" />
<property column="task_desc" name="taskDesc" type="java.lang.String"/>
<property column="RTS_no" name="rtsNo" type="integer" />
<property column="estimatedStartdate" name="estimatedStartdate" type="date"/>
<property column="estimatedEndDate" name="estimatedEndDate" type="date" />
<property column="actualStartDate" name="actualStartDate" type="date"/>
<property column="actualEndDate" name="actualEndDate" type="date"/>
<property column="createdBy" name="createdBy" type="java.lang.String"/>
<property column="created_on" name="createdOn" type="timestamp"/>


</class>


I am trying to do the following in code,

Code:
COLProject project = projectDAO.getProject(new Long(8));
          
           COLTask task = new COLTask();
           task.setTaskName("Rohit");
           task.setTaskDesc("Description is not important");
           task.setRtsNo(1256);
           task.setEstimatedStartdate(new Date());
           task.setEstimatedStartdate(new Date());
           task.setActualEndDate(new Date());
           task.setActualStartDate(new Date());
           task.setCreatedBy("Rohit");
           task.setCreatedOn(timeHelp.getTimestamp());
          
           tasks.add(task);
          
           project.setTasks(tasks);
          
           projectDAO.saveProject(project);


Thanks,
Rohit


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 28, 2007 7:52 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
Either save each Task before you save the parent, or use cascade:

<set name="tasks" cascade="all">

You will probably get an insert and then an update, so it is recommended to use a bidirectional relationship with inverse=true on the <set>. Read the first few sections of the manual on this and it will explain why.

http://www.hibernate.org/hib_docs/v3/re ... child.html

-Chris

_________________
Chris Bredesen
Senior Software Maintenance Engineer, JBoss


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