java - hibernate什么时候加载映射关系
问题描述
我正在创建一个 Spring Boot 应用程序并使用它的内置JpaRepository
接口来存储我的实体。我有以下两个实体(删除了 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 存储库不会立即写入数据库。在测试中,如果您需要测试查询方法,则需要使用由提供的 TestEntityManager @DataJpaTest
(它只是存储库在后台使用的实体管理器,但有一些方便的测试方法)。
更新 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());
}
}
推荐阅读
- python - 如何在 for 循环中捕获异常并仅在循环完成后返回异常?
- spring-boot - 在 Spring Boot 应用程序中将 web-application-type 设置为 none 时未发送电子邮件
- python - 如何在python中拆分字符串并只得到一个单词
- python - 数据框散点图上的关键错误
- python - 烧瓶错误处理程序未正确处理棉花糖 ValidationError
- r - 如何在 R 中求和多个不同维度的矩阵
- mysql - 如何将指数值转换为浮点数
- apache-spark - 如何在 spark 配置文件中定义 total-executor-cores?
- python-3.x - 如何打印标签名称的值而不是 Beautiful Soup 4 和 Python3 中的值?
- awk - Bash count 数字的数量