-->
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.  [ 6 posts ] 
Author Message
 Post subject: mapping-problem, null-values
PostPosted: Mon May 17, 2004 8:14 am 
Newbie

Joined: Thu Apr 08, 2004 7:32 am
Posts: 10
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


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 18, 2004 3:10 am 
Newbie

Joined: Thu Apr 08, 2004 7:32 am
Posts: 10
Is there really none who can help me? I don't think so.
Even a short answer would help me very much!


Top
 Profile  
 
 Post subject:
PostPosted: Tue May 18, 2004 9:57 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
0 is a valid FK, you must use Null

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2004 3:17 am 
Newbie

Joined: Thu Apr 08, 2004 7:32 am
Posts: 10
Hello Emmanuel,

thank you for your answer. Unfortunately I don't understand what you mean. Sorry!
Where do I have to use Null? Which part in my mapping do I have to change?
Thank you!

paula


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2004 3:30 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
You can't map that, "no association" has to be represented as "null" in the database, not 0


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 19, 2004 4:12 am 
Newbie

Joined: Thu Apr 08, 2004 7:32 am
Posts: 10
It work's!!!
Thank you very much, Emmanuel and Michael!

paula


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