-->
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: Help please - One to many Relationship
PostPosted: Tue Feb 17, 2004 6:38 pm 
Newbie

Joined: Tue Feb 17, 2004 5:23 pm
Posts: 1
Hi,

This is the first time iam working with Hibernate and iam facing some problems. When iam implementing parent-child relationship a row in the parent table gets created but it does not create it in the child table. What happens is that an Hibernate exception is thrown Hibernate Exception SQL update or deletion failed (row not found). It is not attempting to create the new child record rather it is trying to update the child record which iam not able to understand. Iam using the generator class="assigned" for Id. Please help me out. I have spent lot of time to debug the problem but in vain.

The logs:

Hibernate: insert into event (title, hostname, hostid, location, contact_phone,
eventDate, description, img_filename, eventid) values (?, ?, ?, ?, ?, ?, ?, ?, ?
)
Hibernate: update invitee set eventid=?, i_firstname=?, i_lastname=?, status=? w
here i_emailid=?
Hibernate ExceptionSQL update or deletion failed (row not found)

Event.hbm.xml
-------------

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

<hibernate-mapping>
<class name="com.eforce.eInvitation.Event" table="event" >
<id name="eventId" column="eventid" unsaved-value="null">
<generator class="assigned"/>
</id>
<property name="title" column="title" type="java.lang.String" />
<property name="hostName" column="hostname" type="java.lang.String" />
<property name="hostId" column="hostid" type="java.lang.String" />
<property name="location" column="location" type="java.lang.String" />
<property name="contactphone" column="contact_phone" type="java.lang.String" />
<property name="eventDate" column="eventDate" type="java.lang.String" />
<property name="description" column="description" type="java.lang.String" />
<property name="img_filename" column="img_filename" type="java.lang.String" />

<set name="invitees" cascade="all">
<key column="eventid" />
<one-to-many class="com.eforce.eInvitation.Invitee" />
</set>

</class>
</hibernate-mapping>

public class Event implements Serializable {

private String eventId= null;
private String title = null;
private String hostId = null;
private String hostName = null;
private String location=null;
private String contactphone=null;
private String eventDate=null;
private String description=null;
private String img_filename=null;
private Set invitees = null;

public String getEventId()
{
return this.eventId;
}

public String getTitle()
{
return this.title;
}

public String getHostId()
{
return this.hostId;
}

public String getHostName()
{
return this.hostName;
}

public String getLocation()
{
return this.location;
}

public String getContactphone()
{
return this.contactphone;
}

public String getEventDate()
{
return this.eventDate;
}

public String getDescription()
{
return this.description;
}

public String getImg_filename()
{
return this.img_filename;
}

public Set getInvitees() {

return this.invitees;
}

public void setEventId(String eventId)
{
this.eventId = eventId;
}
public void setTitle(String title)
{
this.title = title;
}
public void setHostId(String hostId)
{
this.hostId = hostId;
}

public void setHostName(String hostName)
{
this.hostName = hostName;
}

public void setLocation(String location)
{
this.location = location;
}
public void setContactphone(String contactphone)
{
this.contactphone = contactphone;
}
public void setEventDate(String eventDate)
{
this.eventDate = eventDate;
}
public void setDescription(String description)
{
this.description = description;
}
public void setImg_filename(String img_filename)
{
this.img_filename = img_filename;
}
public void setInvitees(Set invitees) {
this.invitees = invitees;
}

}


Invitee.hbm.xml
---------------

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

<hibernate-mapping>
<class name="com.eforce.eInvitation.Invitee" table="invitee" >
<id name="i_emailId" column="i_emailid" unsaved-value="any">
<generator class="assigned"/>
</id>
<property name="eventId" column="eventid" type="java.lang.String" />
<property name="i_firstname" column="i_firstname" type="java.lang.String" />
<property name="i_lastname" column="i_lastname" type="java.lang.String" />
<property name="status" column="status" type="java.lang.String" />

