我是spring和java的新手,所以如果这很明显就道歉。
TLDR 当我使用嵌套资源发送JSON时,它会创建子资源。即使它已经存在,导致持久性问题,你如何在Spring中阻止它?
我有两个实体,书和书架。书架可以有多本书,但书只能放在一个书架上。所以货架(1)< - > (*)预订。
@Entity
public class Book {
@Id
@GenericGenerator(name = "uuid-gen", strategy = "uuid2")
@GeneratedValue(generator = "uuid-gen")
@Type(type = "pg-uuid")
private UUID id;
@Column(nullable = false)
private String name;
private String description;
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(foreignKey = @ForeignKey(name = "shelf_id"))
private Shelf shelf;
public Book() {
}
public Book(UUID id, String name, String description, Shelf shelf) {
this.id = id;
this.name = name;
this.description = description;
this.shelf = shelf;
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setShelf(Shelf shelf) {
this.shelf = shelf;
}
}
保质
@Entity
public class Shelf {
@Id
@GenericGenerator(name = "uuid-gen", strategy = "uuid2")
@GeneratedValue(generator = "uuid-gen")
@Type(type = "pg-uuid")
private UUID id;
@Column(nullable = false)
private String name;
private String description;
@OneToMany(fetch=FetchType.LAZY, mappedBy = "shelf", cascade = CascadeType.ALL)
private Set<Book> books;
public Dashboard() {
}
public Dashboard(UUID id, String name, String description, Set<Book> books) {
this.id = id;
this.name = name;
this.description = description;
this.book = book;
}
public UUID getId() {
return id;
}
public void setId(UUID id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setBooks() {
for (Book book : books)
book.setShelf(this);
}
public Set<Book> getBooks() {
return books;
}
}
我有一个扩展JpaRepository的ShelfRepository。
当我向身体提出请求时
{"name":"ShelfName", "books": [{"name": "bookName"}]}
它将返回创建资源Book and Shelf但不链接它们,因为Book是先创建的而没有Shelf引用。因此需要在架子上调用setBooks。不理想,但我无法找到另一种方式。
创建一本书并使用id作为books数组中的引用(我希望在我的API中使用),如下所示:
{"name":"otherShelfName", "books": [{"id": "7d9c81c2-ac25-46ab-bc4d-5e43c595eee3"}]}
由于本书已经存在,这会导致持久性问题
org.hibernate.PersistentObjectException:传递给persist的分离实体
有没有办法在Spring中拥有如上所述的嵌套资源,并且能够在没有持久性问题的情况下进行关联?
--------服务
@Service
public class BookService {
@Autowired
private BookRepository bookRepository;
public List<Book> findAll() {
return bookRepository.findAll();
}
public Book create(Book book) {
return bookRepository.save(book);
}
}
@Service
public class ShelfService {
@Autowired
private ShelfRepository shelfRepository;
public List<Shelf> findAll() {
return shelfRepository.findAll();
}
public Book create(Book book) {
Shelf newShelf = shelfRepository.save(shelf);
shelf.setBooks();
return newShelf;
}
}
答案 0 :(得分:-1)
欢迎来到痛苦的......呃,我的意思很棒...... ORM的世界在做简单的事情是微不足道的,但做得太复杂也变得越来越复杂了:P
可能有帮助的几个指针:
我希望所有这些信息对您有所帮助。