-->
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.  [ 8 posts ] 
Author Message
 Post subject: OneToOne mit zusammengesetzem Primärschlüssel
PostPosted: Sat Feb 20, 2010 12:53 pm 
Newbie

Joined: Sat Feb 20, 2010 12:46 pm
Posts: 4
Hallo,

ich habe zwei Objekte:

Auftrag:
- Id
- Verkehrsart
- Ladung
- Von
- Nach
...

Be-/Entladestelle (BES):
- Typ (Beladung oder Entladung)
- Ort (Auftrag: von oder nach)
- Art (Auftrag: Verkehrsart
- Öffnungszeiten

Die Tabelle BES hat einen zusammengesetzen Primärschlüssel, der sich aus Typ, Ort und Art zusammensetzt.

Ich möchte nun eine Java-Entity Auftrag und Ladestelle erstellen. Innerhalb der Auftrags-Entity soll es:
Code:
private Ladestelle beladestelle
private Ladestelle entladestelle

geben.

In SQL kann ich das über zwei JOINS machen, wir realisiere ich das mit Hibernate?

Danke und viele Grüße

Marco


Top
 Profile  
 
 Post subject: Re: OneToOne mit zusammengesetzem Primärschlüssel
PostPosted: Mon Feb 22, 2010 9:08 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Ich and Deiner Stelle wuerde die beiden Relationen als unidirectionale ManyToOne Assoziationen modellieren,
mit der Besonderheit dass die Klasse Ladestelle einen zusammengesetzten Schluessel hat:

Code:
@Entity
public class Auftrag {
...
@javax.persistence.ManyToOne
private Ladestelle beladestelle;

@javax.persistence.ManyToOne
private Ladestelle entladestelle;


Code:
@Entity
public class Ladestelle {
    @Embeddable
    public class LadestelleId implements Serializable {
        @Column
        String typ;
       
        @Column
        String ort;

        @Column
        String art;
    }
   
      @Id
      Ladestelle key;

       @Column
        String oeffnungszeiten;
}


Top
 Profile  
 
 Post subject: Re: OneToOne mit zusammengesetzem Primärschlüssel
PostPosted: Mon Feb 22, 2010 11:27 am 
Newbie

Joined: Sat Feb 20, 2010 12:46 pm
Posts: 4
Hallo,

wenn ich alles so mappe, wie du beschrieben hast, bekomme ich folgende Exception:

Code:
Caused by: org.hibernate.AnnotationException: Column name LKZ of JobEndPoint not found in JoinColumns.referencedColumnName
        at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:319)
        at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:89)
        at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:499)
        at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:304)
        at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115)
        at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1225)
        at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:159)
        at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:854)
        at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:191)
        at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:253)
        ... 5 more
Java Result: 1


Der String "Ort" wird bei mir aus "LKZ" und "PLZ" zusammengesetzt. Ich habe alles auf Schreibfehler geprüft - es gibt diese Spalte. Ich verstehe die Fehlermeldung nicht...

Danke schonmal für deine Antwort und ich hoffe, dass du noch eine Idee hast.

Viele Grüße

Marco


Top
 Profile  
 
 Post subject: Re: OneToOne mit zusammengesetzem Primärschlüssel
PostPosted: Mon Feb 22, 2010 11:32 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Mein Fehler, anstelle von
Code:
     @Id
      Ladestelle key;


muss natuerlich
Code:
     @Id
      LadestelleId key;


Top
 Profile  
 
 Post subject: Re: OneToOne mit zusammengesetzem Primärschlüssel
PostPosted: Mon Feb 22, 2010 11:54 am 
Newbie

Joined: Sat Feb 20, 2010 12:46 pm
Posts: 4
Hatte ich schon übernommen. Immer noch die Exception... :/


Top
 Profile  
 
 Post subject: Re: OneToOne mit zusammengesetzem Primärschlüssel
PostPosted: Mon Feb 22, 2010 12:11 pm 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Schick mal Dein Mapping, (4 Augen sehen mehr als 2 ;-))


Top
 Profile  
 
 Post subject: Re: OneToOne mit zusammengesetzem Primärschlüssel
PostPosted: Mon Feb 22, 2010 1:10 pm 
Newbie

Joined: Sat Feb 20, 2010 12:46 pm
Posts: 4
Code:
@Entity
@Table(name = "BES", catalog = "...", schema = "dbo")
public class JobEndPoint implements Serializable {

    private static final long serialVersionUID = 1017986852824783744L;

    @Id
    protected JobEndPointPK jobEndPointPK;

    @Column(name = "`Stoppzeit`")
    int stopTime;

    @Column(name = "`WA-Zeit von`")
    @Temporal(TemporalType.TIME)
    Date acceptanceTimeFrom;

    @Column(name = "`WA-Zeit bis`")
    @Temporal(TemporalType.TIME)
    Date acceptanceTimeTo;

    @Column(name = "`WA-Zeit2 von`")
    @Temporal(TemporalType.TIME)
    Date acceptanceTime2From;

    @Column(name = "`WA-Zeit2 bis`")
    @Temporal(TemporalType.TIME)
    Date acceptanceTime2To;

    @Column(name = "`Kdspez Puffer1 (0:00 Uhr bis 06:30 Uhr)`")
    int buffer1;

    @Column(name = "`Kdspez Puffer2 (6:30 Uhr bis 13:00 Uhr)`")
    int buffer2;

    @Column(name = "`Kdspez Puffer3 (13:00 Uhr bis 24:00 Uhr)`")
    int buffer3;
//...
}

