Thomas Vervik
Thomas Vervik

Reputation: 4535

Hibernate saveAndUpdate doesnt persist relations, while merge does

I have the following entity with the following relation:

@Entity
@Table(name = "bpr_user")
public abstract class User {
  ...
  @OneToOne(fetch = FetchType.EAGER, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH,  CascadeType.REMOVE})
   @JoinColumn(name = "bcard_id", nullable = true)
   public BCard getBcard() {
     return bcard;
   }
}

When I run merge like this:

    Adult adult = new Adult();
    BCard bcard = createBCard();
    adult.setBcard(bcard);
    adult = adultDao.merge(adult);

The Bcard entity get persisted and given an id, but if I use saveOrUpdate only Adult get persisted, not Bcard. Why??

    Adult adult = new Adult();
    BCard bcard = createBCard();
    adult.setBcard(bcard);
    adult = adultDao.saveOrUpdate(adult);

Upvotes: 0

Views: 109

Answers (1)

JB Nizet
JB Nizet

Reputation: 691755

Because you have configured a cascade for MERGE, but not for SAVE_UPDATE.

Use the Cascade annotation to add a cascade for SAVE_UPDATE.

Upvotes: 2

Related Questions