在邮递员中发送有效负载时,有没有办法从另一个表的主键插入外键值的值?



>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")

我创建了两个实体。

  1. 一个Author(谁可以拥有/写很多书) [OneToMany]
  2. 只能属于一个作者的Book[ManyToOne]

当我在Postman中发送有效负载来测试插入author@PostMapping时,表中bookauthor_id中应该引用表(id)的主键的外键author插入null而不是author主键的值。

当我添加referencedColumnName时,它会抛出一个异常,指示null无法插入book.author_id

我试过:

  1. 使用@PrimaryKeyJoinColumn注释
  2. referencedColumnName属性

我能够通过添加两个注释来解决我的问题:JsonManageReference 和 JsonBackReference。

场上@JsonManageReference 作者类中的私有列表书籍

和 场上@JsonBackReference 书籍类中的私人作者作者

最新更新