-->
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.  [ 1 post ] 
Author Message
 Post subject: MySQL InnoDB und ForeignKey One-to-one Mapping Problem
PostPosted: Tue Jun 16, 2009 9:09 am 
Newbie

Joined: Tue Jun 16, 2009 8:43 am
Posts: 1
Hallo,

Leider konnte weder das HibernateManual, noch google, noch sonst jemand mit meinem Problem helfen.
Selbst der 1zu1 Nachbau aus dem Tutorial hat nicht funktioniert.

Meine Datenbank besteht aus mehreren Tabellen in der kinder den Primärschlüssel durch id
darstellt. Alle anderen Tabellen haben id als Fremdschlüssel, auch id genannt.

Meine Mappingdatei (mit JBoss Hibnerate Tools erstellt) sieht so aus:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
                                   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="db.kinder">
<class name="Kinder" table="kinder">
  <id column="id" name="id">
   <generator class="native"/>
  </id>
  <property column="vorname" generated="never" lazy="false" name="vorname"/>
  <property column="nachname" generated="never" lazy="false" name="nachname"/>
  <property column="gruppe" generated="never" lazy="false" name="gruppe"
   not-null="true" type="integer"/>
  <property column="klasse" generated="never" lazy="false" name="klasse"/>
  <property column="schule" generated="never" lazy="false" name="schule"/>
  <one-to-one cascade="all" class="Heimweg" constrained="true" name="heimweg"/>
  <one-to-one cascade="all" class="Hgz" constrained="true" name="hgz"/>
  <one-to-one cascade="all" class="Sonstiges" constrained="true" name="sonstiges"/>
  <one-to-one cascade="all" class="Ue" constrained="true" name="ue"/>
</class>
<class name="Heimweg" table="kinder_heimweg">
  <id column="id" name="id">
   <generator class="foreign">
    <param name="property">kinder</param>
   </generator>
  </id>
  <property column="Mon" generated="never" lazy="false" name="Mon" type="boolean"/>
  <property column="Tue" generated="never" lazy="false" name="Tue" type="boolean"/>
  <property column="Wed" generated="never" lazy="false" name="Wed" type="boolean"/>
  <property column="Thu" generated="never" lazy="false" name="Thu" type="boolean"/>
  <property column="Fri" generated="never" lazy="false" name="Fri" type="boolean"/>
  <one-to-one cascade="all" class="Kinder" name="kinder"/>
</class>
<class name="Hgz" table="kinder_hgz">
  <id column="id" name="id">
   <generator class="foreign">
    <param name="property">kinder</param>
   </generator>
  </id>
  <property column="Mon" generated="never" lazy="false" name="mon" type="time"/>
  <property column="Tue" generated="never" lazy="false" name="tue" type="time"/>
  <property column="Wed" generated="never" lazy="false" name="wed" type="time"/>
  <property column="Thu" generated="never" lazy="false" name="thu" type="time"/>
  <property column="Fri" generated="never" lazy="false" name="fri" type="time"/>
  <one-to-one cascade="all" class="Kinder" name="kinder"/>
</class>
<class name="Sonstiges" table="kinder_sonstiges">
  <id column="id" name="id">
   <generator class="foreign">
    <param name="property">kinder</param>
   </generator>
  </id>
  <property column="allergien" generated="never" lazy="false" name="allergien"/>
  <property column="abholberechtigte" generated="never" lazy="false" name="abholberechtigte"/>
  <property column="baecker" generated="never" lazy="false"
   name="baecker" type="boolean"/>
  <property column="buecherei" generated="never" lazy="false"
   name="buecherei" type="boolean"/>
  <property column="fleisch" generated="never" lazy="false"
   name="fleisch" type="boolean"/>
  <property column="kommentare" generated="never" lazy="false" name="kommentare"/>
  <one-to-one cascade="all" class="Kinder" name="kinder"/>
