配置set的时候,inverse="true"一般要写在某一边吗?如果 Book 和 Author 多对多,写在 Author 这边的话: Book book1 = new Book();
book1.setId(1L);
book1.setName("Hibernate In Action");
book1.setPrice(54.30F);
book1.setPublishDate(new Date());
book1.setRe("Manning In Action Sections");
Book book2 = new Book();
book2.setId(2L);
book2.setName("jQuery实战");
book2.setPrice(25.90F);
book2.setPublishDate(new Date());
book2.setRe("Manning In Action Sections");
Author author1 = new Author();
author1.setId(1L);
author1.setName("zhangsan");
Author author2 = new Author();
author2.setId(2L);
author2.setName("lisi");
Set<Author> authors = new HashSet<Author>();
authors.add(author1);
authors.add(author2);
book1.setAuthors(authors);
Set<Book> books = new HashSet<Book>();
books.add(book1);
books.add(book2);
author1.setBooks(books);
session.save(book1);
这样存了之后,取 author1 的话不会有 book2 相关的信息。
Author au = (Author)session.load(Author.class, new Long(1L));
如果两边都不写inverse="true",数据库的数据就会重复。应该怎么弄?
book1.setId(1L);
book1.setName("Hibernate In Action");
book1.setPrice(54.30F);
book1.setPublishDate(new Date());
book1.setRe("Manning In Action Sections");
Book book2 = new Book();
book2.setId(2L);
book2.setName("jQuery实战");
book2.setPrice(25.90F);
book2.setPublishDate(new Date());
book2.setRe("Manning In Action Sections");
Author author1 = new Author();
author1.setId(1L);
author1.setName("zhangsan");
Author author2 = new Author();
author2.setId(2L);
author2.setName("lisi");
Set<Author> authors = new HashSet<Author>();
authors.add(author1);
authors.add(author2);
book1.setAuthors(authors);
Set<Book> books = new HashSet<Book>();
books.add(book1);
books.add(book2);
author1.setBooks(books);
session.save(book1);
这样存了之后,取 author1 的话不会有 book2 相关的信息。
Author au = (Author)session.load(Author.class, new Long(1L));
如果两边都不写inverse="true",数据库的数据就会重复。应该怎么弄?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货