简体   繁体   English

父实体的主键未在子实体中存储为外键

[英]Primary key of Parent Entity not stored as Foreign Key in Child Entity

I am using OneToMany relationship in spring data Jpa and testing the api using postMan 我在spring数据Jpa中使用OneToMany关系并使用postMan测试api

@Entity
@Table(name = "book_category")
public class BookCategory {
ParentEntity
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "bookCat_id")
    private Long id;

    private String name;

    @OneToMany(cascade = CascadeType.ALL, mappedBy="bookCategory")
    private Set<Book> books;

    public BookCategory(String name, Set<Book> books) {
        this.name = name;
        this.books = books;
    }

// getter and setter

}

ChildEntity


    @Entity
    @Table(name = "book")
    public class Book {

        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name = "book_id")
        private Long bookId;

        private String name;

        @ManyToOne(cascade = CascadeType.PERSIST)
        @JoinColumn(name = "bookCat_id")
        BookCategory bookCategory;

        public Book() {

        }

    //getter  and Setter

    }

ControllerClass ControllerClass

@RestController
@RequestMapping(value = "/bookCategory")
public class BookController {

    @Autowired
    BookCategoryRepository bookCategoryRepository;

    @Autowired
    BookRepository bookRepository;

    @PostMapping("/addBookCategory")
    public BookCategory savePerson(@Valid @RequestBody BookCategory bookCategory){
        return bookCategoryRepository.save(bookCategory);
    }
}

calling Rest Api from postman and passing json as 从邮递员那里打电话给Rest Api并传递json为

{

    "name":"Category 1",
    "books":[
          {"name" : "Hello Koding 1"},
          {"name":"Hello Koding 2"}
        ]
} 

Following Query is executing by hibernate query is also correct while i am calling rest point the thing Hibernate: insert into book_category (name) values (?) Hibernate: insert into book (book_cat_id, name) values (?, ?) 以下查询是通过hibernate执行查询也是正确的,而我正在调用休息点的东西Hibernate:插入book_category(name)值(?)Hibernate:插入book(book_cat_id,name)值(?,?)

it is not inserting book_cat_id, in book_cat_id it is passing null so null is getting store 它没有插入book_cat_id,在book_cat_id中传递null,因此null获取存储

Data stored in database book_category Parent Table in database 数据库中存储的数据book_category数据库中的父表

book(ChildTable) book(ChildTable) I want to get Child Table Like I want my table like this book(ChildTable) 书(ChildTable)我想得到Child Table就像我想要这样的表

The problem is that you are not setting parent in child object. 问题是您没有在子对象中设置父级。 Somewhere in the code you should call the 代码中的某处你应该调用

public void setBookCategory(BookCategory bookCategory) { ... }

method of the Book entity. Book实体的方法。

I'd suggest to resolve this issue by using DTOs in controller and mapping them to entities in service layer, as using peristent entities as parameters of http requests can lead to serious security vulnerabilities as explained here . 我建议在使用控制器DTO的,并将它们映射到业务层实体,如使用peristent实体来解决这一问题作为HTTP请求的参数可能会导致严重的安全漏洞作为解释在这里

EDIT: Alternatively, even thought I would not encourage this solution would be to modify the savePerson method like this 编辑:或者,甚至认为我不鼓励这个解决方案是修改像这样的savePerson方法

@PostMapping("/addBookCategory")
public BookCategory savePerson(@Valid @RequestBody BookCategory bookCategory){
    bookCategory.getBooks().forEach(book -> book.setBookCategory(bookCategory));
    return bookCategoryRepository.save(bookCategory);
}

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

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