-->
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 bidirectional cascding operations
PostPosted: Wed Jul 30, 2008 11:37 am 
Newbie

Joined: Wed Jul 30, 2008 11:28 am
Posts: 2
Hi All,


I am using hibernate3 to develop my one application. I have two tables which are having parent and child relationship.

First Mapping file: (peace of code)
---------------------------------------------------
<bag name="eventsList" table="CR_T_EVENT_DETAILS" lazy="false" cascade="all" inverse="true">
<key column="ANNOUNCEMENT_SEQ_ID"/>
<one-to-many class="com.sandp.care.model.EventDetailsModel"/>
</bag>

Second Mapping file: (peace of code)
---------------------------------------------------
<many-to-one name="announcementSummaryModel" class="com.sandp.care.model.AnnouncementSummaryModel" not-null="true">
<column name="ANNOUNCEMENT_SEQ_ID" precision="22" scale="0" />
</many-to-one>



Problem : When I am trying to insert saveOrUpdate method on parent object, I am getting following exception..
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.sandp.care.model.AnnouncementSummaryModel
at org.hibernate.engine.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:219)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:397)
at org.hibernate.type.ManyToOneType.isDirty(ManyToOneType.java:242)
at org.hibernate.type.TypeFactory.findDirty(TypeFactory.java:563)
at org.hibernate.persister.entity.AbstractEntityPersister.findDirty(AbstractEntityPersister.java:3071)
at org.hibernate.event.def.DefaultFlushEntityEventListener.dirtyCheck(DefaultFlushEntityEventListener.java:472)
at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:197)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:120)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:196)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.sandp.care.dao.CreateAnnouncementDAO.addDraftAnnouncement(CreateAnnouncementDAO.java:90)
at com.sandp.care.bo.CreateAnnouncementBO.addDraftAnnouncement(CreateAnnouncementBO.java:422)
at com.sandp.care.action.CreateAnnouncementAction.saveAsDraft(CreateAnnouncementAction.java:674)
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:585)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:763)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:225)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:127)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3214)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:121)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:1983)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:1890)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1344)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:209)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:181)

Can anybody help on this please..

Thanks in Advance,
Vinod


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 31, 2008 10:20 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
I think you might not be saving both sides of the relationship. Make sure both sides of the relationship hit a Session save.

I've got a little tutorial on my site. Check it out.

http://www.hiberbook.com/HiberBookWeb/learn.jsp?tutorial=18mappingonetomanyassociations

You'll notice I save both sides of the relationship between a player and a team:

Code:
import javax.persistence.*;
@Entity
public class Player {
private long id;
private String nickName;
private Team team;

@ManyToOne
@JoinColumn(name="team_id")     
public Team getTeam() {return team;}
public void setTeam(Team t) {this.team = t;}
@Id
@GeneratedValue
public long getId() {return id;}
public void setId(long id) {this.id = id;}
public String getNickName() {return nickName;}
public void setNickName(String n) {nickName = n;}
}


import java.util.List; import javax.persistence.*;
@Entity
public class Team {
private long id;
private String name;
private List<Player> players;

@OneToMany(mappedBy="team", 
              targetEntity=Player.class,
    fetch=FetchType.EAGER, cascade = CascadeType.ALL)
public List<Player> getPlayers() {return players;}
public void setPlayers(List<Player> p){players=p;}
@Id
@GeneratedValue
public long getId() {return id;}
public void setId(long id) {this.id = id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
}


Session session = HibernateUtil.beginTransaction();
Team team = new Team();
Player p1 = new Player();
Player p2 = new Player();
session.save(team);
session.save(p1);
session.save(p2);


team.setName("Pickering Atoms");
p1.setNickName("Lefty");
p1.setTeam(team);
p2.setNickName("Blinky");
p2.setTeam(team);
HibernateUtil.commitTransaction();

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.