简体   繁体   中英

Update one-to-one relation in JPA

Hello i got the following entity class Book: $

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

@OneToOne(cascade=CascadeType.PERSIST)
private BookProfile bookProfile;

@OneToMany(cascade=CascadeType.PERSIST , orphanRemoval=true, fetch=FetchType.EAGER)
private List<Rating> ratings;

$

i try to add a Profile like this:

$

Book book = manager.find(Book.class, bId);
BookProfile bookProfile = new BookProfile();
manager.getTransaction().begin();
book.setBookProfile(bookProfile);
manager.merge(book);
manager.getTransaction().commit();

$

But nothing happens the profile in Book is still null. What am i doing wrong ?

您需要将MERGE添加到bookProfile字段的级联类型。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM