-->
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.  [ 3 posts ] 
Author Message
 Post subject: composite PK/FK mapping
PostPosted: Fri Aug 25, 2006 4:02 am 
Newbie

Joined: Thu Aug 17, 2006 4:40 am
Posts: 6
Hi all,
i'm using hibernate in connection with EJB 3.0, JBoss 4.0.4 and MySQL 5.0.22. I my case there are two objects/tables using composite PKs. The second PK contains a part of the FK of the first object. The Primary Keys are annotate by the @EmbeddedId tag. If i want to deploy my application on JBoss i get the following exception in my server log file.

org.hibernate.MappingException: Repeated column in mapping for entity: xx.xx.xx.xx.Reservation column: DATUM (should be mapped with insert="false" update="false")

Any suggestions? . Thank you in advance.
Dirk

Environment:
Hibernate EntityManager 3.2.0.CR1
Hibernate Annotations 3.2.0.CR1
Hibernate 3.2 cr2

Sources:
Table 1 - Cruise.java
Table 2 - Reservation.java
PK implementation - CruisePK.java, Reservation.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", updatable=false, insertable=false))
   })
   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;
   }

   public String getDate() { return _date; }
   public void setDate(String date) { _date = date; }
   
   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;
   }

   public String getDate() { return _date; }
   public void setDate(String date) { _date = date; }
   
   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  
 
 Post subject:
PostPosted: Mon Sep 11, 2006 9:00 am 
Beginner
Beginner

Joined: Sat Oct 08, 2005 2:13 am
Posts: 47
Hi

as the message says tou should map with (insert="false" update="false")

at the class Reservation

Code:

@JoinColumn(name = "CRUISE_ID", referencedColumnName="ID"),
@JoinColumn(name = "DATUM", referencedColumnName="DATUM")



should change to :

Code:

@JoinColumn(name = "CRUISE_ID", referencedColumnName="ID",insertable="false",updatable="false"),
@JoinColumn(name = "DATUM", referencedColumnName="DATUM",insertable="false",updatable="false")




----------------------
Mohammad Norouzi


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 05, 2006 8:32 am 
Beginner
Beginner

Joined: Mon Apr 03, 2006 2:41 am
Posts: 25
Location: Mauritius
Hi,

I have the same problem, but in my case, if i put "insertable="false",updatable="false", the column which is not a part of the PK is not present in the insert statement.

Code:
@ManyToOne(cascade = {}, fetch = FetchType.LAZY)
@JoinColumns( {
@JoinColumn(name = "Year", referencedColumnName = "Year", unique = false, nullable = false, insertable = false, updatable = false),
@JoinColumn(name = "QuestionnaireTemplateId", referencedColumnName = "QuestionnaireTemplateId", unique = false, nullable = false, insertable = false, updatable = false) })
public Questionnairetemplate getQuestionnairetemplate() {
   return this.questionnairetemplate;
}


so in my case the 'year' column is used in the PK and the column 'QuestionnaireTemplateId' is not in the insert statement.

Any idea about this issue ?

More details on :
http://forum.hibernate.org/viewtopic.ph ... 14#2325014

Thanks in advance for your help

Regards,
Christophe


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