简体   繁体   English

Spring boot hibernate 双向映射多对一不能建立关系

[英]Spring boot hibernate bidirectional mapping many to one can not established relationship

I have two entity table one category and other is subject我有两个实体表一个类别,另一个是主题

My category entity我的类别实体

@Entity
public class Category extends AuditableEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column(unique = true)
private String name;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "description_id")
private CategoryDescription description;

@OneToMany( mappedBy = "category", cascade = CascadeType.ALL, 
orphanRemoval = true)
private List<Subject> subjects;

//getter and setter
}

And my Subject entity还有我的主题实体

@Entity
public class Subject extends AuditableEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column(unique = true)
private String name;

@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "description_id")
private SubjectDescription description;

@ManyToOne(fetch = FetchType.EAGER)
private Category category;

//Getter and Setter
}

Category Repository类别库

@Repository
@Transactional
public interface CategoryRepository extends 
JpaRepository<Category, Integer> {
}

Subject Repository主题库

@Repository
@Transactional
public interface SubjectRepository extends JpaRepository<Subject, 
Integer> {
}

Category Controller类别控制器

@RestController
@RequestMapping("/category/api/")
public class CategoryResource {

private final CategoryService categoryService;
private final SubjectService subjectService;
private final TopicService topicService;

public CategoryResource(CategoryService categoryService, 
SubjectService subjectService, TopicService topicService) {
    this.categoryService = categoryService;
    this.subjectService = subjectService;
    this.topicService = topicService;
}

@PostMapping("save")
public void saveCategory(@RequestBody Category category) {

    categoryService.save(category);

}

I am using postman to save data.我正在使用邮递员来保存数据。 Problem is that after saving data to the category and subject table my subject table column category_id is null i can not established a relationship between them my sql structure and data is after saving data it shows like问题是,将数据保存到类别和主题表后,我的主题表列 category_id 为空,我无法在它们之间建立关系,我的 sql 结构和数据在保存数据后显示如下

Category table分类表

在此处输入图片说明

Subject Table主题表

在此处输入图片说明

category_id is NULL how to set category id i am trying many ways but couldn't find a solution.Please help me to solve this issue category_id is NULL 如何设置类别 id 我尝试了很多方法但找不到解决方案。请帮我解决这个问题

It's great that you are learning spring boot!很高兴你正在学习弹簧靴!

To answer your question since the answer is pretty simple, your code is missing category in subject.要回答您的问题,因为答案非常简单,您的代码缺少主题类别。

subject.setCategory(category);

Now this might cause you an exception, so make sure you save category before you persist subject.现在这可能会导致您出现异常,因此请确保在保留主题之前保存类别。

Cheers!干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM