I'm trying to implements this solution: BookPublisher有一个复合键,所以我构建了embeddable类:
@Embeddable
public class BookPublisherID implements Serializable{
private int bookID;
private int publisherID;
//getters and constructor
这是bookPublisher类:
@Entity
@Table(name = "book_publisher")
public class BookPublisher implements Serializable{
@EmbeddedId
private BookPublisherID id;
private Date publishedDate;
@MapsId("bookID")
@ManyToOne
@JoinColumn(name = "book_id")
private Book book;
@MapsId("publisherID")
@ManyToOne
@JoinColumn(name = "publisher_id")
private Publisher publisher;
//getters and setters
这是书类:
@Entity
public class Book{
private int id;
private String name;
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<BookPublisher> bookPublishers;
//getters and setters
发布商类:
@Entity
public class Publisher{
private int id;
private String name;
@OneToMany(mappedBy = "book", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<BookPublisher> bookPublishers;
//getters and setters
BookPublisher存储库:
public interface BookPublisherRepository extends JpaRepository<BookPublisher, BookPublisherID>{}
这是实施:
@Service
public class BookPublisherImpl implements BookPublisherMetier{
@Autowired
private BookPublisherRepository bookPublisherRepository;
@Override
public BookPublisher savePublication(BookPublisher bookPublisher) {
bookPublisher.setPublishedDate(new Date());
return bookPublisherRepository.save(bookPublisher);
}
}
问题是如何在复合键的情况下在Json中添加bookPublisher(我使用postman)。我尝试了下面的解决方案和其他可能性,但我遇到了同样的问题: 尝试从null一对一属性中分配id [com.example.entities.BookPublisher.book]
{
"id": {
"bookID":1,
"publisherID":1
},
"book":{
"id":1
},
"publisher":{
"id":1
}
}
非常感谢。
答案 0 :(得分:0)
我在这里看到几个关键项目。