Hi,
I'm having trouble using hibernate with JPA annotations. I'm a newbie so apologies if this is a stupid question.
I'm trying to do one-to-many relationship between a Product entity and Version entities. A product has many versions, each of which has one product. Code is shown below.
I want a deletion of the product to cascade through to the Version table and remove all versions associated to that product.
The PostgreSQL error I'm getting is
"Caused by: org.postgresql.util.PSQLException: ERROR: update or delete on table "product" violates foreign key constraint "fk782db4b819a92fb2" on table "version"
Detail: Key (id)=(2) is still referenced from table "version"."
which leads to:
"Caused by: org.hibernate.exception.ConstraintViolationException: could not execute update query"
Even though I specify CascadeType.ALL (or REMOVE) in my annotation, the version table ends up with the following foreign key constraint:
REFERENCES product () MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION;
----------------------------------------------------------------------------
Code:
/* skipping imports and package*/
@Entity
public class Product {
public static final String DELETE_allProducts = "DELETE FROM Product";
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Basic
@Column(nullable = false, length = 40)
private String name;
@OneToMany(mappedBy="product", cascade=CascadeType.ALL)
List<Version> versions;
}
/* skipping imports and package*/
@Entity
public class Version {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
long id;
@Basic
@Column(nullable = false, length = 30)
String name;
@ManyToOne
Product product;
}