Spring boot 在MySQL数据库中使用Spring Boot Rest API的一对一映射
我试图在SpringBootRESTAPI中创建一个嵌套自定义类型。我已经创建了Book.java实体类Spring boot 在MySQL数据库中使用Spring Boot Rest API的一对一映射,spring-boot,api,hibernate,rest,jpa,Spring Boot,Api,Hibernate,Rest,Jpa,我试图在SpringBootRESTAPI中创建一个嵌套自定义类型。我已经创建了Book.java实体类 包com.api.book.bootrestbook.entities; 导入javax.persistence.CascadeType; 导入javax.persistence.Column; 导入javax.persistence.Entity; 导入javax.persistence.GeneratedValue; 导入javax.persistence.GenerationType;
包com.api.book.bootrestbook.entities;
导入javax.persistence.CascadeType;
导入javax.persistence.Column;
导入javax.persistence.Entity;
导入javax.persistence.GeneratedValue;
导入javax.persistence.GenerationType;
导入javax.persistence.Id;
导入javax.persistence.OneToOne;
导入javax.persistence.Table;
@实体
@表(name=“Book”)
公共课堂用书{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
@列(name=“Book\u ID”)
私有int-id;
私有字符串标题;
@OneToOne(级联=级联类型.ALL)
私人作者;
}
Author.java
包com.api.book.bootrestbook.entities;
导入javax.persistence.Column;
导入javax.persistence.Entity;
导入javax.persistence.GeneratedValue;
导入javax.persistence.GenerationType;
导入javax.persistence.Id;
导入javax.persistence.Table;
@实体
@表(name=“Author”)
公共类作者{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私人int-authorId;
@列(name=“first_name”)
私有字符串名;
私有字符串lastName;
私有字符串语言;
}
我正在尝试将Book类author数据成员映射到author类对象
这是我的handler方法,用于在数据库表中填写请求中的详细信息
@PostMapping(“/books”)
公共书籍addBook(@RequestBody Book){
this.bookServiceJPA.addBook(book);
还书;
}
这是我的BookServiceJPA课程-
package com.api.book.bootrestbook.services;
导入java.util.List;
导入javax.sound.sampled.Line;
导入com.api.book.bootrestbook.dao.BookRepository;
导入com.api.book.bootrestbook.entities.book;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.stereotype.Component;
@组成部分
公共类图书服务JPA{
@自动连线
私人书库;
//为post请求添加一本书,为此我们必须使用post方法
公共无效地址簿(b册){
这个.bookRepository.save(b);
System.out.println(“保存的书籍详细信息”);
返回;
}
}
这是我的BookRepository界面-
import org.springframework.data.repository.crudepository;
公共接口BookRepository扩展了Crudepository{
公共图书findById(int-id);
}
在运行我的应用程序之后,它正在运行,并且也创建了表,但当我尝试发出请求时
我的代码在控制台中显示此错误-
2021-04-13 00:16:47.581信息9404-[nio-8080-exec-2]o.a.c.c.c.[Tomcat].[localhost].[/]:初始化Spring DispatcherServlet'DispatcherServlet'
2021-04-13 00:16:47.583信息9404---[nio-8080-exec-2]o.s.web.servlet.DispatcherServlet:初始化servlet'DispatcherServlet'
2021-04-13 00:16:47.591信息9404---[nio-8080-exec-2]o.s.web.servlet.DispatcherServlet:在4毫秒内完成初始化
2021-04-13 00:16:48.032错误9404---[nio-8080-exec-2]o.a.c.c.c.[/].[dispatcherServlet]:路径[]上下文中Servlet[dispatcherServlet]的Servlet.service()引发异常[请求处理失败;嵌套异常为org.springframework.dao.InvalidDataAccessApiUsageException:传递到persist的分离实体:com.api.book.bootrestbook.entities.Author;嵌套异常为org.hibernate.PersistentObjectException:传递到persist的分离实体:com.api.book.bootrestbook.entities.Author],具有根本原因
org.hibernate.PersistentObjectException:传递给persist的分离实体:com.api.book.bootrestbook.entities.Author
在org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:120)~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
在org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:104)~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
在org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:744)~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
在org.hibernate.internal.SessionImpl.persist(SessionImpl.java:712)~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
在org.hibernate.engine.spi.CascadingActions$7.cascade(CascadingActions.java:298)~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
在org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:492)~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
在org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:416)~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
在org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:218)~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
在org.hibernate.engine.internal.Cascade.Cascade(Cascade.java:151)~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
在org.hibernate.event.internal.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:427)~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
在org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:264)~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
在org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:193)~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
在org.hibernate.event.internal.AbstractSaveEventListener.SaveWithGenerateId(AbstractSaveEventListener.java:135)~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
在org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:185)~[hibernate-core-5.4.29.Final.jar:5.4.29.Final]
{
"title": "Java",
"author": {
"firstName": "Mayank",
"lastName": "kumar",
"language": "English"
}
}
@Entity
@Table(name = "Author")
public class Author {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int authorId;
@Column(name = "first_name")
private String firstName;
@Column(name = “last_name")
private String lastName;
@Column(name = “language”)
private String language;
@OneToOne(
mappedBy = “book”,
orphanRemoval = true,
cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE },
fetch = FetchType.EAGER
)
private Book book;
}
@Entity
@Table(name = "Book")
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "Book_ID")
private int id;
@Column(name = “title”)
private String title;
@JoinColumn(
name = “author_id",
referencedColumnName = "id",
foreignKey = @ForeignKey(name = “author_book_fk")
)
private Author author;
}
public Book save(String bookTitle, Long authorId) {
Author author = authorService.findById(authorId); // should be formatted to 1L, 2L, etc…
Book book = new Book();
book.setTitle(bookTitle);
book.setAuthor(author);
bookRepository.save(book);
}