-->
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.  [ 2 posts ] 
Author Message
 Post subject: Refferenz-ID wird nicht ausgefüllt (NULL)
PostPosted: Wed Aug 17, 2005 2:25 am 
Newbie

Joined: Fri Aug 12, 2005 7:20 am
Posts: 3
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3

Mapping documents: DataItem, PhoneItem

Hallo zusammen,

ich arbeite mir grade in Hibernate ein und ersuche eine kleine "Adressverwaltung" zu schreiben.

Auszug aus der DataItem.hbm.xml:
Code:
<hibernate-mapping>
    <class name="DataItem" table="DATAITEMS">
        <id name="id" column="uId">
            <generator class="increment"/>
        </id>
        <property name="hName"/>
        <property name="name"/>
        <property name="street"/>
        <property name="zip"/>
        <property name="location"/>
        <property name="prePhone"/>
        <property name="phone"/>
        <property name="preMobile"/>
        <property name="mobile"/>
   <set name="phoneNumbers" cascade="all" inverse="true" lazy="false">
         <key column="uId"/>
      <one-to-many class="PhoneItem"/>
   </set>
    </class>
</hibernate-mapping>


Auszug aus der PhoneItem.hbm.xml:
Code:
<hibernate-mapping>

    <class name="PhoneItem" table="PHONEITEMS">
        <id name="id" column="pId">
            <generator class="increment"/>
   </id>
   <many-to-one name="items" class="DataItem" column="uId"/>
        <property name="number"/>
    </class>

</hibernate-mapping>



DataItem.class:
Code:
public class DataItem {
   private long id;
   private String name, hName, street, zip, location, prePhone, phone, preMobile, mobile, email;
   private Set   phoneNumbers;

   public Set getPhoneNumbers() {
      return phoneNumbers;
   }
   
   public void setPhoneNumbers(Set phoneNumbers) {
      this.phoneNumbers = phoneNumbers;
   }
   DataItem() { }

   
    public long getId() {
      return id;
   }

   public void setId(long id) {
      this.id = id;
   }
.
.
.


Auszug aus der PhoneItem.class
Code:
public class PhoneItem {
   private int      id      = 0;
   private String    number    = "";
   private DataItem   items;

   public DataItem getItems() {
      return items;
   }
   
   public void setItems(DataItem items) {
      this.items = items;
   }
   
   public int getId() {
      return id;
   }

   public void setId(int id) {
      this.id = id;
   }

   public String getNumber() {
      return number;
   }

   public void setNumber(String number) {
      this.number = number;
   }
   
   
   
}


So lege ich z.B. neue Objekte an:
Code:
      DataItem data = new DataItem();
      .
      .
      .
        Session s = factory.openSession();
      try {
            s.saveOrUpdate(data);
            s.flush();
            s.connection().commit();
        }
        catch(HibernateException he) {
            System.out.println(he);
        }
        catch(SQLException he) {
           System.out.println(he);
        }
        finally {
            s.close();
        }
        instanceHolder.add(data);
       
        Set items = new HashSet();
      for(int i=0; i<tempInstanceHolder.size(); i++) {
         PhoneItem item = new PhoneItem();
         item.setNumber(tempInstanceHolder.get(i).getNumber());
         items.add(item);
      }
      tempInstanceHolder.clear();
      s = factory.openSession();
      try {
         DataItem dataObject = instanceHolder.get(instanceHolder.size()-1);
         dataObject.setPhoneNumbers(items);
         s.saveOrUpdate(dataObject);
            s.flush();
            s.connection().commit();
        }
        catch(HibernateException he) {
            System.out.println(he);
        }
        catch(SQLException he) {
           System.out.println(he);
        }
        finally {
            s.close();
        }


Es finden sich letztendlich auch ALLE Objekte in der Datenbank wieder, jedoch wird das Feld "uID" von PhoneItems NICHT berücksichtigs (NULL) und daher kann er sie nicht zurück refferenzieren...

Was mache ich falsch bzw. was kann ich machen?

Danke im Voraus!!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 23, 2005 8:45 am 
Newbie

Joined: Mon May 30, 2005 6:27 am
Posts: 6
Lass die IDs doch einfach von der Datenbank generieren und ändere den Generator folgender Maßen:

<generator class="identity"/>


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