-->
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.  [ 3 posts ] 
Author Message
 Post subject: cascading updates with many-to-one relationship
PostPosted: Wed Sep 03, 2003 4:15 am 
Newbie

Joined: Wed Aug 27, 2003 12:26 pm
Posts: 5
Hi folks,

I am having trouble with cascading updates in a simple many-to-one relationship. I have an class called LanguageAbility with a one-to-many relationship with a class called Language. When I create a new LanguageAbility object, assign it a non-persisted Language object, then persist the LanguageAbility object, I would expect the Language object to be persisted also (with cascade="all"). However, when I try to do that, as shown in Test.java, I get the following error:

Code:
net.sf.hibernate.HibernateException: SQL update or deletion failed (row not found)


I don't understand what i am doing wrong. Thanks in advance for any help with this, and if I need to post more info please tell me.

Cheers,

James

Test.java
=========

public class Test
{
public static void main(String[] args)
{
try
{
Configuration cfg = new Configuration().configure();
SessionFactory sessions = cfg.buildSessionFactory();
Session sess = sessions.openSession();

Transaction tx = sess.beginTransaction();

Language l = new Language("Spanish", Language.WRITTEN);

LanguageAbility la = new LanguageAbility(l, 9);

sess.save(la);

sess.flush();

tx.commit();
}
catch(Exception e)
{
System.out.println(e);
}
}
}

LanguageAbility.hbm.xml
=======================

<hibernate-mapping>
<class name="LanguageAbility" table="languageability">
<id name="id">
<generator class="native"/>
</id>

<property name="ability" not-null="true"/>

<many-to-one name="language" column="language_id" not-null="true" cascade="all"/>
</class>
</hibernate-mapping>


Language.hbm.xml
================

<hibernate-mapping>
<class name="Language" table="languages">
<id name="id">
<generator class="native"/>
</id>

<property name="name">
<column name="name" sql-type="varchar(64)" not-null="true"/>
</property>

<property name="type">
<column name="type" not-null="true"/>
</property>
</class>
</hibernate-mapping>



Language.java
=============

public class Language
{
public static final int SPOKEN = 1;
public static final int WRITTEN = 2;

private String name;
private int id;
private int type;

public Language(){};

public Language(String name, int type)
{
this.name = name;
this.type = type;
}

public String getName()
{
return name;
}

public int getType()
{
return type;
}

public int getId()
{
return id;
}

public void setName(String name)
{
this.name = name;
}

public void setType(int type)
{
this.type = type;
}

public void setId(int id)
{
this.id = id;
}
}

LanguageAbility.java
====================

public class LanguageAbility
{
private Language language;
private int ability;
private int id;

public LanguageAbility(){};

public LanguageAbility(Language language, int ability)
{
this.language = language;
this.ability = ability;
}

public Language getLanguage()
{
return language;
}

public int getAbility()
{
return ability;
}


public int getId()
{
return id;
}

public void setLanguage(Language language)
{
this.language = language;
}

public void setAbility(int ability)
{
this.ability = ability;
}

public void setId(int id)
{
this.id = id;
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 03, 2003 5:57 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
This is a FAQ.


set unsaved-value="0"


re-read Parent/Child Relationship


Top
 Profile  
 
 Post subject: cascading updates with many-to-one relationship
PostPosted: Thu Nov 04, 2004 5:43 am 
Newbie

Joined: Thu Oct 28, 2004 8:11 am
Posts: 1
I tried using unsaved-value="0".But it does not work.However it works using unsaved-value="any" in the id tag.


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