Hello,
I've a mapping-problem. I hope, someone can help me out.
I've the three classes "Kunde", "Projekt" and "Job".
The associations between these classes look like that:
Kunde 1---n Projekt
Projekt 0---n Job
Kunde 1---n Job
A Job can belong to a Projekt or not, but must belong to a Kunde.
My problem is:
I want to save a Job, who belongs not to an Projekt. So the value in column "projekt-id" is set to 0.
As soon as the accordant data record is saved, nothing works any more. For example I can't show a list of Kunden.
Error:
org.apache.jasper.JasperException: net.sf.hibernate.UnresolvableObjectException:
No row with the given identifier exists: 0, of class: pmtool.entities.Projekt
Yes, and it's true, no data record with id 0 exists in table projekt.
I will give you my mappings, I hope it makes my problem clearer to you.
Kunde.hbm.xml:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="pmtool.entities.Kunde" table="kunde">
<id name="id" type="integer" unsaved-value="null">
<column name="kunde_id" not-null="true"/>
<generator class="identity"/>
</id>
<property name="name">
<column name="name" sql-type="varchar(32)" not-null="true"/>
</property>
<bag name="jobs" cascade="all" inverse="true" lazy="false" order-by="eingabedatum">
<key column="kunde_id"/>
<one-to-many class="pmtool.entities.Job"/>
</bag>
<bag name="projekte" cascade="all" inverse="true" lazy="false" order-by="name">
<key column="kunde_id"/>
<one-to-many class="pmtool.entities.Projekt"/>
</bag>
<bag name="ansprechpartner" cascade="all" inverse="true" lazy="false" order-by="nachname">
<key column="kunde_id"/>
<one-to-many class="pmtool.entities.Ansprechpartner"/>
</bag>
</class>
Projekt.hbm.xml:Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="pmtool.entities.Projekt" table="projekt">
<id name="id" type="integer">
<column name="projekt_id" not-null="true"/>
<generator class="identity"/>
</id>
<property name="name">
<column name="name" sql-type="varchar(32)" not-null="true"/>
</property>
<bag name="jobs" cascade="all" inverse="true" lazy="false" order-by="eingabedatum">
<key column="projekt_id"/>
<one-to-many class="pmtool.entities.Job"/>
</bag>
<many-to-one name="kunde" column="kunde_id" not-null="true"/>
</class>
</hibernate-mapping>
Job.hbm.xml:Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="pmtool.entities.Job" table="job">
<id name="jobid" type="integer" unsaved-value="null">
<column name="job_id" not-null="true"/>
<generator class="identity"/>
</id>
<property name="jobname">
<column name="jobname" sql-type="varchar(32)" not-null="true"/>
</property>
<property name="jobart">
<column name="jobart" not-null="false"/>
</property>
<property name="status">
<column name="status" not-null="false"/>
</property>
<property name="deadlineTag">
<column name="deadlineTag" sql-type="varchar(10)" not-null="false"/>
</property>
<property name="deadlineStunde">
<column name="deadlineStunde" sql-type="varchar(5)" not-null="false"/>
</property>
<property name="eingabedatum">
<column name="eingabedatum" sql-type="varchar(10)" not-null="false"/>
</property>
<property name="von">
<column name="von" sql-type="varchar(5)" not-null="false"/>
</property>
<property name="bis">
<column name="bis" sql-type="varchar(5)" not-null="false"/>
</property>
<property name="arbeiten">
<column name="arbeiten" not-null="false"/>
</property>
<property name="auftragsdatum">
<column name="auftragsdatum" sql-type="varchar(32)" not-null="false"/>
</property>
<property name="istzeit">
<column name="istzeit" not-null="false"/>
</property>
<property name="richtwert">
<column name="richtwert" not-null="false"/>
</property>
<property name="datum">
<column name="datum" sql-type="varchar(10)" not-null="false"/>
</property>
<property name="beschreibung">
<column name="beschreibung" not-null="false"/>
</property>
<property name="auftragsart">
<column name="auftragsart" not-null="false"/>
</property>
<property name="ansprechpartner">
<column name="ansprechpartner" not-null="false"/>
</property>
<many-to-one name="kunde" column="kunde_id" not-null="true" class="pmtool.entities.Kunde"/>
<many-to-one name="projekt" column="projekt_id" not-null="false" class="pmtool.entities.Projekt"/>
</class>
</hibernate-mapping>
In the weblog-application-example in the Hibernate-doku (
http://www.hibernate.org/hib_docs/reference/en/html/example-weblog.html#example-weblog-mappings) I've seen the element
"not-null" for <many-to-one>, although I's not mentioned in chapter 5.1.10 (
http://www.hibernate.org/hib_docs/reference/en/html/mapping.html#mapping-declaration-manytoone). I use this element.
Then I thought: "Perhaps the element "not-null" can used in <one-to-many>, too." But unfortunately it doesn't work.
What do I have to change in my mappings, that Jobs which not belong to a Projekt can be created and everything works normal afterwards?
I hope, someone can help me. If you don't understand what I mean, please tell me what further information you need an what I've to explain once more.
Thank you!
paula