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;
}
}