@Embeddable
public class JobEndPointPK implements Serializable {

    @Basic(optional = false)
    @Column(name = "`Ladetyp`", nullable = false, length = 50)
    @Enumerated
    EndPointType type;

    @Basic(optional = false)
    @Column(name = "`KundenNr`", nullable = false)
    int customerId;

    @Basic(optional = false)
    @Column(name = "`VerkArt`", nullable = false, length = 10)
    @Enumerated
    JobType jobType;

    @Basic(optional = false)
    @Column(name = "`LKZ`", nullable = false, length = 3)
    String countryCode;
   
    @Basic(optional = false)
    @Column(name = "`PLZ`", nullable = false, length = 7)
    String zipCode;
//...
}

@Entity
@Table(name = "Auftragsdaten", catalog = "...", schema = "dbo")
public class Job implements Comparable<Object>, Serializable {

   private static final long serialVersionUID = 4285871251915951149L;

    @Id
    @Basic(optional = false)
    @Column(name = "`id`", nullable = false)
    int id;

    @Column(name = "`AufNr`", nullable=false)
   int jobId;

    @Transient
   List<Integer> jobsAdded;

    @Column(name = "`Beladedatum`", nullable=false)
    @Temporal(TemporalType.DATE)
   Date loadDate;

    @Column(name = "`Beladezeit`")
    @Temporal(TemporalType.TIME)
   Date loadTimeFrom;

    @Transient
   Date loadTimeTo;

    @Column(name = "`Entladedatum`", nullable=false)
    @Temporal(TemporalType.DATE)
   Date unloadDate;

    @Column(name = "`Entladezeit Beginn`")
    @Temporal(TemporalType.TIME)
   Date unloadTimeFrom;

    @Column(name = "`Entladezeit Ende`")
    @Temporal(TemporalType.TIME)
   Date unloadTimeTo;

    @Transient
   List<JobEndPoint> froms;

    @ManyToOne
    @JoinColumns ({
        @JoinColumn(name="`AbsNr`", referencedColumnName = "`KundenNr`", insertable = false, updatable = false),
        @JoinColumn(name="`Verkehrsart`", referencedColumnName = "`VerkArt`", insertable = false, updatable = false),
        @JoinColumn(name="`von LKZ`", referencedColumnName = "`LKZ`", insertable = false, updatable = false),
        @JoinColumn(name="`von PLZ`", referencedColumnName = "`PLZ`", insertable = false, updatable = false),
        @JoinColumn(name="`Beladetyp`", referencedColumnName = "`Ladetyp`", insertable = false, updatable = false)
    })
    JobEndPoint fromPoint;

    @Transient
    JobEndPoint toPoint;

    @Column(name = "`Verkehrsart`", length = 10, nullable=false)
    @Enumerated
   JobType type;

    @Column(name = "`Anzahl Paletten CCG1`")
   int numberCCG1;

    @Column(name = "`Anzahl Paletten CCG2`")
    int numberCCG2;

    @Transient
   int numberFullContainer;

    @Transient
    int numberEmptyContainer;

    @Column(name = "`Anzahl Container`")
    int numberContainer;

    @Column(name = "`Anz Stellplätze`")
   int numberUnits;

    @Column(name = "`Bruttogewicht`", nullable=false)
   int loadWeight;

    @ManyToOne
    @JoinColumn(name="`Kühlkennzeichen`")
   CoolingCode coolingCode;

    @Transient
   boolean obligation;

    @Transient
   float distance;

    @Transient
   double loadFactor;

//...
}


Danke noch mal. Was genau mache ich falsch?

Hier ist die Exception:
Code:
Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: TouroptimizerFrameworkPU] Unable to configure EntityManagerFactory
        at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:265)
        at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:125)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:51)
        at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:33)
        at JobJpaController.<init>(JobJpaController.java:24)
        at Tester.main(Tester.java:6)
Caused by: org.hibernate.AnnotationException: Column name LKZ of JobEndPoint not found in JoinColumns.referencedColumnName
        at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:319)
        at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:89)
        at org.hibernate.cfg.AnnotationConfiguration.processFkSecondPassInOrder(AnnotationConfiguration.java:499)
        at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:304)
        at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115)
        at org.hibernate.ejb.Ejb3Configuration.buildMappings(Ejb3Configuration.java:1225)
        at org.hibernate.ejb.EventListenerConfigurator.configure(EventListenerConfigurator.java:159)
        at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:854)
        at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:191)
        at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:253)
        ... 5 more
Java Result: 1


Top
 Profile  
 
 Post subject: Re: OneToOne mit zusammengesetzem Primärschlüssel
PostPosted: Wed Feb 24, 2010 7:20 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Du machst Dir das Leben schwer indem Du saemtliche Column-namen um-mappts und zwar auf sehr sonderlicher Weise.

Quote:
@Column(name = "`Anz Stellplätze`")


Du willst also tatsaechlich, dass im Schema auf der Datenbank das Feld
'Anz Stellplätze'
heisst?
Das ist jenseits von Gut und Boese und widerspricht jeglicher gaengigen Namenskonvention.
Damit wirst Du auch Probleme beim Erstellen und Ausfuehren von Queries hinsteuern.
Ich empfehle:
1. Keine Leerzeichen und keine Umlaute in Feldnamen
2. Niemals doppelte Quotierung verwenden ( verschachtelete Anfuehrungszeichen ) !
3. Verwendung des name-attributes in der @Column Annotations weitestgehend vermeiden.


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