JavaSpring错误:NOTNULL属性在多对一关系中引用null或瞬时值
我有三个实体:FamilyMember、Comment和PersonalPage。一个家庭成员可以发布多条评论,而一条评论属于一个家庭成员,因此这是一种多对一关系。此外,一个个人页面可以有许多评论,但一条评论属于一个个人页面。所以这也是一种多对一的关系。 我用JavaSpring编写了所有这些数据库结构,以创建一个API。当我尝试执行post以保存评论时,会显示以下错误: not null属性引用空值或临时值:com.coronahelp.entity.Comment.familyMember 我寻找了一个解决方案,但没有找到任何与我的问题类似的答案。 下面是所涉及的类的代码(请记住,家族成员是从Person扩展而来的)。家庭成员的Id是一个字符串JavaSpring错误:NOTNULL属性在多对一关系中引用null或瞬时值,java,spring,hibernate,jpa,Java,Spring,Hibernate,Jpa,我有三个实体:FamilyMember、Comment和PersonalPage。一个家庭成员可以发布多条评论,而一条评论属于一个家庭成员,因此这是一种多对一关系。此外,一个个人页面可以有许多评论,但一条评论属于一个个人页面。所以这也是一种多对一的关系。 我用JavaSpring编写了所有这些数据库结构,以创建一个API。当我尝试执行post以保存评论时,会显示以下错误: not null属性引用空值或临时值:com.coronahelp.entity.Comment.familyMember
package com.coronahelp.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
@Data
@AllArgsConstructor
@NoArgsConstructor
@MappedSuperclass
public abstract class Person {
@Id
private String id;
private String name;
private String surname;
private String secondSurname;
public String getId() {
return id;
}
public void setId(String id) {
if(id.trim().length() > 0)
{
this.id = id;
}
}
public String getName() {
return name;
}
public void setName(String name) {
if(name.trim().length() > 0)
{
this.name= name;
}
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
if(surname.trim().length() > 0)
{
this.surname= surname;
}
}
public String getSecondSurname() {
return secondSurname;
}
public void setSecondSurname(String secondSurname) {
if(secondSurname.trim().length() > 0)
{
this.secondSurname = secondSurname;
}
}
}
以下是存储库、服务和控制器的代码
package com.coronahelp.repository;
import com.coronahelp.entity.Comment;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface CommentRepository extends JpaRepository<Comment, Integer> {
}
最后,这是我和邮递员写的帖子,请检查是否正确。请记住,我试图保存一条评论:
http://localhost:8787/saveComment
{
"familyMember": {
"id": "2"
},
"personalPage" : {
"id": 1
},
"date": "2021-05-16",
"text": "my comment"
}
请帮我解决这个问题。如果解决办法显而易见,不要对我太苛刻。我刚开始学春天。提前感谢。您希望在保存新评论时创建FamilyMember,还是在添加新评论时FamilyMember应该已经存在?FamilyMember应该已经存在。在数据库中,注释表的列family_member_id应为外键。exception comment.familyMember中的。family member为null或在数据库中尚不存在。你能在
returnrepository.save(comment)上设置一个断点吗代码>,运行您的邮递员,在到达断点时查看哪一个是这种情况?我在该行放置了一个断点。它将familyMember和personalPage显示为空。罕见的是,我的数据库中确实有一个Id为2的家庭成员和一个Id为1的个人页面。似乎无法找到请求的家庭成员和个人页面。这可能意味着您的json反序列化不会在@PostMapping中收到的评论中创建成员familyMember和personalPage
package com.coronahelp.entity;
import javax.persistence.*;
import java.util.Date;
@Entity
public class Comment {
public Comment()
{
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@ManyToOne
@JoinColumn(name="family_member_id", nullable = false)
private FamilyMember familyMember;
@ManyToOne
@JoinColumn(name="personal_page", nullable = false)
private PersonalPage personalPage;
private Date date;
private String text;
public int getId()
{
return id;
}
public String getDate()
{
return date.toString();
}
public void setDate(Date date) {
this.date = date;
}
public void setText(String text)
{
if(text.trim().length() > 0)
{
this.text = text;
}
}
public String getText()
{
return text;
}
}
package com.coronahelp.repository;
import com.coronahelp.entity.Comment;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface CommentRepository extends JpaRepository<Comment, Integer> {
}
package com.coronahelp.service;
import com.coronahelp.entity.Comment;
import com.coronahelp.repository.CommentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CommentService {
@Autowired
private CommentRepository repository;
public Comment saveComment(Comment comment)
{
return repository.save(comment);
}
//other CRUD operations
}
package com.coronahelp.controllers;
import com.coronahelp.entity.Comment;
import com.coronahelp.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
public class CommentController {
@Autowired
private CommentService service;
@PostMapping("/saveComment")
public Comment saveComment(@RequestBody Comment comment)
{
return service.saveComment(comment);
}
//other CRUD operations
}
{
"familyMember": {
"id": "2"
},
"personalPage" : {
"id": 1
},
"date": "2021-05-16",
"text": "my comment"
}