</class>
<class name="Ue" table="kinder_ue">
  <id column="id" name="id">
   <generator class="foreign">
    <param name="property">kinder</param>
   </generator>
  </id>
  <property column="ue_mo" generated="never" lazy="false" name="mon" type="time"/>
  <property column="ue_di" generated="never" lazy="false" name="tue" type="time"/>
  <property column="ue_mi" generated="never" lazy="false" name="wed" type="time"/>
  <property column="ue_do" generated="never" lazy="false" name="thu" type="time"/>
  <property column="ue_fr" generated="never" lazy="false" name="fri" type="time"/>
  <one-to-one cascade="all" class="Kinder" name="kinder"/>
</class>
</hibernate-mapping>


Als Beispiel nur die Klasse Kinder und Ue

Code:
package db.kinder;

import javax.persistence.*;

/**
*
* @author muki Diese Klasse repräsentiert die Tabelle hort.kinder
*
*/
@Entity
@Table(name = "kinder")
public class Kinder {

   @Id
   @GeneratedValue
   @Column(name = "id")
   private Integer id;

   @Column(name = "vorname")
   private String vorname;

   @Column(name = "nachname")
   private String nachname;

   @Column(name = "gruppe")
   private Integer gruppe;

   @Column(name = "klasse")
   private String klasse;

   @Column(name = "schule")
   private String schule;

   private Heimweg heimweg;
   private Hgz hgz;
   private Nachmittag nachmittag;
   private Sonstiges sonstiges;
   private Ue ue;

   public Kinder() {
   }

   public Kinder(Integer id, String vorname, String nachname, Integer gruppe,
         String klasse, String schule, Heimweg heimweg, Hgz hgz,
         Nachmittag nachmittag, Sonstiges sonstiges, Ue ue) {
      this.id = id;
      this.vorname = vorname;
      this.nachname = nachname;
      this.gruppe = gruppe;
      this.klasse = klasse;
      this.schule = schule;
      this.heimweg = heimweg;
      this.hgz = hgz;
      this.nachmittag = nachmittag;
      this.sonstiges = sonstiges;
      this.ue = ue;

   }

   public Integer getId() {
      return id;
   }

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

   public String getVorname() {
      return vorname;
   }

   public void setVorname(String vorname) {
      this.vorname = vorname;
   }

   public String getNachname() {
      return nachname;
   }

   public void setNachname(String nachname) {
      this.nachname = nachname;
   }

   public Integer getGruppe() {
      return gruppe;
   }

   public void setGruppe(Integer gruppe) {
      this.gruppe = gruppe;
   }

   public String getKlasse() {
      return klasse;
   }

   public void setKlasse(String klasse) {
      this.klasse = klasse;
   }

   public String getSchule() {
      return schule;
   }

   public void setSchule(String schule) {
      this.schule = schule;
   }

   public Heimweg getHeimweg() {
      return heimweg;
   }

   public void setHeimweg(Heimweg heimweg) {
      this.heimweg = heimweg;
   }

   public Hgz getHgz() {
      return hgz;
   }

   public void setHgz(Hgz hgz) {
      this.hgz = hgz;
   }

   public Nachmittag getNachmittag() {
      return nachmittag;
   }

   public void setNachmittag(Nachmittag nachmittag) {
      this.nachmittag = nachmittag;
   }

   public Sonstiges getSonstiges() {
      return sonstiges;
   }

   public void setSonstiges(Sonstiges sonstiges) {
      this.sonstiges = sonstiges;
   }

   public Ue getUe() {
      return ue;
   }

   public void setUe(Ue ue) {
      this.ue = ue;
   }
}


Code:
package db.kinder;

import java.util.Date;
import javax.persistence.*;

@Entity
@Table(name = "kinder_ue")
public class Ue {
   
   public Ue() {}
   
