Skip to Main Content

Integration

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

more on JPa delete

HjavaDec 31 2010 — edited Jan 10 2011
have some time today, so try JPA anagin I get delete by use NativeQuery
but it suppose have two other ways
option one
I hard code it and I get
1)Query query=em.createQuery(" delete FROM student p WHERE p.userId =1570");
I get Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails
the student is a parent table , in normal sql I can't delete from the children table , but if I delete from the parent table all relate records in child just go , why I can't use this ideal in JPa??
option two
public class StudentFormService {
protected EntityManager em;

public StudentFormService(EntityManager em) {
this.em = em;
}
public void removeStudent(int id) {
Student emp = findStudent(id);
if (emp != null) {
em.remove(emp);
}
}

public Student findStudent(int userId) {
return em.find(student.class, userId);
}
then in my servlet How should I call the remove in servlet
I have
Student SD =studentDBO.findStudent(deleteId);
em.getTransaction().begin();
SD=studentDBO.removeStudent(deleteId);*/
not ideal how to continue
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 4 2011
Added on Dec 31 2010
6 comments
1,329 views