Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: Hibernate Core 3.2.5, Annotations 3.3.0, Validator 3.0.0
Mapping documents:
Code:
@Entity
@Table ( name = "payments" )
@Cache ( usage = CacheConcurrencyStrategy.READ_WRITE)
public class Payment implements Serializable{
private PaymentPK paymentPK;
private Date paymentdate;
private BigDecimal amount;
/**
* @return the paymentPK
*/
@Id
public PaymentPK getPaymentPK() {
return paymentPK;
}
/**
* @param paymentPK the paymentPK to set
*/
public void setPaymentPK(PaymentPK paymentPK) {
this.paymentPK = paymentPK;
}
/**
* @return the paymentdate
*/
@Column ( name = "paymentdate" )
public Date getPaymentdate() {
return paymentdate;
}
/**
* @param paymentdate the paymentdate to set
*/
public void setPaymentdate(Date paymentdate) {
this.paymentdate = paymentdate;
}
/**
* @return the amount
*/
@Min (0)//es darf kein negativer betrag gebucht werden
@Column ( name = "amount" )
public BigDecimal getAmount() {
return amount;
}
/**
* @param amount the amount to set
*/
public void setAmount(BigDecimal amount) {
this.amount = amount;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((paymentPK == null) ? 0 : paymentPK.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Payment))
return false;
final Payment other = (Payment) obj;
if (paymentPK == null) {
if (other.paymentPK != null)
return false;
} else if (!paymentPK.equals(other.paymentPK))
return false;
return true;
}
Code:
@Entity
@Table ( name="customers" )
@Cache ( usage = CacheConcurrencyStrategy.READ_WRITE )
public class Customer implements Serializable{
/* Spalten der Tabelle als Attribute */
/** Erste Adresszeile*/
private String addresslineOne;
/** Zweite Adresszeile */
private String addresslineTwo;
/** Stadt */
private String city;
/** Vorname des Kontaktes */
private String contactfirstname;
/** Nachname des Kontaktes */
private String contactlastname;
/** Bundesland des Kunden */
private String country;
/** Kreditrahmen des Kunden */
private BigDecimal creditlimit;
/** Name des Kunden */
private String customername;
/** Datenbank Primärschlüssel, Kundennummer */
private Integer customernumber;
/** Telefonnummer des Kunden */
private String phone;
/** Postleitzahl des Kunden */
private String postalcode;
/** Betreuender Verkäufer des Kunden */
private Employee salesRep;
/** Staat des Kunden */
private String state;
/** Ein Set von allen Bezahlungen die für den Kunden bereits in der DB hinterlegt sind. */
private Set<Payment> payments;
@Length ( max = 50)
@Column ( name="addressline1" )
public String getAddresslineOne() {
return addresslineOne;
}
public void setAddresslineOne(String addresslineOne) {
this.addresslineOne = addresslineOne;
}
@Basic ( optional = true )
@Length ( max = 50)
@Column ( name="addressline2" )
public String getAddresslineTwo() {
return addresslineTwo;
}
public void setAddresslineTwo(String addresslineTwo) {
this.addresslineTwo = addresslineTwo;
}
@Length ( max = 50 )
@Column ( name="city" )
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Length ( max = 50 )
@Column ( name = "contactfirstname" )
public String getContactfirstname() {
return contactfirstname;
}
public void setContactfirstname(String contactfirstname) {
this.contactfirstname = contactfirstname;
}
@Length ( max = 50 )
@Column ( name = "contactlastname" )
public String getContactlastname() {
return contactlastname;
}
public void setContactlastname(String contactlastname) {
this.contactlastname = contactlastname;
}
@Length ( max = 50 )
@Column ( name = "country" )
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
@Basic ( optional = true )
@Column ( name = "creditlimit" )
public BigDecimal getCreditlimit() {
return creditlimit;
}
public void setCreditlimit(BigDecimal creditlimit) {
this.creditlimit = creditlimit;
}
@Length ( max = 50 )
@Column ( name = "customername" )
public String getCustomername() {
return customername;
}
public void setCustomername(String customername) {
this.customername = customername;
}
@Id
@Column ( name = "customernumber" )
public Integer getCustomernumber() {
return customernumber;
}
public void setCustomernumber(Integer customernumber) {
this.customernumber = customernumber;
}
@Length ( max = 50 )
@Column ( name = "phone" )
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Basic ( optional = true )
@Length ( max = 15 )
@Column ( name = "postalcode" )
public String getPostalcode() {
return postalcode;
}
public void setPostalcode(String postalcode) {
this.postalcode = postalcode;
}
@ManyToOne ( optional = true, targetEntity = Employee.class)
@JoinColumn ( name = "salesrepemployeenumber" )
public Employee getSalesRep() {
return salesRep;
}
public void setSalesRep(Employee salesRepEmployee) {
this.salesRep = salesRepEmployee;
}
@Basic ( optional = true )
@Length ( max = 50 )
@Column ( name = "state" )
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
/**
* @return the payments
*/
@OneToMany ( targetEntity = Payment.class, fetch = FetchType.LAZY)
@Cascade ( {org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN} )
@JoinColumn ( name = "customernumber" )
@OnDelete ( action = OnDeleteAction.CASCADE )
public Set<Payment> getPayments() {
return payments;
}
/**
* @param payments the payments to set
*/
public void setPayments(Set<Payment> payments) {
this.payments = payments;
}
/**
* Die überschriebene Methode nutzt zur hashCode-Berechnung die <code>customernumber</code> (PK der Datenbank), sowie
* den <code>customername</code>
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((customername == null) ? 0 : customername.hashCode());
result = prime * result
+ ((customernumber == null) ? 0 : customernumber.hashCode());
return result;
}
/**
* Die überschriebene equals-Methode vergleicht die Objekte zunächst danach ob es sich um eine Instanz der
* gleichen Klasse handelt. Im Anschluss werden die <code>customernumber</code> und falls diese nicht vorhanden sein sollte der
* <code>customername</code> verglichen
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Customer))
return false;
final Customer other = (Customer) obj;
if (customernumber == null) {
if (other.customernumber != null)
return false;
} else if (!customernumber.equals(other.customernumber))
return false;
if (customername == null) {
if (other.customername != null)
return false;
} else if (!customername.equals(other.customername))
return false;
return true;
}
/**
* Die überschriebene toString-Methode gibt die Customernumber und den Customername aus
*/
@Override
public String toString(){
return "ID: "+customernumber+" Name: "+customername;
}
}
Code:
@Embeddable
public class PaymentPK implements Serializable{
private Customer customer;
private String checknumber;
/**
* @return the customer
*/
@ManyToOne ( targetEntity = Customer.class)
@JoinColumn ( name = "customernumber" )
public Customer getCustomer() {
return customer;
}
/**
* @param customer the customer to set
*/
public void setCustomer(Customer customer) {
this.customer = customer;
}
/**
* @return the checknumber
*/
@Length ( max = 50 )
@Column ( name = "checknumber" )
public String getChecknumber() {
return checknumber;
}
/**
* @param checknumber the checknumber to set
*/
public void setChecknumber(String checknumber) {
this.checknumber = checknumber;
}
/* (non-Javadoc)
* @see java.lang.Object#hashCode()
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((checknumber == null) ? 0 : checknumber.hashCode());
result = prime * result
+ ((customer == null) ? 0 : customer.hashCode());
return result;
}
/* (non-Javadoc)
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof PaymentPK))
return false;
final PaymentPK other = (PaymentPK) obj;
if (checknumber == null) {
if (other.checknumber != null)
return false;
} else if (!checknumber.equals(other.checknumber))
return false;
if (customer == null) {
if (other.customer != null)
return false;
} else if (!customer.equals(other.customer))
return false;
return true;
}
}
Name and version of the database you are using:
HSQLDB 1.8.0.0
Problems with Session and transaction handling?
Hallo zusammen,
mein Problem liegt zur Zeit im Mapping mit Annotations. Mein Ziel ist es einen Customer zu löschen und kaskadierend die Payments mitzulöschen. Im XML-Mapping konnte ich das mit dem Attribut inverse erreichen. Wie stelle ich das aber über Annotationen her??
Im Grunde geht es mir bei den geposteten Mappings hauptsächlich um diesen Bereich aus dem Entity customer:
/**
* @return the payments
*/
@OneToMany ( targetEntity = Payment.class, fetch = FetchType.LAZY)
@Cascade ( {org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN} )
@JoinColumn ( name = "customernumber" )
@OnDelete ( action = OnDeleteAction.CASCADE )
public Set<Payment> getPayments() {
return payments;
}
Es wird so wie ich es kenne keine große Sache sein, aber sie will sich mir einfach nicht erschliessen.
Vielen Dank für Eure Mithilfe!
Oliver