0

These are two related entities in Eclipselink JPA:

@Entity
public class Department {

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

    private String name;

}

@Entity
public class Person {

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

    private String firstName;

    private String lastName;

    @ManyToOne(cascade={CascadeType.ALL})
    private Department department;

}

and this is the generated DDL:

CREATE TABLE PERSON (ID BIGINT IDENTITY NOT NULL, FIRSTNAME VARCHAR, LASTNAME VARCHAR, DEPARTMENT_ID BIGINT, PRIMARY KEY (ID))
CREATE TABLE DEPARTMENT (ID BIGINT IDENTITY NOT NULL, NAME VARCHAR, PRIMARY KEY (ID))
ALTER TABLE PERSON ADD CONSTRAINT FK_PERSON_DEPARTMENT_ID FOREIGN KEY (DEPARTMENT_ID) REFERENCES DEPARTMENT (ID)

The environment is: - eclipselink 2.5.2 - mysql-connector-java 5.1.6

I would expect to have at least a ON DELETE CASCADE clause on the foreign key definition. What is the cascade option intended for, in the @ManyToOne relationship? Do I really have to delete the children records manually before deleting the parent record?

pms
  • 944
  • 12
  • 28
  • duplicate post: http://stackoverflow.com/questions/7197181/jpa-unidirectional-many-to-one-and-cascading-delete – pms Aug 01 '14 at 16:05
  • In EclipseLink you would specify the @CascadeDelete annotation to tell it the relationship should be auto deleted at the database level, as described here http://wiki.eclipse.org/EclipseLink/Examples/JPA/DeleteCascade – Chris Aug 01 '14 at 18:27

1 Answers1

0

The CascadeType you mention in your example is one value of those: ALL, PERSIST, MERGE, REMOVE, REFRESH, DETACH. This is ORM, but not SQL DDL related.

I guess You are searching for a SQL DDL foreign key constraint definition ...on delete cascade. To geht this SQL DDL generated, You need an @CascadeOnDelete annotation, as shown in this example:

...
@OneToMany(mappedBy="abc", orphanRemoval=true, cascade={CascadeType.ALL})
@CascadeOnDelete
private List<MobilPhoneNumer> mobilePhonesNumbers;
...
Hartmut Pfarr
  • 5,534
  • 5
  • 36
  • 42