Hi all
I have a problem with my web application(EJB3 - JPA - HIBERNATE).
I have these entity:
COMMESSA.java
Code:
@Entity
public class Commessa implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "COMMESSA_ID")
private Long commessaId;
....
....
@JoinColumn(name = "PROJECT_MANAGER_ID", referencedColumnName = "UTENTE_ID")
@ManyToOne(optional = true)
private Utente projectManagerId;
@OneToMany(cascade = {CascadeType.MERGE ,CascadeType.REMOVE ,CascadeType.REFRESH} ,mappedBy = "commessaId",fetch=FetchType.EAGER)
private Set<Fattura> fatturaList;
@OneToMany(cascade = {CascadeType.MERGE ,CascadeType.REMOVE , CascadeType.REFRESH} ,mappedBy = "commessaId",fetch=FetchType.EAGER)
private Set<Timesheet> timesheetList;
TIMESHEET.java
Code:
@Entity
public class Timesheet implements Serializable {
private static final long serialVersionUID = 1L;
@JoinColumn(name = "COMMESSA_ID", referencedColumnName = "COMMESSA_ID")
@ManyToOne(optional = true)
private Commessa commessaId;
@LazyCollection(LazyCollectionOption.FALSE)
@OneToMany(mappedBy = "timesheetId",cascade = CascadeType.ALL)
private List<Giornofatturabile> giornofatturabileList;
In my EAO class : commessaEAO.java I have a method which delete a "COMMESSA":
Code:
....
private EntityManager em;
public void deleteCommessa(Long commessaId)throws SystemFailureException{
try{
Commessa commessa=em.find(Commessa.class,commessaId);
this.em.remove(this.em.merge(comessa);
}catch(Exception e){
e.printStackTrace();
//throw new SystemFailureException(e);
}
}
When i try to delete item from commessa i have this exception:
Code:
javax.persistence.EntityNotFoundException: deleted entity passed to persist: [it.uniroma2.pms.entity.Timesheet#<null>]
I have tryed many solution:
1. Remove the CascadeType.PERSIST
2. I have also followed this tutorial :http://community.jboss.org/wiki/EJB3relationships.pdf
But nothing have solved my problem.
if i change code of deleteCommessa() to this,it work,but I'm sure this solution is not a good way to do things:
Code:
Commessa commessa=em.find(Commessa.class,commessaId);
List<Long> tsIdList = new ArrayList();
for(Timesheet timesheet : commessa.getTimesheetList()){
tsIdList.add(new Long(timesheet.getTimesheetId()));
timesheet.setCommessaId(null);
//this.em.remove(timesheet);
}
commessa.getTimesheetList().clear();
System.out.println("FINE METODO PRIMA DI REMOVE");
this.em.remove(this.em.merge(commessa));
this.em.flush();
for(Long tsId : tsIdList){
Query query=this.em.createNativeQuery("DELETE FROM GIORNOFATTURABILE WHERE TIMESHEET_ID=?1");
query.setParameter(1,tsId);
query.executeUpdate();
}
Query query=this.em.createNativeQuery("DELETE FROM TIMESHEET WHERE COMMESSA_ID IS NULL");
query.executeUpdate();
P.s.: Other OneToMany relation Set<Fattura> fatturaList don't have any problem.
I can delete normally a parent with relative childs:
The code is the same of <Set>timesheetList.
Code:
public void deleteFattura(Long fatturaId)throws SystemFailureException{
try{
Fattura fattura=this.em.find(Fattura.class,fatturaId);
this.em.remove(em.merge(fattura));
}catch(Exception e){
throw new SystemFailureException(e);
}
}
any suggestion is appreciated.Thank you!