0

The question may seem a little bit of confusing so I'll try to bring it on with examples.

I'm working with JPA 2.0 and Eclipselink 2.2

I've got three classes Person, Student and Credentials wich have this relationship:

  • Student inherits (extends) from Person
  • Student has credentials (@OneToOne)

My classes are defined as the following:

Person.java

@Entity
@Table(name = "person")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING, length = 20)
@DiscriminatorValue("person")
public abstract class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String firstName;
    private String lastName;
    private String street;
    private String city;
    private String zipCode;
    private String phoneNumber;

    /*Getters and Setters*/

}

Student.java

@SuppressWarnings("serial")
@Entity
@Table(name = "student")
@DiscriminatorValue("student")
public class Student extends Person implements Serializable {

    @Column(unique = true)
    private String studentId;

    @OneToOne(fetch = FetchType.EAGER, cascade = { CascadeType.ALL }, orphanRemoval = true, optional = false, mappedBy = "student")
    @CascadeOnDelete
    private Credentials credentials;

    /*Getters and Setters*/
}

Credentials.java

@SuppressWarnings("serial")
@Entity
public class Credentials implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(unique = true)
    private String username;

    private String password;

    private Student student;
}

The problem is that when I try to delete a Student

        entityManager.getTransaction().begin();
        entityManager.remove(student);
        entityManager.getTransaction().commit();

This constraint appears (I can post more of the error message but this seems to be the key):

jul 26, 2013 11:51:50 AM com.vaadin.Application terminalError
Grave: Terminal error:
javax.persistence.PersistenceException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.2.0.v20110202-r8913): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Cannot delete or update a parent row: a foreign key constraint fails (`studentrecord`.`credentials`, CONSTRAINT `FK_CREDENTIALS_STUDENT_ID` FOREIGN KEY (`STUDENT_ID`) REFERENCES `person` (`ID`))
Error Code: 1451
Call: DELETE FROM person WHERE (ID = ?)
    bind => [36]
Query: DeleteObjectQuery(Alex)

I've tried several anotations, one direction, bidirectional relationship between Student and credentials but the result is pretty allways the same.

How can I delete the Student entity?

Any help would be much appreciated, thanks in advance.

Mannix
  • 41
  • 6
  • Not answering your question, but since there is no relationship in your Person class, you can use a mappedSuperClass instead of inherintance (for better performance) – ben75 Jul 26 '13 at 13:04
  • Since Person is abstract: I don't see any reason to define a discriminator value in Person class. – ben75 Jul 26 '13 at 13:06
  • You can only use the CascadeOnDelete annotation if there is a cascade delete set on the relationship in the database. Setting it means that when the database deletes Student, it will also take care of deleting Credentials. Since that is not happening, you probably do not have cascade delete set in the database. Remove the annotation and JPA will do the delete for you, or change the database - you can let EclipseLink DDL drop and recreate the database for you http://wiki.eclipse.org/EclipseLink/Examples/JPA/DDL EclipseLink 2.4 I believe indluce the cascade delete in the generated DDL – Chris Jul 26 '13 at 14:17
  • @ben75 Sorry, the abstract anotation was put afterwards, when trying to do some tests. I had tried `@MappedSuperclass` too but got the same result. – Mannix Jul 28 '13 at 15:35
  • @Chris My database is entirely generated through persistence.xml file. I've put some dummy code and have the `` syntax to rebuild the structure during development. I had no luck by deleting the tables manually. – Mannix Jul 28 '13 at 15:37
  • You are using EclipseLink 2.2, which will not add the cascade on delete constraint to the tables. You will need to use 2.4, modify the tables yourself (you can have EL generate a script and just modify it), or you can remove the annotation. – Chris Jul 28 '13 at 20:40
  • @Chris thanks and sorry for the late reply. I'll definitely try to change the version of EclipseLink. So far nothing seems to have worked. – Mannix Aug 07 '13 at 16:31

0 Answers0