</class>
</hibernate-mapping>

package com.eforce.eInvitation;

import java.io.Serializable;
import com.eforce.eInvitation.Event;


public class Invitee implements Serializable{

private String i_emailId = null;
private String eventId = null;
private String i_firstname = null;
private String i_lastname = null;
private String status = null;
private Event parent = null;

public Event getParent()
{
return this.parent;
}

public void setParent(Event parent)
{
this.parent = parent;
}


public String getI_emailId()
{
return this.i_emailId;
}

public String getEventId()
{
return this.eventId;
}

public String getI_firstname()
{
return this.i_firstname;
}

public String getI_lastname()
{
return this.i_lastname;
}

public String getStatus()
{
return this.status;
}

public void setI_emailId(String i_emailId)
{
this.i_emailId = i_emailId;
}

public void setEventId(String eventId)
{
this.eventId = eventId;
}

public void setI_firstname(String i_firstname)
{
this.i_firstname = i_firstname;
}

public void setI_lastname(String i_lastname)
{
this.i_lastname = i_lastname;
}

public void setStatus(String status)
{
this.status = status;
}

}
Service class
-------------
public void createEvent(CreateEventVO eventVO)
{

Session session = ConnectionFactory.getInstance().getSession();

try
{
guid = new GUID().toString();
System.out.println("New GUID : " + guid);
Transaction t = session.beginTransaction();
session.save(getEvent(eventVO));
t.commit();
}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
//throw new RuntimeException(e);
}
finally
{
if (session != null)
{
try
{
session.close();
}
catch (HibernateException e)
{
System.err.println("Hibernate Exception" + e.getMessage());
throw new RuntimeException(e);
}
}
}

}

public Event getEvent(CreateEventVO eventVO){

this.eventVO = eventVO;
event = new Event();

System.out.println("value of eventid " + guid);
event.setEventId(guid);
event.setTitle(eventVO.getTitle());
event.setHostName(eventVO.getHostName());
event.setHostId(eventVO.getHostId());
event.setLocation(eventVO.getLocation());
event.setContactphone(eventVO.getPhone());
event.setEventDate(eventVO.getDateTime());
event.setDescription(eventVO.getDescription());
event.setImg_filename(eventVO.getGraphics());

Set invitees = new HashSet();
Invitee invitee = null;

System.out.println("value of emain AList : "+ eventVO.getInviteeList().size());
if(eventVO.getInviteeList().size() > 0)
{
ArrayList emailId = (ArrayList)eventVO.getInviteeList();

for(int i = 0; i < eventVO.getInviteeList().size(); i++)
{
invitee = new Invitee();
invitee.setI_emailId(emailId.get(i).toString());
System.out.println("invitee.getI_emailId : "+ emailId.get(i).toString());
invitee.setEventId(guid);
invitees.add(invitee);
}
System.out.println("invitee size : "+ invitees.size());
}
event.setInvitees(invitees);

return event;
}[code][/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 7:25 pm 
Regular
Regular

Joined: Mon Nov 24, 2003 6:36 pm
Posts: 105
Hi, I based on the error you are showing us hibernate is attempting to update, instead of insert correct?
If you are using an assigned identifier, you have to tell hibernate how to differentiate a new/vs existing object (since it can't use the identifier)

see "unsaved-value" in the docs. Use some other field of the object (like a timestamp) and things will be better.


any reason you're using generator=assigned?

James


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 17, 2004 7:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
unsaved-value will not be of any help if you use asigned generator - after all, the id is the same when saved and unsaved :) You have to either use a version property with an unsaved-value or use an Interceptor and implement isUnsaved()


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 18, 2004 3:32 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This is a FAQ !
http://www.hibernate.org/116.html#A17
http://www.hibernate.org/116.html#A18
This question is posted so many times, I had to check the FAQ again to be sure.

_________________
Emmanuel


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.