为了账号安全,请及时绑定邮箱和手机立即绑定

JPA ManyToMany链接错误的实体

JPA ManyToMany链接错误的实体

至尊宝的传说 2021-05-04 13:47:56
假设我们有4个实体:电影,工作室,网站和标签。电影,工作室和网站可以具有0个或多个标签(每个标签具有0到多个标签)。电影有0或1个站点(一个站点有0到许多电影)。电影有0个或多个制片厂(一个电影制片厂有0到许多电影)。我通过如下介绍TagCollection和StudioCollection实现了这些关系。TagCollection具有与Movie的oneToOne映射,以及具有Tag的manyToMany(与StudioCollection相同)。我这样做是因为:我希望所有标签/工作室都转到同一个表格,无论它们附加到哪个实体链接表将具有一个返回给拥有实体的唯一ID,这意味着当我查询该关系时,我不必指定标签是用于电影,网站还是摄影棚:该ID是唯一的,并且仅存在1个实体。该计划失败了,因为当我保存带有某些标签的电影并进行检索时,这些标签将链接到制片厂或除原始所有者以外的任何其他实体。测试如下(随后是代码)。知道我在做什么错吗?我怀疑链接表中的ID是错误的,但是我无法证明这一点,也看不出为什么会发生这种情况(这不是由于输入错误或不良影响所致)。如果需要更多内容,请在此处发布完整代码,但以下是我的实现和测试失败的一部分。谢谢你的时间。@Test    public void addedMoviesWithAttributesArePersisted() {    final MovieBuilder mb = new MovieBuilder();    mb.title("Title");    // Add a site to the movie    final Site site = new Site("Site TEst");    mb.site(site);    //Add 2 studios to the movie    final Studio studio1 = new Studio("Studio 1");    final Studio studio2 = new Studio("Studio 2");    final StudioCollection studioCollection = new StudioCollection();    studioCollection.add(Sets.newSet(studio1, studio2));    mb.studioCollections(studioCollection);    // Add 2 tags to the movie    final Tag tag1 = new Tag("Tag 1");    final Tag tag2 = new Tag("Tag 2");    final TagCollection tagCollection = new TagCollection();    tagCollection.add(Sets.newSet(tag1, tag2));    mb.tagCollection(tagCollection);    final Movie movie = mb.build();    this.underTest.persist(movie);    final Set<Movie> foundMovies = this.underTest.findByTitle("Title");    assertFalse(foundMovies.isEmpty());    assertEquals(1, foundMovies.size());    assertTrue(foundMovies.contains(movie));    final Movie foundMovie = foundMovies.iterator().next();    // This fails    assertFalse(foundMovie.getTagCollection().isEmpty());
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 157 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信