-->
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.  [ 4 posts ] 
Author Message
 Post subject: one-to-many / bidirectional: "Parent" id is not se
PostPosted: Fri May 20, 2005 9:27 am 
Newbie

Joined: Tue May 17, 2005 4:54 am
Posts: 15
Location: Germany, Karlsruhe
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.java

Code:
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


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 20, 2005 12:36 pm 
Newbie

Joined: Thu Feb 17, 2005 3:01 pm
Posts: 9
In a bi-directional mapping situtation you have to explicitly add the reference of the parent on the child .

We had a addchild method on the parent

public void addCalcDetails(ConCalcDetail conCalcDetail)
{
if (conCalcDetail != null)
{
if (this.calcDetails == null)
{
this.setCalcDetails(new HashSet());
}
conCalcDetail.setContract(this);
this.getCalcDetails().add(conCalcDetail);

}
}

Hope this helps


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 20, 2005 2:45 pm 
Newbie

Joined: Tue May 17, 2005 4:54 am
Posts: 15
Location: Germany, Karlsruhe
Hi murparth,

thanks for your reply!

However I'm not quite sure about some details:


I currently do the following when add a new child:

session = SessionUtil.currentSession();
Transaction tx = session.beginTransaction();
session.save(p);
session.flush();
tx.commit();


You suggest to add an addChild()-method in the parent class (im my case it would be addPerson() ).
So actually I would have to call the parent class to add a new child:

...

Parent p = new Parent();
p.addChild( c );



--> Is is then still sufficient to make a call like this:
session.save(p); ??
Or do I have to do session.save(c) ??

I'm quite confused now...

If you (and of course everybody else, too :-)) could come up with some more details I'd be rather glad. Maybe I've overseen some important fact.

Best regards
Georg


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 20, 2005 4:34 pm 
Newbie

Joined: Fri May 20, 2005 4:16 pm
Posts: 13
GeorgGentoo wrote:
--> Is is then still sufficient to make a call like this:
session.save(p); ??
Or do I have to do session.save(c) ??


This depends on whether you've enabled cascading.


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