Java 休眠多对多关系

Java 休眠多对多关系,java,sql,hibernate,jpa,spring-boot,Java,Sql,Hibernate,Jpa,Spring Boot,我试图映射我的数据库表,这样我就可以在图书和作者表之间建立多对多关系 首先,这是一对多关系设计,由于@xenteros和@Amer Qarabsa,我开始将其重新设计为多对多关系 这就是我的数据库模式的样子: 作者(模型)类别: 实体 @表(name=“author”) 公共类作者{ @身份证 @GeneratedValue(策略=GenerationType.AUTO) @列(name=“author\u id”) 私有int-id; @列(name=“name”) 私有字符串名称; @列(

我试图映射我的数据库表,这样我就可以在图书和作者表之间建立多对多关系

首先,这是一对多关系设计,由于@xenteros和@Amer Qarabsa,我开始将其重新设计为多对多关系

这就是我的数据库模式的样子:

作者(模型)类别:

实体
@表(name=“author”)
公共类作者{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
@列(name=“author\u id”)
私有int-id;
@列(name=“name”)
私有字符串名称;
@列(name=“姓氏”)
私家姓;
@列(name=“出生日期”)
私人出生日期;
@多个(级联=级联类型.ALL)
@JoinTable(name=“book\u author”,
joinColumns=@JoinColumn(name=“book\u id”),
inverseJoinColumns=@JoinColumn(name=“author\u id”))
private Set authorsBooks=new HashSet();
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getNames(){
返回姓氏;
}
public void setSurname(字符串姓氏){
this.姓氏=姓氏;
}
公共日期getDateOfBirth(){
出生返回日期;
}
公共无效设置出生日期(出生日期){
this.dateOfBirth=出生日期;
}
公共集getAuthorsBooks(){
归还作者书籍;
}
public void setAuthorsBooks(集authorsBooks){
this.authorsBooks=authorsBooks;
}
}
图书(模型)类:

@实体
@表(name=“book”)
公共课堂用书{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
@列(name=“book\u id”)
私人书号;
@列(name=“title”)
私有字符串标题;
@列(name=“title\u原件”)
私有字符串标题原件;
@列(name=“首映日”)
私人约会首映;
@多个(级联=级联类型.ALL)
@JoinTable(name=“book\u author”,joinColumns={@JoinColumn(name=“book\u id”)},inverseJoinColumns={@JoinColumn(name=“author\u id”)})
私有集=新哈希集(0);
public int getBookId(){
返回bookId;
}
公共作废退书簿(int bookId){
this.bookId=bookId;
}
公共字符串getTitle(){
返回标题;
}
公共无效集合标题(字符串标题){
this.title=标题;
}
公共字符串getTitleOriginal(){
返回标题原件;
}
public void setTitleOriginal(字符串titleOriginal){
this.titleOriginal=titleOriginal;
}
公开日期getPremiereDate(){
返回首字母;
}
公共无效设置初始日期(初始日期){
this.premiereDate=premiereDate;
}
公共集getBookAuthors(){
将此文件返回给作者;
}
公共书籍作者(集书籍作者){
this.booksAuthors=booksAuthors;
}
}
AuthorController类:

@Controller
public class AuthorController {

    @Autowired
    private AuthorRepository authorRepository;

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<Collection<Author>> getAuthors() {
        return new ResponseEntity<>(authorRepository.findAll(), HttpStatus.OK);
    }

    @RequestMapping(value = "/authors/{id}", method = RequestMethod.GET)
    public ResponseEntity<Author> getAuthor(@PathVariable int id) {
        Author author = authorRepository.findOne(id);

        if (author != null) {
            return new ResponseEntity<>(authorRepository.findOne(id), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<?> addAuthor(@RequestBody Author author) {
        return new ResponseEntity<>(authorRepository.save(author), HttpStatus.CREATED);
    }

    @RequestMapping(value = "/authors/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<Void> deleteAuthor(@PathVariable int id) {
        authorRepository.delete(id);

        return new ResponseEntity<Void>(HttpStatus.OK);
    }

    /*
    @RequestMapping(value = {"/authors"}, method = RequestMethod.GET)
    public ModelAndView allAuthors() {
        ModelAndView modelAndView = new ModelAndView("authors/home"); //viewname przekazujemy z folderu templates
        List<Author> author = authorService.getAllAuthors();
        modelAndView.addObject("authors",author);  //ta nazwa tutaj "authors" sluzy potem do wykorzystania jej w templacie
        return modelAndView;
    }
    */

}
@Controller
public class BookController {

    @Autowired
    private BookRepository bookRepository;

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<Collection<Book>> getBooks() {
        return new ResponseEntity<>(bookRepository.findAll(), HttpStatus.OK);
    }

    @RequestMapping(value = "/books/{id}", method = RequestMethod.GET)
    public ResponseEntity<Book> getBook(@PathVariable int id) {
        Book book = bookRepository.findOne(id);

        if (book != null) {
            return new ResponseEntity<>(bookRepository.findOne(id), HttpStatus.OK);
        } else {
            return new ResponseEntity<>( HttpStatus.NOT_FOUND);
        }
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<?> addBook(@RequestBody Book book) {
        return new ResponseEntity<>(bookRepository.save(book), HttpStatus.CREATED);
    }

    @RequestMapping(value = "/books/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<Void> deleteBook(@PathVariable int id) {
        bookRepository.delete(id);

        return new ResponseEntity<Void>(HttpStatus.OK);
    }

    /*
    @RequestMapping(value = {"/books"}, method = RequestMethod.GET)
    public ModelAndView allBooks() {
        ModelAndView modelAndView = new ModelAndView("books/home"); //viewname przekazujemy z folderu templates
        List<Book> book = bookService.getAllBooks();
        modelAndView.addObject("books",book);  //ta nazwa tutaj "books" sluzy potem do wykorzystania jej w templacie
        return modelAndView;
    }
    */


}
@控制器
公共类AuthorController{
@自动连线
私人作者或报告作者或报告作者;
@RequestMapping(method=RequestMethod.GET)
公众反应{
返回新的响应属性(authorRepository.findAll(),HttpStatus.OK);
}
@RequestMapping(value=“/authors/{id}”,method=RequestMethod.GET)
公共响应属性getAuthor(@PathVariable int-id){
Author=authorRepository.findOne(id);
if(author!=null){
返回新的响应属性(authorRepository.findOne(id),HttpStatus.OK);
}否则{
返回新的ResponseEntity(未找到HttpStatus.NOT_);
}
}
@RequestMapping(method=RequestMethod.POST)
公共响应地址作者(@RequestBody Author){
返回新的响应属性(authorRepository.save(author),HttpStatus.CREATED);
}
@RequestMapping(value=“/authors/{id}”,method=RequestMethod.DELETE)
公共响应属性deleteAuthor(@PathVariable int-id){
authorRepository.delete(id);
返回新的响应状态(HttpStatus.OK);
}
/*
@RequestMapping(值={“/authors”},方法=RequestMethod.GET)
公共模型和视图所有作者(){
ModelAndView ModelAndView=new ModelAndView(“作者/主页”);//viewname przekazujemy z folderu模板
List author=authorService.getAllAuthors();
modelAndView.addObject(“作者”,作者);//ta nazwa tutaj“作者”sluzy potem do wykorzystania jej w templacie
返回模型和视图;
}
*/
}
BookController类别:

@Controller
public class AuthorController {

    @Autowired
    private AuthorRepository authorRepository;

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<Collection<Author>> getAuthors() {
        return new ResponseEntity<>(authorRepository.findAll(), HttpStatus.OK);
    }

    @RequestMapping(value = "/authors/{id}", method = RequestMethod.GET)
    public ResponseEntity<Author> getAuthor(@PathVariable int id) {
        Author author = authorRepository.findOne(id);

        if (author != null) {
            return new ResponseEntity<>(authorRepository.findOne(id), HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<?> addAuthor(@RequestBody Author author) {
        return new ResponseEntity<>(authorRepository.save(author), HttpStatus.CREATED);
    }

    @RequestMapping(value = "/authors/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<Void> deleteAuthor(@PathVariable int id) {
        authorRepository.delete(id);

        return new ResponseEntity<Void>(HttpStatus.OK);
    }

    /*
    @RequestMapping(value = {"/authors"}, method = RequestMethod.GET)
    public ModelAndView allAuthors() {
        ModelAndView modelAndView = new ModelAndView("authors/home"); //viewname przekazujemy z folderu templates
        List<Author> author = authorService.getAllAuthors();
        modelAndView.addObject("authors",author);  //ta nazwa tutaj "authors" sluzy potem do wykorzystania jej w templacie
        return modelAndView;
    }
    */

}
@Controller
public class BookController {

    @Autowired
    private BookRepository bookRepository;

    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<Collection<Book>> getBooks() {
        return new ResponseEntity<>(bookRepository.findAll(), HttpStatus.OK);
    }

    @RequestMapping(value = "/books/{id}", method = RequestMethod.GET)
    public ResponseEntity<Book> getBook(@PathVariable int id) {
        Book book = bookRepository.findOne(id);

        if (book != null) {
            return new ResponseEntity<>(bookRepository.findOne(id), HttpStatus.OK);
        } else {
            return new ResponseEntity<>( HttpStatus.NOT_FOUND);
        }
    }

    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<?> addBook(@RequestBody Book book) {
        return new ResponseEntity<>(bookRepository.save(book), HttpStatus.CREATED);
    }

    @RequestMapping(value = "/books/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<Void> deleteBook(@PathVariable int id) {
        bookRepository.delete(id);

        return new ResponseEntity<Void>(HttpStatus.OK);
    }

    /*
    @RequestMapping(value = {"/books"}, method = RequestMethod.GET)
    public ModelAndView allBooks() {
        ModelAndView modelAndView = new ModelAndView("books/home"); //viewname przekazujemy z folderu templates
        List<Book> book = bookService.getAllBooks();
        modelAndView.addObject("books",book);  //ta nazwa tutaj "books" sluzy potem do wykorzystania jej w templacie
        return modelAndView;
    }
    */


}
@控制器
公共类图书管理员{
@自动连线
私人书库;
@RequestMapping(method=RequestMethod.GET)
公共响应getBooks(){
返回新的响应属性(bookRepository.findAll(),HttpStatus.OK);
}
@RequestMapping(value=“/books/{id}”,method=RequestMethod.GET)
公共响应属性getBook(@PathVariable int-id){
Book Book=bookRepository.findOne(id);
if(book!=null){
返回新的响应属性(bookRepository.findOne(id),HttpStatus.OK);
}否则{
返回新的ResponseEntity(未找到HttpStatus.NOT_);
}
}
@RequestMapping(method=RequestMethod.POST)
公共响应地址簿(@RequestBody Book){
返回新的响应属性(bookRepository.save(book),HttpStatus.CREATED);
}
@RequestMapping(value=“/books/{id}”,method=RequestMethod.DELETE)
公共响应属性deleteBook(@PathVariable int-id){
bookRepository.delete(id);
返回新的响应状态(HttpStatus.OK);
}
/*
@RequestMapping(值={“/books”},m
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-08-07 13:02:18.638 ERROR 6812 --- [  restartedMain] o.s.boot.SpringApplication               : Application startup failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerMapping' defined in class path resource [org/springframework/web/servlet/config/annotation/DelegatingWebMvcConfiguration.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'bookController' method 
public org.springframework.http.ResponseEntity<?> eu.fitk.controller.BookController.addBook(eu.fitk.model.Book)
to {[],methods=[POST]}: There is already 'authorController' bean method
public org.springframework.http.ResponseEntity<?> eu.fitk.controller.AuthorController.addAuthor(eu.fitk.model.Author) mapped.
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:867) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:543) ~[spring-context-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107) [spring-boot-1.5.6.RELEASE.jar:1.5.6.RELEASE]
    at eu.fitk.BookwebApplication.main(BookwebApplication.java:9) [classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_73]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_73]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_73]
    at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_73]
    at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-1.5.6.RELEASE.jar:1.5.6.RELEASE]
Caused by: java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'bookController' method 
public org.springframework.http.ResponseEntity<?> eu.fitk.controller.BookController.addBook(eu.fitk.model.Book)
to {[],methods=[POST]}: There is already 'authorController' bean method
public org.springframework.http.ResponseEntity<?> eu.fitk.controller.AuthorController.addAuthor(eu.fitk.model.Author) mapped.
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.assertUniqueMethodMapping(AbstractHandlerMethodMapping.java:576) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping$MappingRegistry.register(AbstractHandlerMethodMapping.java:540) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.registerHandlerMethod(AbstractHandlerMethodMapping.java:264) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.detectHandlerMethods(AbstractHandlerMethodMapping.java:250) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.initHandlerMethods(AbstractHandlerMethodMapping.java:214) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.servlet.handler.AbstractHandlerMethodMapping.afterPropertiesSet(AbstractHandlerMethodMapping.java:184) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.afterPropertiesSet(RequestMappingHandlerMapping.java:127) ~[spring-webmvc-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.10.RELEASE.jar:4.3.10.RELEASE]
    ... 21 common frames omitted


Process finished with exit code 0
    @JoinTable(name = "book_author", joinColumns = { @JoinColumn(name = "book_id") }, inverseJoinColumns = { @JoinColumn(name = "author_id") })
@RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<?> addBook(@RequestBody Book book) {
        return new ResponseEntity<>(bookRepository.save(book), HttpStatus.CREATED);
    }

@RequestMapping(value="/addauthor", method = RequestMethod.POST)
    public ResponseEntity<?> addAuthor(@RequestBody Author author) {
        return new ResponseEntity<>(authorRepository.save(author), HttpStatus.CREATED);
    }
@RequestMapping(value="/addauthor", method = RequestMethod.POST)
    public ResponseEntity<?> addAuthor(@RequestBody Author author) {
        return new ResponseEntity<>(authorRepository.save(author), HttpStatus.CREATED);
    }
@RequestMapping(value = "/addbook", method = RequestMethod.POST)
    public ResponseEntity<?> addBook(@RequestBody Book book) {
        return new ResponseEntity<>(bookRepository.save(book), HttpStatus.CREATED);
    }