空外键(Springboot、Hibernate、Postman)
Null Foreign Key (Springboot, Hibernate, Postman)
我正在使用带 Hibernate 的 Springboot,我想使用 POST 请求保存一个新的“post”到我的数据库。我想强调的一件事是我正在使用依赖项“spring-boot-starter-data-rest”。
数据库架构(MySQL):
Class 用户:
@Entity
@Table(name="user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id", nullable = false)
public int id;
@OneToMany(mappedBy = "user_id_fk")
public Set<Post> posts;
@Column(name="email")
private String email;
@Column(name="username")
private String username;
@Column(name="password")
private String password;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="create_time")
protected Date createTime;
@Column(name="type")
private String accountType;
public User() {
this.createTime = new java.util.Date();
}
public User(String email, String username, String password, String firstName, String lastName, Date createTime, String accountType) {
this.email = email;
this.username = username;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.createTime = createTime;
this.accountType = accountType;
this.createTime = new java.util.Date();
}
public User(int id, String email, String username, String password, String firstName, String lastName, Date createTime, String accountType) {
this.id = id;
this.email = email;
this.username = username;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.createTime = createTime;
this.accountType = accountType;
this.createTime = new java.util.Date();
}
加上 Getters & Setters & toString()。
Class Post:
@Entity
@Table(name="post")
public class Post implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
public int id;
@ManyToOne(optional = false)
@JoinColumn(name = "user_id_fk", nullable = false)
public User user_id_fk;
@Column(name="comment")
private String comment;
@Column(name="likes")
private int likes;
@Column(name="dislike")
private int dislike;
@Column(name="create_time")
protected Date createTime;
public Post() {
this.createTime = new java.util.Date();
}
public Post(String comment, int likes, int dislike, User user_id_fk) {
this.user_id_fk = user_id_fk;
this.comment = comment;
this.likes = likes;
this.dislike = dislike;
this.createTime = new java.util.Date();
}
public Post(int id, User user_id_fk, String comment, int likes, int dislike) {
this.id = id;
this.user_id_fk = user_id_fk;
this.comment = comment;
this.likes = likes;
this.dislike = dislike;
this.createTime = new java.util.Date();
}
加上 Getters & Setters & toString()。
Post 请求(我正在使用 Postman 发送请求):
{
"comment" : "This is a comment",
"likes" : 123,
"dislike" : 1,
"user_id_fk" :
[
{
"id" : 1
}
]
}
在“user_id_fk”的请求中,我尝试使用 [{"id" : 1 } ] 和 { "id" : 1 },但结果是一样的。
问题:
当我从我的控制器执行完全相同的代码时,除了一切正常之外。请记住,我正在使用依赖项“spring-boot-starter-data-rest”。
此外,当我在没有 “optional = false”
的情况下执行代码时,“nullable = false”
正在将数据插入数据库,但“user_id_fk”为空 :(.
我遇到的错误:
not-null property references a null or transient value : com.citizen.citizen.entity.Post.user_id_fk;
nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : com.citizen.citizen.entity.Post.user_id_fk]
也就是说外键("user_id_fk")
为空但不应该为空。
任何帮助将不胜感激。
根据 this 文章,您应该使 user_id_fk
可为空,然后:
- 发送POST创建用户
- 发送第二个POST创建Post
- 发送 PUT 以创建两者之间的关系。
This 文章说的一样。
documentation 只提到通过关联链接处理关联。
我只是删除了依赖项“spring-boot-starter-data-rest”,我通过创建自定义 rest 解决了这个问题,一切正常。亲吻!
我正在使用带 Hibernate 的 Springboot,我想使用 POST 请求保存一个新的“post”到我的数据库。我想强调的一件事是我正在使用依赖项“spring-boot-starter-data-rest”。
数据库架构(MySQL):
Class 用户:
@Entity
@Table(name="user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id", nullable = false)
public int id;
@OneToMany(mappedBy = "user_id_fk")
public Set<Post> posts;
@Column(name="email")
private String email;
@Column(name="username")
private String username;
@Column(name="password")
private String password;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(name="create_time")
protected Date createTime;
@Column(name="type")
private String accountType;
public User() {
this.createTime = new java.util.Date();
}
public User(String email, String username, String password, String firstName, String lastName, Date createTime, String accountType) {
this.email = email;
this.username = username;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.createTime = createTime;
this.accountType = accountType;
this.createTime = new java.util.Date();
}
public User(int id, String email, String username, String password, String firstName, String lastName, Date createTime, String accountType) {
this.id = id;
this.email = email;
this.username = username;
this.password = password;
this.firstName = firstName;
this.lastName = lastName;
this.createTime = createTime;
this.accountType = accountType;
this.createTime = new java.util.Date();
}
加上 Getters & Setters & toString()。
Class Post:
@Entity
@Table(name="post")
public class Post implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id")
public int id;
@ManyToOne(optional = false)
@JoinColumn(name = "user_id_fk", nullable = false)
public User user_id_fk;
@Column(name="comment")
private String comment;
@Column(name="likes")
private int likes;
@Column(name="dislike")
private int dislike;
@Column(name="create_time")
protected Date createTime;
public Post() {
this.createTime = new java.util.Date();
}
public Post(String comment, int likes, int dislike, User user_id_fk) {
this.user_id_fk = user_id_fk;
this.comment = comment;
this.likes = likes;
this.dislike = dislike;
this.createTime = new java.util.Date();
}
public Post(int id, User user_id_fk, String comment, int likes, int dislike) {
this.id = id;
this.user_id_fk = user_id_fk;
this.comment = comment;
this.likes = likes;
this.dislike = dislike;
this.createTime = new java.util.Date();
}
加上 Getters & Setters & toString()。
Post 请求(我正在使用 Postman 发送请求):
{
"comment" : "This is a comment",
"likes" : 123,
"dislike" : 1,
"user_id_fk" :
[
{
"id" : 1
}
]
}
在“user_id_fk”的请求中,我尝试使用 [{"id" : 1 } ] 和 { "id" : 1 },但结果是一样的。
问题:
当我从我的控制器执行完全相同的代码时,除了一切正常之外。请记住,我正在使用依赖项“spring-boot-starter-data-rest”。
此外,当我在没有 “optional = false”
的情况下执行代码时,“nullable = false”
正在将数据插入数据库,但“user_id_fk”为空 :(.
我遇到的错误:
not-null property references a null or transient value : com.citizen.citizen.entity.Post.user_id_fk;
nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : com.citizen.citizen.entity.Post.user_id_fk]
也就是说外键("user_id_fk")
为空但不应该为空。
任何帮助将不胜感激。
根据 this 文章,您应该使 user_id_fk
可为空,然后:
- 发送POST创建用户
- 发送第二个POST创建Post
- 发送 PUT 以创建两者之间的关系。
This 文章说的一样。 documentation 只提到通过关联链接处理关联。
我只是删除了依赖项“spring-boot-starter-data-rest”,我通过创建自定义 rest 解决了这个问题,一切正常。亲吻!