java - @ManyToOne JPA spring mysql插入错误,列为空

标签 java mysql hibernate jpa spring-data-jpa

开始进入 Spring Boot 并使用 jpa 存储库来保存本地 mysql 数据库,但我的插入遇到了问题。我将其简化为我的用户和任务类,用户可以有一项任务,并且我在任务类中使用 @ManyToOne 注释来进行一个定向映射。当在 post man 中调用端点时,我收到错误,跟踪显示 user_id 列为空(这是来自用户的映射列)。

为了调试,我在实际的 taskRepo.save(task) 方法上的任务服务类中设置了一个断点,看起来 user_id 实际上是。我尝试了一对多的双向映射和通过选项映射的多对一的双向映射,但没有成功。

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(unique = true)
private Long id;

private Long postId;

@Column(unique = true, nullable = false)
private String username;

@Column(unique = true, nullable = false, name = "email")
private String email;

@Column(nullable = false)
private String password;





@Id
@GeneratedValue(strategy =GenerationType.IDENTITY)
@Column(name = "id", nullable = false)
private Long id;

@Column(name = "taskName", nullable = false)
private String taskName;

@Column(name = "description")
private String description;

@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private User user;






@Autowired
private TaskService taskService;

@GetMapping("/getAllTask")
public List<Task> getAllTasks(){
    return taskService.getAllTask();
}

@CrossOrigin(origins = "http://localhost:3000")
@PostMapping("/addTask")
public void addTask(@RequestBody Task task){
    taskService.addTask(task);
}



@Autowired
private TaskRepo taskRepo;

public List<Task> getAllTask(){
    List<Task> tasks = new ArrayList<>();
    taskRepo.findAll().forEach(task -> tasks.add(task));
    return tasks;
}

public void addTask(Task task){
    taskRepo.save(task);
}







{
        "taskName": "Task 1",
        "description": "This is the first item",
        "user_id": 1
    }



{
    "timestamp": "2019-04-02T02:15:06.989+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement",
    "path": "/task/addTask"
}

[enter image description here][1] ERROR 30016 --- [nio-8080-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement] with root cause

java.sql.SQLIntegrityConstraintViolationException: Column 'user_id' cannot be null

最佳答案

您的 json 格式不正确。 user_id 不是 Task 的字段。

你的 json 应该是这样的:

{
  "taskName": "Task 1",
  "description": "This is the first item",
  "user": {
    "id": 1
  }
}

user json 字段引用

@ManyToOne
@JoinColumn(name = "user_id", nullable = false)
private User user;

由于 User 是一个对象,因此需要将其表示为 JsonObject

关于java - @ManyToOne JPA spring mysql插入错误,列为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55466284/

相关文章:

java - 使用 @Where 子句的 Hibernate @OneToOne 映射

mysql - 无法选择列名 int

java - Hibernate hql/criteria 结果包含集合

java - 正确测试 Spring 服务

java - 如何在 JUnit 中模拟 ERP 系统

java - 如何在 mysql 中运行内联查询

Mysql - 如何在每周、每周的特定日期和时间将数据从一个表传输到另一个表?

c# - C 和 C++ 以外的语言中未定义行为的示例

java - Android 如何在安装后立即执行特定操作?

Java 从 JWT token 获取主题