   public Ue(Integer id,
           Date mon,
           Date tue,
           Date wed,
           Date thu,
           Date fri,
           Kinder kinder)
   {
      this.id = id;
      this.mon = mon;
      this.tue = tue;
      this.wed = wed;
      this.thu = thu;
      this.fri = fri;
      this.kinder = kinder;
   }
   
   @Id @GeneratedValue
   @Column(name = "id")
   private Integer id;
   
   @Column(name = "ue_mo")
   private Date mon;
   
   @Column(name = "ue_di")
   private Date tue;
   
   @Column(name = "ue_mi")
   private Date wed;
   
   @Column(name = "ue_do")
   private Date thu;
   
   @Column(name = "ue_fr")
   private Date fri;
   
   private Kinder kinder;

   public Integer getId() {
      return id;
   }

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

   public Date getMon() {
      return mon;
   }

   public void setMon(Date mon) {
      this.mon = mon;
   }

   public Date getTue() {
      return tue;
   }

   public void setTue(Date tue) {
      this.tue = tue;
   }

   public Date getWed() {
      return wed;
   }

   public void setWed(Date wed) {
      this.wed = wed;
   }

   public Date getThu() {
      return thu;
   }

   public void setThu(Date thu) {
      this.thu = thu;
   }

   public Date getFri() {
      return fri;
   }

   public void setFri(Date fri) {
      this.fri = fri;
   }

   public Kinder getKinder() {
      return kinder;
   }

   public void setKinder(Kinder kinder) {
      this.kinder = kinder;
   }

   
}


Die Anntotations sind im Moment noch funktionslos.

Meine Klasse die die Werte in die Datenbank schreiben soll, sieht ungefähr so aus
(auf das essentielle reduziert):

Code:
            aKind       = new Kinder();
            sonstiges    = new Sonstiges();
            ue          = new Ue();
            hgz       = new Hgz();
            heimweg      = new Heimweg();

/** setzen der Werte, funktioniert auch **/

         aKind.setSonstiges(sonstiges);
         aKind.setUe(ue);
         aKind.setHgz(hgz);
         aKind.setHeimweg(heimweg);
session.save(aKind);



Wenn der code ausgeführt wird, erscheint folgender Fehler

Code:
Exception in thread "AWT-EventQueue-0" org.hibernate.id.IdentifierGenerationException: attempted to assign id from null one-to-one property: kinder
   at org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:68)


Als Datenbank benutze ich MySQL5 mit InnoDB als engine.
Als Beispiel hier wieder kinder und ue

Code:
--
-- Tabellenstruktur für Tabelle `kinder`
--

CREATE TABLE IF NOT EXISTS `kinder` (
  `id` int(11) NOT NULL auto_increment,
  `vorname` varchar(20) NOT NULL default '',
  `nachname` varchar(40) NOT NULL default '',
  `gruppe` int(2) NOT NULL default '0',
  `klasse` char(2) default NULL,
  `schule` varchar(6) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=INNODB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

--
-- Daten für Tabelle `kinder`
--

--
-- Tabellenstruktur für Tabelle `kinder_ue`
--

CREATE TABLE IF NOT EXISTS `kinder_ue` (
  `id` int(11),
  `ue_mo` time default '00:00:00',
  `ue_di` time default '00:00:00',
  `ue_mi` time default '00:00:00',
  `ue_do` time default '00:00:00',
  `ue_fr` time default '00:00:00',
  UNIQUE(id),
  FOREIGN KEY (id) REFERENCES kinder(id)
  ON DELETE CASCADE
) ENGINE=INNODB DEFAULT CHARSET=latin1;

--
-- Daten für Tabelle `kinder_ue`
--


Ich hab davor MySQL 4 und MyISAM als engine benutzt. Selbes Problem. Ich habe versucht,
der Klasse kinder als Primärschlüssel assigned, die id statisch zuzuweisen, aber die
one-to-one Objekte übernehmen sie nicht.

Wo liegt der große Fehler.

Viele Dank, wenn ihr mir weiterhelfen könnt,
Muki


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.