Author.java
:
package com.abc.entity;
@Data
@Entity
@Table(name="author")
public class Author {
@Id
@Column(nullable=false, name="id")
@SequenceGenerator(sequenceName="author_seq", name="a_seq")
@GeneratedValue(generator="a_seq", strategy=GenerationType.SEQUENCE)
private Integer id;
@Column(nullable=false, name="first_name")
@NotBlank(message = "First Name is required")
private String firstName;
@Column(name="last_name")
@NotNull(message = "Last Name is required")
private String lastName;
@Column(name = "email", unique = true)
@NotNull(message="email cannot be null")
@Email
@Pattern(regexp="[A-Za-z0-9._%-+]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}")
private String email;
@NotNull(message="Password")
@Size(min=8, max=16, message="Password must be >= 8 and <=16 ")
private String password;
@OneToMany(/*orphanRemoval = true,*/
//mappedBy="author",
cascade=CascadeType.ALL)
private List<Book> books = new ArrayList<Book>();
/**
* Adding a book and in turn setting it author to this object
* @param book
*/
public void addBook(Book book) {
books.add(book);
book.setAuthor(this);
}
/**
* Removing a book from the author
* @param book
*/
public void removeBook(Book book) {
books.remove(book);
book.setAuthor(null);
}
}
Book.java
:
package com.abc.entity;
@Data
@Entity
@Table(name="book")
public class Book {
@Id
@Column(nullable=false, name="id")
@SequenceGenerator(sequenceName="book_seq", name="b_seq")
@GeneratedValue(generator="b_seq", strategy=GenerationType.SEQUENCE)
private Long id;
@Column(name="isbn")
@NotNull(message = "isbn is required")
//@Size(min=8, max=8, message="isbn must be = 8 numbers")
private Long isbn;
@Column(nullable=false, name="title")
@NotNull(message = "Title is required")
private String title;
@Column(nullable=false, name="edition")
@NotBlank(message = "Edition is required")
private String edition;
@Column(nullable=false, name="price")
@NotNull(message = "Price is required")
private Double price;
@Column(nullable=false, name="date_published")
@Pattern(regexp = "^(0[1-9]|1[0-2])([\\/])([1-9][0-9])$", message = "Must be formatted MM/YY")
private String datePublished;
//@ForeignKey(name="FK_COUNTRY")
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name="author_id")//, referencedColumnName="id")
@OnDelete(action = OnDeleteAction.CASCADE)
private Author author;
}
我认为 book
表中的外键列将与 author
表的主键一起插入。
但是,如果我删除了指向作者主键的 referencedColumnName
属性,则会插入 null
。
当我添加 referencedColumnName
属性时,出现错误,指示 null
无法插入到 book
表的外键列中。
java.sql.SQLIntegrityConstraintViolationException: ORA-01400: cannot insert NULL into ("SPRING"."BOOK"."AUTHOR_ID")
我创建了两个实体。
作者
(可以拥有/写很多书)[OneToMany
]- 一本只能属于一位作者的
图书
[ManyToOne
]
当我在 Postman 中发送有效负载来测试插入 author
的 @PostMapping
时,book
表 (author_id
) 中应引用 author
表主键 (id
) 的外键将插入 null
,而不是 author
主键的值。
当我添加 referencedColumnName
时,它会引发异常,指示 null
无法插入到 book.author_id
中。
我已经尝试过:
- 使用
@PrimaryKeyJoinColumn
注释 referencedColumnName
属性
最佳答案
我能够通过添加两个注释来解决我的问题:JsonManageReference 和 JsonBackReference。
现场的@JsonManageReference 私有(private)列出 Author 类中的书籍
和 @JsonBackReference 在现场 Book 类中的私有(private) Author 作者
关于java - 在 postman 中发送有效负载时,有没有办法从另一个表的主键插入外键值的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55421113/