my basic domain model looks like this :
My problem exists with the mapping between the other Entities and the Attendance entity.
In Student i have tried :
@OneToMany(orphanRemoval=true,mappedBy = "student",cascade= CascadeType.ALL)
//@Cascade(org.hibernate.annotations.CascadeType.ALL)
private Set<Attendance> attendance = new HashSet<Attendance>() ;
on Attendance:
@ManyToOne( cascade= {CascadeType.PERSIST, CascadeType.MERGE} )
//@Cascade(org.hibernate.annotations.CascadeType.PERSIST)
@JoinColumn(name="studentId", nullable = true)
private Student student;
@OneToMany(mappedBy = "attendance",cascade=CascadeType.ALL)
private Set<Absence> absence = new HashSet<Absence>() ;
@ManyToOne()
@JoinColumn(name="seminarId", nullable = false)
private Seminar seminar;
@OneToMany(mappedBy = "attendance",cascade=CascadeType.ALL)
private Set<Payment> payment = new HashSet<Payment>() ;
The problem i have is on the deletion of a Student(or a Seminar). What i want to do is, when a Student is deleted, all of his/her Attendances must be deleted too.
First I tested without creating any of the Absence or payment objects.
If I remove the cascade options i get the following trace
java.lang.IllegalStateException: org.hibernate.TransientObjectException: object is an unsaved transient instance - save the transient instance before merging: com.seminars.domain.Attendance
If i leave the cascade options and try to call em.remove(student) i get
javax.persistence.RollbackException: Error while committing the transaction
..........
Caused by: javax.persistence.EntityNotFoundException: deleted entity passed to persist: [com.seminars.domain.Student#<null>]
If above all this i try to add an absence to an attendance i still get the same as above( even though something tells me that even if student was removed something else would break)
I'm starting to believe that the domain model is completely wrong... Is it even possible to do what i want with JPA and hibernate ? What are the correct mapping options that i need to setup in my case ? (note: i also tried what is shown in this answer but then i had problems with attendance-absence
java.lang.IllegalStateException: org.hibernate.TransientObjectException: object is an unsaved transient instance - save the transient instance before merging: com.seminars.domain.Attendance
I am completely lost, any help would be greatly appreciated
EDIT: Below is the method for deleting a student ( please note that because em.remove(student); dint work, i resorted to manually delete the student. If i add an absence though, it is expected for the below code to not work. The real question is why em.remove() doesnt work, and what are the correct mappings that have to be set in order for it to work)
public void Delete(Object object) {
int id=(Integer) object;
if (!tx.isActive()) {
tx.begin();
}
Attendance att = em.find(Attendance.class, student.getStudentId());
Query query = em.createQuery( "DELETE FROM Attendance c WHERE c.student = :id");
query.setParameter("id", student);
query.executeUpdate();
em.clear();
//findObject(null, id,null);
//student=em.merge(student);
//em.persist(student);
student=em.find(Student.class, id);
query = em.createQuery( "DELETE FROM Student c WHERE c.studentId = :id");
query.setParameter("id", id);
query.executeUpdate();
//em.remove(student);
tx.commit();
//em.clear();
}