krisy
krisy

Reputation: 1558

Cascade and persist

I'm a bit of confused about JPA's Cascade annotation. For example I have two entities:

Company c
Employee e

In the database Employee references Company by a foreign key.

If - without defininf Cascade - I do the following:

e.setCompany(c);
c.getEmployeeCollection().add(e);
em.persist(c);

both company and employee will be persisted into the database.

So what is the point of using

@OneToMany(cascade = CascadeType.ALL, mappedBy = "company")
private Collection<Employee> employeeCollection;

annotation in Company?

Thanks, krisy

Upvotes: 0

Views: 71

Answers (1)

isah
isah

Reputation: 5341

No operations are cascaded by default in JPA. Hibernate implementation follows the spec regarding cascades, so by default it has no cascade operations. You did not mention what JPA implementation you're using.

@OneToMany(cascade = CascadeType.ALL, mappedBy = "company")

I guess you mean the point of cascade attribute(which is to override defaults) because @OneToMany is specified to make the association bidirectional in this case.

Upvotes: 1

Related Questions