Hi,
I have probably an easy case.
I've a class Person and another one called Firma (= company in English) with the relation as follows:
0..* Person works in exactly one Company.
It's no problem adding new Persons to the database, however, the ID for the Company the person works is not updated (though it is set).
Here are my mappings:
PERSON:
Code:
<class name="Person" table="personen">
<id name="id" column="id" type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">sq_personen_id</param>
</generator>
<!-- Foreign Key Reference zu view_Benutzer und 1-1 Assoziation -->
<!--
<generator class="foreign">
<param name="property">benutzer</param>
</generator>
//-->
</id>
<property name="nachname" column="nachname" type="java.lang.String" not-null="true" />
<property name="titel" column="titel" type="java.lang.String" />
<property name="vorname" column="vorname" type="java.lang.String" />
<property name="strasse" column="strasse" type="java.lang.String" />
<property name="hausnummer" column="hausnummer" type="java.lang.String" />
<property name="plz" column="plz" type="java.lang.String" />
<property name="ort" column="ort" type="java.lang.String" />
<property name="land" column="land" type="java.lang.String" />
<property name="email" column="email" type="java.lang.String" not-null="true" />
<property name="telefon" column="telefon" type="java.lang.String" />
<property name="fax" column="fax" type="java.lang.String" />
<property name="mobilfunk" column="mobilfunk" type="java.lang.String" />
<!-- 0..* Personen sind in genau 1 Firma -->
<many-to-one name="firmen" column="firmen_id" class="Firma" />
<!-- 1 Person ist genau 1 Benutzer -->
<one-to-one name="benutzer" class="Benutzer" />
</class>
FIRMA:
Code:
<hibernate-mapping package="org.hibernate">
<class name="Firma" table="firmen">
<id name="id" column="id" type="java.lang.Integer">
<generator class="sequence">
<param name="sequence">sq_firmen_id</param>
</generator>
</id>
<property name="ansprechpartner" column="ansprechpartner" type="java.lang.String" />
<property name="postfach" column="postfach" type="java.lang.String" />
<property name="strasse" column="strasse" type="java.lang.String" />
<property name="hausnummer" column="hausnummer" type="java.lang.String" />
<property name="plz" column="plz" type="java.lang.String" />
<property name="ort" column="ort" type="java.lang.String" />
<property name="land" column="land" type="java.lang.String" />
<property name="email" column="email" type="java.lang.String" />
<property name="telefon" column="telefon" type="java.lang.String" />
<property name="fax" column="fax" type="java.lang.String" />
<property name="mobilfunk" column="mobilfunk" type="java.lang.String" />
<property name="name" column="name" type="java.lang.String" not-null="true" />
<set name="mitarbeiter" inverse="true" cascade="all-delete-orphan">
<key column="id" />
<!-- In 1 Firma arbeiten 0..* Personen -->
<one-to-many class="Person"/>
</set>
</class>
</hibernate-mapping>
The respective Java-Files with their getters/setters:
Firma.java:Code:
public class Firma
extends AbstractFirma
implements Serializable
{
private static final long serialVersionUID = 3690191036387635252L;
/**
* HashSet von Mitarbeitern in einer Firma
*/
private Set mitarbeiter = new HashSet();
private List firmen = new ArrayList();
String selectedId;
private Session session = null;
public Firma() {
}
public Firma(java.lang.Integer id)
{
super(id);
}
.
.
.
.
}
}
Person.javaCode:
package org.hibernate;
import java.io.Serializable;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
import com.mdpro.protokolldb.SessionUtil;
public class Person
extends AbstractPerson
implements Serializable
{
private static final long serialVersionUID = 4050477937146868537L;
// Mehrzahl firma -> firmen
private Firma firma;
private Session session = null;
public Person()
{
}
public Person(java.lang.Integer id)
{
super(id);
}
public Firma getFirmen() {
return firma;
}
public void setFirmen(Firma firma) {
this.firma = firma;
}
/*
* database-related functions
*/
public boolean addPerson(Person p) {
boolean res = false;
try {
session = SessionUtil.currentSession();
Transaction tx = session.beginTransaction();
session.save(p);
session.flush();
tx.commit();
res = true;
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
if (session != null) session.close();
} catch (HibernateException e) {
e.printStackTrace();
}
}
return res;
}
.
.
.
.
.
Name and version of the database you are using:
Postgres 7
with "MyEclipse-Hibernate"
The generated SQL (show_sql=true):
Hibernate: insert into personen (nachname, titel, vorname, strasse, hausnummer, plz, ort, land, email, telefon, fax, mobilfunk, firmen_id, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Your help is very appreciated.
Georg