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;
}
}