Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate Cascade PERSIST

I have a general question about Hibernate that I'm batteling with.

I have class A and class B, where B is dependent on A

In my code when I call em.persist(objOfTypeA) I would expect the insert to go and insert into both tables AAA and BBB. If I manually presist A get A's ID and fill it in the list for each object and then persist that list, things are working. But I would expect this to happen magically by Hibernate.

Am I doing something wrong? Or am I just expecting too much of Hibernate?

Thanks

@Entity
@Table(name = "AAA")
@Veto
public class A {

    @Id
    @GeneratedValue
    @Column(name = "Id")
    private Long id;


    @NotNull
    @Column(name = "Name")
    private Long name;

    ...

    @OneToMany(mappedBy="a", fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
    private List<B> b;
...
}

@Entity
@Table(name = "BBB")
@Veto
public class B {

    @Id
    @GeneratedValue
    @Column(name="Id")
    private Long id;

    @NotNull
    @Column(name="AId")
    private Long aId;

    @NotNull
    @Column(name = "Name")
    private Long name;

    @JoinColumn(name = "AId", referencedColumnName="Id", updatable = false, insertable = false)
    @ManyToOne(optional = false)
    private A a;
...     
}
like image 393
WonkeyDonkey Avatar asked Feb 25 '12 11:02

WonkeyDonkey


People also ask

What is Cascade persist?

The cascade persist is used to specify that if an entity is persisted then all its associated child entities will also be persisted. The following syntax is used to perform cascade persist operation: - @OneToOne(cascade=CascadeType.PERSIST)

What is Hibernate cascade?

Tags:cascade | hibernate. Cascade is a convenient feature to save the lines of code needed to manage the state of the other side manually. The “Cascade” keyword is often appear on the collection mapping to manage the state of the collection automatically.

What is Cascade annotation in Hibernate?

Cascading is a feature in Hibernate, which is used to manage the state of the mapped entity whenever the state of its relationship owner (superclass) affected. When the relationship owner (superclass) is saved/ deleted, then the mapped entity associated with it should also be saved/ deleted automatically.

What is the default cascade type in Hibernate?

There is no default cascade type in JPA. By default, no operation is cascaded.


2 Answers

I finally worked it out. None of the examples I was given or found told me exactly what was wrong, it was only after my own experimentation that I managed to come to this conclusion:

Instead of having

@OneToMany(mappedBy="a", fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
private List<B> b;

that didn't work (FK was still coming as NULL)

This did work:

@OneToMany(fetch=FetchType.LAZY, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
@JoinColumn(name="AId")
@NotNull
private List<B> b;
like image 135
WonkeyDonkey Avatar answered Sep 30 '22 23:09

WonkeyDonkey


Modify the mapping of class B as below,

@Entity
@Table(name = "BBB")
@Veto
public class B {

    @Id
    @GeneratedValue
    @Column(name="Id")
    private Long id;

    @NotNull
    @Column(name="AId", updatable = false, insertable = false)
    private Long aId;

    @NotNull
    @Column(name = "Name")
    private Long name;

    @JoinColumn(name = "AId", referencedColumnName="Id")
    @ManyToOne(optional = false)
    private A a;
...     
}

You were using the attributes updatable = false, insertable = false at wrong place and I have corrected it.

Read here to understand how they works.

like image 20
ManuPK Avatar answered Sep 30 '22 22:09

ManuPK