3

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
SpikerTom
  • 125
  • 3
  • 16

3 Answers3

2

Hibernate Orphan removal example

Vipul Paralikar
  • 1,508
  • 10
  • 22
0

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})
Roman C
  • 49,761
  • 33
  • 66
  • 176
0

my bad.

I was trying to delete the form object by ID. This didnt works.

public void deleteForm(Form form) {

        Session session = HibernateUtil.getSession();
        session.delete(form);
        HibernateUtil.shutDown(session);
    }

Deleting the object that i receive after a query works just fine.

SpikerTom
  • 125
  • 3
  • 16