Hi,
This question has been posted in the forum before and there seem to be no replies for it.
CascadeStrategy with "all-delete-orphan" with annotations does not seem to be implemented with Annotations, possibly because of the limitations of the current EJB 3.0 Spec. But then, there needs to be some solution for itl!! Someone kindly comment or advice and alternative!
Here is a Simple code base that I tried
Code:
@Entity(access = AccessType.FIELD)
public class Customer {
@Id
@Column(length=100)
public String ID;
public String Name;
@OneToMany(cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="CUSTOMER_ID")
public Set<Ticket> Tickets = new HashSet();
public Customer() {
}
public Customer(String _Name) throws Exception {
ID = IDGen.generateID();
Name = _Name;
}
public Ticket addTicket(String _Number) throws Exception {
Ticket TicketObj = new Ticket();
TicketObj.Number = _Number;
TicketObj.customer = this;
Tickets.add(TicketObj);
return TicketObj;
}
}
Customer <> One-ToMany <> Tickets
Code:
@Entity(access = AccessType.FIELD)
public class Ticket {
@Id
@Column(length=100)
public String ID;
public String Number;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="CUSTOMER_ID")
public Customer customer;
public Ticket() throws Exception {
ID = IDGen.generateID();
}
}
Code that uses these relationships
Code:
Customer CustomerObj;
try {
DatabaseManager.beginTransaction();
CustomerObj = new Customer("Gyani");
CustomerObj.addTicket("1");
CustomerObj.addTicket("2");
DatabaseManager.getSession(MountPoint).saveOrUpdate(CustomerObj);
DatabaseManager.commitTransaction();
} catch (Exception ExceptionObj) {
DatabaseManager.rollbackTransaction();
throw ExceptionObj;
} finally {
DatabaseManager.closeSession();
}
//The customer at this point has 2 Tickets
try {
DatabaseManager.beginTransaction();
CustomerObj.Tickets = new HashSet();
CustomerObj.addTicket("3");
DatabaseManager.getSession(MountPoint).saveOrUpdate(CustomerObj);
DatabaseManager.commitTransaction();
} catch (Exception ExceptionObj) {
DatabaseManager.rollbackTransaction();
throw ExceptionObj;
} finally {
DatabaseManager.closeSession();
}
//The customer at this point has 1 Ticket
try {
DatabaseManager.beginTransaction();
CC.Tickets = new HashSet();
CC.addTicket("3");
DatabaseManager.getSession(MountPoint).saveOrUpdate(CC);
DatabaseManager.commitTransaction();
} catch (Exception ExceptionObj) {
DatabaseManager.rollbackTransaction();
throw ExceptionObj;
} finally {
DatabaseManager.closeSession();
}
//ERROR Customer should have 1 Tickets, but it reflects that the customer has 3 Tickets, even though the Customer Object has 1 Ticket !!!