Java 延迟加载的集合在junit中为空

Java 延迟加载的集合在junit中为空,java,spring-boot,hibernate,junit,spring-data-jpa,Java,Spring Boot,Hibernate,Junit,Spring Data Jpa,我有一个SpringBoot应用程序,我在其中定义了一个实体,如下所示 @Entity public class Organisation { @Id @GeneratedValue @JsonIgnore private Long id; private String entityId; @OneToMany(mappedBy = "parent") @Where(clause = "active_ind=true") @JsonIgnore pr

我有一个SpringBoot应用程序,我在其中定义了一个实体,如下所示

@Entity
public class Organisation {

@Id
@GeneratedValue
@JsonIgnore
private Long id;

private String entityId;

@OneToMany(mappedBy = "parent")
@Where(clause = "active_ind=true")
@JsonIgnore
private Set<Organisation> activeSubOrgs = new HashSet<>();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parentId")
private Organisation parent;

public Set<Organisation> getActiveSubOrgs() {
    return activeSubOrgs;
}
@SpringBootTest
@RunWith(SpringRunner.class)
@Transactional
public class OrgServiceTest {

    @Autowired
    private OrganisationService organisationService;

    @Before
    public void setup() {
        Organisation company = new Organisation("c", true);
        company = organisationRepository.save(company);
        Organisation circle = new Organisation("circle1", true);
        circle.setParent(company);
        circle = organisationRepository.save(circle);
        Organisation div1 = new Organisation("div1", true);
        div1.setParent(circle);
        div1 = organisationRepository.save(div1);
    }

    @Test
    public void getChildrenForEntitySuccessTest() {
        Set<Organisation> children = organisationService.getChildrenForEntity("c");
        System.out.println(children.iterator().next().getEntityId());
        assertEquals("circle1", children.iterator().next().getEntityId());
    }
@实体
公营班级组织{
@身份证
@生成值
@杰索尼奥雷
私人长id;
私有字符串entityId;
@OneToMany(mappedBy=“家长”)
@式中(子句=“活动项=真”)
@杰索尼奥雷
private Set activeSubOrgs=new HashSet();
@manytone(fetch=FetchType.LAZY)
@JoinColumn(name=“parentId”)
私人机构家长;
公共集getActiveSubargs(){
返回活动子RGS;
}
在我的服务类中,我有一个函数来获取孩子们

public Set<Organisation> getChildrenForEntity(String entityId) {
    Organisation parent = organisationRepository.findByEntityIdAndActiveInd(entityId, true);
    return parent.getActiveSubOrgs();
}
公共集getChildrenForEntity(字符串entityId){
Organization parent=OrganizationRepository.FindByentityId和ActiveId(entityId,true);
返回parent.getActiveSubOrgs();
}
这很好,从rest控制器调用时会得到子级,但当我在junit中使用相同的函数进行测试时,它总是返回空的。在我的sql跟踪日志中,我看到在调用getActiveSubOrgs()时没有触发查询。我的junit测试如下所示

@Entity
public class Organisation {

@Id
@GeneratedValue
@JsonIgnore
private Long id;

private String entityId;

@OneToMany(mappedBy = "parent")
@Where(clause = "active_ind=true")
@JsonIgnore
private Set<Organisation> activeSubOrgs = new HashSet<>();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parentId")
private Organisation parent;

public Set<Organisation> getActiveSubOrgs() {
    return activeSubOrgs;
}
@SpringBootTest
@RunWith(SpringRunner.class)
@Transactional
public class OrgServiceTest {

    @Autowired
    private OrganisationService organisationService;

    @Before
    public void setup() {
        Organisation company = new Organisation("c", true);
        company = organisationRepository.save(company);
        Organisation circle = new Organisation("circle1", true);
        circle.setParent(company);
        circle = organisationRepository.save(circle);
        Organisation div1 = new Organisation("div1", true);
        div1.setParent(circle);
        div1 = organisationRepository.save(div1);
    }

    @Test
    public void getChildrenForEntitySuccessTest() {
        Set<Organisation> children = organisationService.getChildrenForEntity("c");
        System.out.println(children.iterator().next().getEntityId());
        assertEquals("circle1", children.iterator().next().getEntityId());
    }
@SpringBootTest
@RunWith(SpringRunner.class)
@交易的
公共类服务测试{
@自动连线
私人机构服务;
@以前
公共作废设置(){
组织公司=新组织(“c”,正确);
公司=OrganizationRepository.save(公司);
组织圈=新组织(“圈1”,正确);
圈。母公司(公司);
圆圈=OrganizationRepository.save(圆圈);
组织部门1=新组织(“部门1”,正确);
div1.setParent(圆);
div1=OrganizationRepository.save(div1);
}
@试验
public void getChildrenForEntitySuccessTest(){
Set children=OrganizationService.getChildrenForEntity(“c”);
System.out.println(children.iterator().next().getEntityId());
assertEquals(“circle1”,children.iterator().next().getEntityId());
}

测试中设置的子项是空的,而实际上它应该有圆圈1。我尝试对子项调用Hibernate.initialize(),但这也不起作用。

问题是必须在两侧更新双向关系,即父项和子项必须相互了解。在函数
setup()中
,您只需定义子项的父项。因此,每个子项都知道其父项。但是,父项不知道其子项

对于双向关系,一个很好的处理方法是为一个类定义一个函数来设置/添加属性,并自动更新另一个。对于
OneToMany
关系,可以使用
add(entity)
函数很好地处理

public void addActiveSubOrg(Organisation activeSubOrg) {
    this.activeSubOrgs.add(activeSubOrgs);
    activeSubOrg.setParent(activeSubOrg);
}

问题是双向关系必须在两侧更新,即父级和子级必须相互了解。在函数
setup()
中,您只定义子级的父级。因此,每个子级都知道其父级。但是,父级不知道其子级

对于双向关系,一个很好的处理方法是为一个类定义一个函数来设置/添加属性,并自动更新另一个。对于
OneToMany
关系,可以使用
add(entity)
函数很好地处理

public void addActiveSubOrg(Organisation activeSubOrg) {
    this.activeSubOrgs.add(activeSubOrgs);
    activeSubOrg.setParent(activeSubOrg);
}

是的。双向更新是有效的。但告诉我,我有一个服务,它以类似的方式设置父对象,而不添加父对象的子对象。为什么它在数据库中创建正确的条目,但不更新java端对象?java对象可能处于分离状态,即它们不反映当前数据库。我不确定其中的内容你的实现确实发生了。无论如何,保持双向实体始终同步以避免异常。你可以阅读Hibernate中的不同状态。是的。双向更新是有效的。但是告诉我,我有一个服务,它以类似的方式设置父级,而不添加到父级的子级。为什么它会使r数据库中的条目,但不更新java端对象?java对象可能处于分离状态,即它们不反映当前数据库。我不确定您的实现中实际发生了什么。无论如何,始终保持双向实体同步以避免异常。您可以在Hibernat中了解不同的状态E