I've found lots of these issues, but for some reason they don't work for me. So, I'm obviously doing something wrong.
Form Object:
@Entity
@Table(name = "WEBFORMS_WEBFORM")
public class Form {
@Id
@GeneratedValue
@Column(name = "FORM_ID")
Long id;
@Column(name = "NAME")
String name;
@OrderBy("priority")
@OneToMany(mappedBy = "form", cascade={javax.persistence.CascadeType.ALL}, orphanRemoval = true)
Set<FormField> formFields;
@OrderBy("creationDate DESC")
@OneToMany(mappedBy = "form", cascade={javax.persistence.CascadeType.ALL}, orphanRemoval = true)
Set<FormResult> formResults;
@Column(name = "ALLOW_GUESTS")
Boolean allowGuest;
@Column(name = "SEND_MAIL")
Boolean sendMail;
Form Field Object:
@Entity
@Table(name = "WEBFORMS_FORM_FIELD")
public class FormField {
@Id
@GeneratedValue
@Column(name = "FORM_FIELD_ID")
Long id;
@Column(name = "TYPE")
String type;
@Column(name = "CONTENT")
String content;
@Column(name = "PRIORITY")
Long priority;
@ManyToOne()
@JoinColumn(name = "FORM_ID")
public Form form;
I'm using hibernate annotation:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.5.6-Final</version>
</dependency>
If I create a form Field object within the form then no problems. Both objects are created. When I delete the form object the form field object stays in the database.
Many to one relationship established on the entity FormField. As a result a foreign key constraint on the field. It not allows to delete the entity. The constraint has attribute cascade that you must declare in hibernate.
@ManyToOne (cascade = {javax.persistence.CascadeType.ALL})