Jay*_*667 5 hibernate jpa eclipselink jakarta-ee
假设我们有两个(或更多)类,其中一个是@ManyToMany-引用其他类:(为了简化,我在这里省略了很多注释)
@Entity
class Newspaper {
@Id long id;
@ManyToMany Set<Author> authors = new HashSet<>();
@ManyToMany Set<Article> oldArticles = new HashSet<>();
@ManyToMany Set<Article> newArticles = new HashSet<>();
}
@Entity
class Article {
@Id long id;
}
@Entity
class Author {
@Id long id;
}
Run Code Online (Sandbox Code Playgroud)
现在默认情况下,JPA 将创建两个表:
Newspaper_Author
Newspaper_Article
Run Code Online (Sandbox Code Playgroud)
甚至将oldArticles和混合newArticles到同一张表中,产生有趣的结果;-)
@JoinTable现在,可以通过定义至少一个或所有成员变量来轻松解决此问题:
@Entity
class Newspaper {
@Id long id;
@ManyToMany Set<Author> authors = new HashSet<>();
@ManyToMany Set<Article> oldArticles = new HashSet<>();
@ManyToMany @JoinTable(name = "Newspaper_newArticles") Set<Article> newArticles = new HashSet<>();
}
Run Code Online (Sandbox Code Playgroud)
所以,最后回答我的问题: 当只有这样定义的类时
@Entity
class Newspaper {
@Id long id;
@ManyToMany Set<Author> authors = new HashSet<>();
@ManyToMany Set<Article> oldArticles = new HashSet<>();
@ManyToMany Set<Article> newArticles = new HashSet<>();
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让JPA自动建表
Newspaper_authors
Newspaper_oldArticles
Newspaper_newArticles
Run Code Online (Sandbox Code Playgroud)
更新: ......并使问题变得非常疯狂:
@Entity
class Newspaper {
@Id long id;
@ManyToMany Set<Author> authors = new HashSet<>();
@ManyToMany Set<OldArticle> oldArticles = new HashSet<>();
@ManyToMany Set<NewArticle> newArticles = new HashSet<>();
}
@MappedSuperclass
class Article {
@Id long id;
@ManyToMany Set<Author> authors = new HashSet<>();
}
@Entity
class OldArticle extends Article { /* */ }
@Entity
class NewArticle extends Article { /* */ }
@Entity
class Author {
@Id long id;
}
Run Code Online (Sandbox Code Playgroud)
我怎么可能在这里定义名称Article.authors?
对于您的情况,我建议对所有类型的文章使用@Inheritance和单个表,而不是@MappedSuperclass:
@Data
@NoArgsConstructor
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
public abstract class Article {
@Id
@GeneratedValue
private Integer id;
private String name;
@ManyToMany
private Set<Newspaper> newspapers;
@ManyToMany
private Set<Author> authors;
public Article(String name, Set<Newspaper> newspapers, Set<Author> authors) {
this.name = name;
this.newspapers = newspapers;
this.authors = authors;
}
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Run Code Online (Sandbox Code Playgroud)
所有类型的文章都将存储在单个表中,您可以通过注释type中设置的列来确定它们的类型@DiscriminatorColumn。
然后您将能够在Newspaper实体中使用一组文章:
@Data
@NoArgsConstructor
@Entity
public class Newspaper {
@Id
@GeneratedValue
private Integer id;
private String name;
@ManyToMany(mappedBy = "newspapers")
private Set<Author> authors;
@ManyToMany(mappedBy = "newspapers")
private Set<Article> articles;
public Newspaper(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
注意在使用双向的情况下必须使用的参数mappedBy ManyToMany。
具体文章:
@NoArgsConstructor
@Entity
@DiscriminatorValue("FIRST")
public class FirstTypeArticle extends Article {
public FirstTypeArticle(String name, Set<Newspaper> newspapers, Set<Author> authors) {
super(name, newspapers, authors);
}
}
@NoArgsConstructor
@Entity
@DiscriminatorValue("SECOND")
public class SecondTypeArticle extends Article {
public SecondTypeArticle(String name, Set<Newspaper> newspapers, Set<Author> authors) {
super(name, newspapers, authors);
}
}
Run Code Online (Sandbox Code Playgroud)
注意注释@DiscriminatorValue,它用于设置鉴别器列的值。
作者实体(也包含一组文章):
@Data
@NoArgsConstructor
@Entity
public class Author {
@Id
@GeneratedValue
private Integer id;
private String name;
@ManyToMany
private Set<Newspaper> newspapers;
@ManyToMany(mappedBy = "authors")
private Set<Article> articles;
public Author(String name, Set<Newspaper> newspapers) {
this.name = name;
this.newspapers = newspapers;
}
}
Run Code Online (Sandbox Code Playgroud)
对于我的 Spring Boot 2.1.1 演示项目(带有 H2 数据库)中的这些实体集,Hibernate 创建了下表,无需任何其他设置:
ARTICLE
ARTICLE_AUTHORS
ARTICLE_NEWSPAPERS
AUTHOR
AUTHOR_NEWSPAPERS
NEWSPAPER
Run Code Online (Sandbox Code Playgroud)
存储库:
public interface ArticleRepo extends JpaRepository<Article, Integer> {
}
public interface AuthorRepo extends JpaRepository<Author, Integer> {
}
public interface NewspaperRepo extends JpaRepository<Newspaper, Integer> {
}
Run Code Online (Sandbox Code Playgroud)
使用示例:
@RunWith(SpringRunner.class)
@DataJpaTest
public class ArticleRepoTest {
@Autowired private ArticleRepo articleRepo;
@Autowired private AuthorRepo authorRepo;
@Autowired private NewspaperRepo newspaperRepo;
private List<Article> articles;
@Before
public void setUp() throws Exception {
List<Newspaper> newspapers = newspaperRepo.saveAll(List.of(
new Newspaper("newspaper1"),
new Newspaper("newspaper2")
));
List<Author> authors = authorRepo.saveAll(List.of(
new Author("author1", new HashSet<>(newspapers)),
new Author("author2", new HashSet<>(newspapers))
));
articles = articleRepo.saveAll(List.of(
new FirstTypeArticle("first type article", new HashSet<>(newspapers), new HashSet<>(authors)),
new SecondTypeArticle("second type article", new HashSet<>(newspapers), new HashSet<>(authors))
));
}
@Test
public void findAll() {
List<Article> result = articleRepo.findAll();
result.forEach(System.out::println);
assertThat(result)
.hasSize(2)
.containsAll(articles);
}
}
Run Code Online (Sandbox Code Playgroud)
更新
我个人不喜欢使用@Inheritance...
我也尝试避免使用mappedBy功能,因为我既不需要双向寻址...
当然,您可以使用@MappedSuperclass而不是@Inheritancein Article。并且您可以避免mappedBy并使用单向ManyToMany。
但在这种情况下,您将不得不保存独立实体,例如Author和Articlethrough Newspaperonly(请参阅cascade = CascadeType.MERGE参数)。对于我来说,这是相当不方便的(我试图用实用方法addAuthors和来中和它addArticles):
@Data
@NoArgsConstructor
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type")
public abstract class Article {
@Id
@GeneratedValue
private Integer id;
private String name;
@ManyToMany
private Set<Newspaper> newspapers;
@ManyToMany
private Set<Author> authors;
public Article(String name, Set<Newspaper> newspapers, Set<Author> authors) {
this.name = name;
this.newspapers = newspapers;
this.authors = authors;
}
@Override
public String toString() {
return getClass().getSimpleName() + "{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
Run Code Online (Sandbox Code Playgroud)
@Data
@NoArgsConstructor
@Entity
public class Newspaper {
@Id
@GeneratedValue
private Integer id;
private String name;
@ManyToMany(mappedBy = "newspapers")
private Set<Author> authors;
@ManyToMany(mappedBy = "newspapers")
private Set<Article> articles;
public Newspaper(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
@NoArgsConstructor
@Entity
@DiscriminatorValue("FIRST")
public class FirstTypeArticle extends Article {
public FirstTypeArticle(String name, Set<Newspaper> newspapers, Set<Author> authors) {
super(name, newspapers, authors);
}
}
@NoArgsConstructor
@Entity
@DiscriminatorValue("SECOND")
public class SecondTypeArticle extends Article {
public SecondTypeArticle(String name, Set<Newspaper> newspapers, Set<Author> authors) {
super(name, newspapers, authors);
}
}
Run Code Online (Sandbox Code Playgroud)
然后您将得到以下自动生成的表:
AUTHOR
FIRST_TYPE_ARTICLE
FIRST_TYPE_ARTICLE_AUTHORS
SECOND_TYPE_ARTICLE
SECOND_TYPE_ARTICLE_AUTHORS
NEWSPAPER
NEWSPAPER_AUTHORS
NEWSPAPER_FIRST_TYPE_ARTICLES
NEWSPAPER_SECOND_TYPE_ARTICLES
Run Code Online (Sandbox Code Playgroud)
使用示例
添加报纸:
newspapers = newspaperRepo.saveAll(List.of(
new Newspaper("newspaper1"),
new Newspaper("newspaper2")
));
Run Code Online (Sandbox Code Playgroud)
添加作者:
newspaperRepo.save(newspapers.get(0).addAuthors(
new Author("author1"),
new Author("author2")
));
Run Code Online (Sandbox Code Playgroud)
获取作者:
authors = authorRepo.findAll();
Run Code Online (Sandbox Code Playgroud)
添加文章:
newspaperRepo.save(newspapers.get(0).addArticles(
new FirstTypeArticle("article1", authors.get(0), authors.get(1)),
new SecondTypeArticle("article2", authors.get(1))
));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2362 次 |
| 最近记录: |