-->
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: Hibernate Annotations: Probleme beim Composite PK/FK Mapping
PostPosted: Thu Aug 24, 2006 11:40 am 
Newbie

Joined: Thu Aug 17, 2006 4:40 am
Posts: 6
Hallo zusammen,
ich habe Probleme mit dem Mapping von Composite PK/FK. Ich habe zwei Tabellen mit jeweils zusammengesetzten Primärschlüsseln. Der zweite Primärschlüssel beinhaltet einen Teil des Fremdschlüssels aus Tabelle 1. Die Primary Key Objekte sind mittels @EmbeddedId Annotation definiert. Wenn ich jetzt versuche meine Applikation zu deployen bekomme ich immer folgende Fehlermeldung:
org.hibernate.MappingException: Repeated column in mapping for entity: xx.xx.xx.xx.Reservation column: DATUM (should be mapped with insert="false" update="false")
Ich verwende JBoss 4.04 inklusive Hibernate als Application-Server sowie MySQL 5.0.22 als Datenbank.
Vielen Dank im voraus.
Dirk

Anbei die Sourcen:

Tabelle 1 : Cruise.java
Tabelle 2 : Reservation.java
Composite Key Implementation: CruisePK.java, ReservationPK.java

Cruise.java
Code:
@Entity
@Table(name = "CRUISE")
public class Cruise
{
   private int _id;
   private String _name = "";
   private String _owner = "";
   private String _date = "";
   private int _shipId;
   private Collection<Reservation> _reservations = new ArrayList<Reservation>();
   private CruisePK _pk;

   @EmbeddedId
   @AttributeOverrides({
      @AttributeOverride(name="id", column=@Column(name="ID")),
      @AttributeOverride(name="date", column=@Column(name="DATUM"))
   })

   public CruisePK getPk() { return _pk; }
   public void setPk( CruisePK pk ) { _pk = pk; }

   @Column(name = "NAME")
   public String getName()
   {
      return _name;
   }

   public void setName(String name)
   {
      _name = name;
   }

   @Column(name="OWNER")
   public String getOwner() {
      return _owner;
   }

   public void setOwner(String owner){
      _owner = owner;
   }

   @Column(name = "SHIPID")
   public int getShipId()
   {
      return _shipId;
   }

   public void setShipId(int id)
   {
      _shipId = id;
   }

   @OneToMany(mappedBy = "cruise", cascade={ CascadeType.PERSIST, CascadeType.MERGE })
   public Collection<Reservation> getReservations()
   {
      return _reservations;
   }

   public void setReservations(Collection<Reservation> reservations)
   {
      _reservations = reservations;
   }
}


Reservation.java
Code:
@Entity
@Table(name = "RESERVATION")
public class Reservation
{
   private int _id;
   private String _date = "";
   private String _amount = "";
   private Cruise cruise;
   private ReservationPK _pk;

   public Reservation(){};
   public Reservation(Cruise cr) {cruise = cr;}

   @EmbeddedId
   @AttributeOverrides({
      @AttributeOverride(name="id", column=@Column(name="ID")),
      @AttributeOverride(name="date", column=@Column(name="DATUM"))
   })

   public ReservationPK getPk() { return _pk; }
   public void setPk( ReservationPK pk ) { _pk = pk; }

   @Column(name = "AMOUNT")
   public String getAmount()
   {
      return _amount;
   }

   public void setAmount(String amount)
   {
      _amount = amount;
   }

   @ManyToOne
   @JoinColumns({
      @JoinColumn(name = "CRUISE_ID", referencedColumnName="ID"),
      @JoinColumn(name = "DATUM", referencedColumnName="DATUM")
      })
   public Cruise getCruise()
   {
      return cruise;
   }

   public void setCruise(Cruise cr)
   {
      cruise = cr;
   }
}


CruisePK.java
Code:
@Embeddable
public class CruisePK implements Serializable {

   private String _date = "";
   private int _id;
   
   public CruisePK() {};
   
   public CruisePK(String date, int id){
      _date = date;
      _id = id;
   }

   @Column(name="DATUM")
   public String getDate() { return _date; }
   public void setDate(String date) { _date = date; }
   
   @Column(name="ID")
   public int getId() { return _id; }
   public void setId(int id) { _id = id; }
   
   
   public boolean equals(Object obj){
      if (obj == this) {
         return true;
      }
      if ( !( obj instanceof CruisePK ) ) {
         return false;
      }
      CruisePK pk = (CruisePK)obj;
      if (!_date.equals(pk.getDate())) {
         return true;
      }
      if (_id != pk.getId()) {
         return false;
      }
      return true;
   }
   
   public int hashCode() {
      if ( _date == null ) {
         return 0;
      }
      return (_date.hashCode() * 29) + _id * 31;
   }
   
}


ReservationPK.java
Code:
@Embeddable
public class ReservationPK implements Serializable {
   
   private String _date = "";
   private int _id;
   
   public ReservationPK() {};
   
   public ReservationPK(String date, int id){
      _date = date;
      _id = id;
   }

   @Column(name="DATUM", updatable=false, insertable=false)
   public String getDate() { return _date; }
   public void setDate(String date) { _date = date; }
   
   @Column(name="ID")
   public int getId() { return _id; }
   public void setId(int id) { _id = id; }
   
   
   public boolean equals(Object obj){
      if (obj == this) {
         return true;
      }
      if ( !( obj instanceof CruisePK ) ) {
         return false;
      }
      CruisePK pk = (CruisePK)obj;
      if (!_date.equals(pk.getDate())) {
         return true;
      }
      if (_id != pk.getId()) {
         return false;
      }
      return true;
   }
   
   public int hashCode() {
      if ( _date == null ) {
         return 0;
      }
      return (_date.hashCode() * 30) + _id * 32;
   }

}


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.