gpt4 book ai didi

java - hibernate什么时候加载映射关系

转载 作者:行者123 更新时间:2023-12-02 11:16:48 25 4
gpt4 key购买 nike

我正在创建一个 Spring Boot 应用程序,并使用它的 JpaRepository 接口(interface)构建来存储我的实体。我有以下两个实体(为了可读性删除了 getter 和 setter):

个人资料实体

@Entity
public class Profile {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@OneToMany(mappedBy = "profileOne", orphanRemoval = true)
private List<Match> matchOnes;

@OneToMany(mappedBy = "profileTwo", orphanRemoval = true)
private List<Match> matchTwos;
}

匹配实体

@Entity
@Table(uniqueConstraints={
@UniqueConstraint(columnNames = { "profileOne_id", "profileTwo_id" })
})
public class Match {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;

@ManyToOne
@JoinColumn(name = "profileOne_id")
private Profile profileOne;

@ManyToOne
@JoinColumn(name = "profileTwo_id")
private Profile profileTwo;
}

为了理解 JpaRepository 行为,我编写了以下单元测试。

@RunWith(SpringRunner.class)
@DataJpaTest
public class IProfileDaoTest {

@Autowired
private IProfileDao profileDao; //This class extends JpaRepository<Profile, long>

@Autowired
private IMatchDao matchDao; //This class extends JpaRepository<Match, long>

@Test
public void saveProfileTest() throws Exception {

@Test
public void profileMatchRelationTest() throws Exception {
//Test if matches stored by the IMatchDao are retrievable from the IProfileDao
Profile profileOne = new Profile("Bob"),
profileTwo = new Profile("Alex");
profileDao.saveAndFlush(profileOne);
profileDao.saveAndFlush(profileTwo);
matchDao.saveAndFlush(new Match(profileOne, profileTwo));
profileOne = profileDao.getOne(profileOne.getId());
Assert.assertEquals("Match not retrievable by profile.", 1, profileOne.getMatchOnes().size());
}
}

现在我预计匹配项会出现在个人资料实体中,但事实并非如此。我还尝试将 CascadeType.ALL 添加到匹配实体中的 @ManyToOne 注释,并将 FetchType.EAGER 添加到 @OneToMany 配置文件实体中的注释。

是否可以通过请求 profileDao 中的配置文件来获取用 matchDao 保存的比赛?或者我应该找到具有单独功能的配置文件的匹配项?

最佳答案

出于性能(可能还有其他)原因,Spring Data 存储库不会立即写入数据库。在测试中,如果您需要测试查询方法,则需要使用 @DataJpaTest 提供的 TestEntityManager (它只是存储库在后台使用的实体管理器,但有一些方便的测试方法)。

更新 1:匹配项不会添加到个人资料中。为了确保关系是双向的,匹配项应该具有配置文件,但配置文件也应该具有匹配项。

试试这个:

@RunWith(SpringRunner.class)
@DataJpaTest
public class IProfileDaoTest {

@Autowired
private IProfileDao profileDao; //This class extends JpaRepository<Profile, long>

@Autowired
private IMatchDao matchDao; //This class extends JpaRepository<Match, long>

@Autowired
private TestEntityManager testEntityManager;

@Test
public void saveProfileTest() throws Exception {

@Test
public void profileMatchRelationTest() throws Exception {
//Test if matches stored by the IMatchDao are retrievable from the IProfileDao
Profile profileOne = new Profile("Bob"),
profileTwo = new Profile("Alex");

//Persist the profiles so they exist when they are added to the match
entityManager.persistAndFlush(profileOne);
entityManager.persistAndFlush(profileTwo);

//Create and persist the match with two profiles
Match yourMatch = entityManager.persistFlushFind(new Match(profileOne, profileTwo));

//Add the match to both profiles and persist them again.
profileOne.matchOnes.add(yourMatch);
entityManager.persistAndFlush(profileOne);
profileTwo.matchTwos.add(yourMatch);
entityManager.persistAndFlush(profileTwo);

profileOne = profileDao.getOne(profileOne.getId());
Assert.assertEquals("Match not retrievable by profile.", 1, profileOne.getMatchOnes().size());
}
}

关于java - hibernate什么时候加载映射关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50216469/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com