首页 > 解决方案 > 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 保存的匹配项?或者我应该找到具有单独功能的配置文件的匹配项?

标签: javajpaspring-data-jpa

解决方案


由于性能(可能还有其他)原因,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());
    }
}

推荐阅读