Hello Hibernate Gurus
I have two classes (Purchase & PendingPayment) mapped with One-To-One bi-directional relationship. A Purchase may or may not have a pending payment but a purchase must exists for a pendingPayment. The classes share the primary key.
When I add the pendingPayment object to my Purchase and save it, an appropriate record is created in the pendingPayment table, but when I remove this reference and save/merge the purchase object, the record is not removed from the pendingPayment table. I have tried setting the cascade to CascadeType.ALL and CascadeType.DELETE_ORPHAN, but the delete on the child table just does not seem to be happening.
Any help on what I might be doing wrong would be greatly appreciated!
Following is simplified version of my code:
Purchase
Code:
package test;
import javax.persistence.*;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
@Entity
@Table(name = "PURCHASE")
public class Purchase {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "PURCHASE_ID")
private Long id;
@OneToOne(optional = true, fetch = FetchType.EAGER)
@Fetch(FetchMode.SELECT)
@org.hibernate.annotations.Cascade({org.hibernate.annotations.CascadeType.DELETE_ORPHAN,
org.hibernate.annotations.CascadeType.SAVE_UPDATE})
@PrimaryKeyJoinColumn
private PendingPayment pendingPayment;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
Purchase() {
}
public void addPending() {
if (pendingPayment == null) {
pendingPayment = new PendingPayment(this);
pendingPayment.setPaymentStatus("NEW");
}
}
public void removePending() {
if (pendingPayment != null) {
pendingPayment.setPurchase(null);
pendingPayment = null;
}
}
}
PendingPaymentCode:
package test;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;
@Entity
@Table(name="PENDING_PAYMENT")
public class PendingPayment {
@Id @GeneratedValue (generator="hibernate_foreign")
@GenericGenerator(name = "hibernate_foreign", strategy = "foreign", parameters = {
@Parameter(name="property", value="purchase")})
@Column(name = "PURCHASE_ID")
Long id;
@OneToOne(mappedBy = "pendingPayment", optional = false)
Purchase purchase;
@Column(name = "PAYMENT_STATUS")
String paymentStatus;
PendingPayment(Purchase purchase) {
setPurchase(purchase);
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Purchase getPurchase() {
return purchase;
}
public void setPurchase(Purchase purchase) {
this.purchase = purchase;
}
public String getPaymentStatus() {
return paymentStatus;
}
public void setPaymentStatus(String paymentStatus) {
this.paymentStatus = paymentStatus;
}
}
Here is the code that drives the operation
Code:
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction tx = session.beginTransaction();
Purchase purchase = new Purchase();
purchase.addPending();
session.save(purchase);
tx.commit();
Transaction newTransaction = session.beginTransaction();
purchase.removePending();
session.merge(purchase);
newTransaction.commit();
session.close();
Following is my configuration of the session factory
Code:
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">testdb</property>
<property name="hibernate.default_schema">test</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping class="test.Purchase" />
<mapping class="test.PendingPayment" />
</session-factory>
Hibernate version:
Hibernate Core: 3.2.6
Hibernate Annotations: 3.3.1
Name and version of the database you are using:
MySQL version 4.1.20
Thank You.