简体   繁体   English

JPA-延迟加载单向OneToMany无法与鉴别器一起使用

[英]JPA - lazy load unidirectional OneToMany not working with discriminator

I'm trying to have a oneToMany relationship between Visit and CrewMember. 我正在尝试在Visit和CrewMember之间建立oneToMany关系。 This CrewMember is a "type of person" and has some specific attributes that are not needed for other types (Passenger for example) -> hence the inheritance 这个CrewMember是“人的类型”,具有一些其他类型(例如,Passenger)不需要的特定属性->因此继承

This oneToMany relationship has to be lazy loaded, but when I then get the crewMembers it seems as if my join doesn't take the discriminator into account. 这个oneToMany关系必须被延迟加载,但是当我获得乘员组成员时,似乎我的加入没有考虑歧视因素。

Visit class: 参观课程:

@Entity
@Table(name = "VISIT")
public class Visit {
  @OneToMany
  @JoinColumn(name = "VISIT_ID")
  private List<CrewMember> crewMembers;
}

CrewMember class: CrewMember类:

@Entity
@DiscriminatorValue(value = "CREW")
public class CrewMember extends Person {
  @Column(name = "CREW_NUMBER")
  private Integer number;
}

Person class: 人类:

@Entity
@Table(name = "PERSON_ON_BOARD")
@DiscriminatorColumn(name = "TYPE_PERSON_ON_BOARD")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class Person {
  ...
}

I've created a method to find those crewMembers like this: 我创建了一种方法来查找这些乘员组成员,如下所示:

@Stateless
public class PeopleOnBoardRepositoryBean implements PeopleOnBoardRepository {

  @Override
  public List<CrewMember> findCrewMembers(long visitId) {
    TypedQuery<Visit> query =  em().createQuery("SELECT v FROM Visit v JOIN fetch v.crewMembers WHERE v.id = :visitId", Visit.class);
    query.setParameter("visitId", visitId);
    Visit visit = query.getSingleResult();
    return visit.getCrewMembers();
  }
}

But the result always contains ALL the persons (crewMembers, passengers) instead of only the crewMembers.. Why is that and what am I doing wrong? 但是结果总是包含所有人员(crewMembers,乘客),而不是只有crewMembers ..为什么会这样,我在做什么错呢?

If I'm trying to do it the "normal way which I would think that should work" like: 如果我尝试按照“我认为应该正常工作的正常方式”进行操作,例如:

public List<CrewMember> findCrewMembers(long visitId) {
    TypedQuery<Visit> query =  em().createQuery("SELECT v FROM Visit v  WHERE v.id = :visitId", Visit.class);
    query.setParameter("visitId", visitId);
    Visit visit = query.getSingleResult();
    return visit.getCrewMembers();
}

then I'm getting: 然后我得到:

org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: be.fgov.health.msw.domain.visit.Visit.crewMembers, no session or session was closed

EDIT : 编辑

After adding: 添加后:

Visit visit = query.getSingleResult();
visit.getCrewMembers.size();
return visit.getCrewMembers();

I've got the LazyInitializationException fixed. 我已经修复了LazyInitializationException。 This was needed to fetch all the crewMembers. 这是获取所有乘员组成员所必需的。 Otherwise I was just passing a pointer to the arrayList without prefetched elements hence the LazyInitializationException. 否则,我只是传递了一个指向没有预取元素的arrayList指针,因此出现了LazyInitializationException异常。

The problem with the discriminator is still there though.. I'm getting ALL the persons instead of only the crewMembers. 鉴别器的问题仍然存在。.我得到了所有人员,而不仅仅是乘员组成员。 I've also tried adding: 我也尝试添加:

@ForceDiscriminator 

on my Person class but this doesn't change anything. 在我的Person类上,但这没有任何改变。 I've read that on http://www.gmarwaha.com/blog/2009/08/26/hibernate-why-should-i-force-discriminator/ 我已经在http://www.gmarwaha.com/blog/2009/08/26/hibernate-why-should-i-force-discriminator/上阅读了

EDIT 2: 编辑2:

I've "fixed" (workaround) the discriminator column by adding: 我通过添加以下内容来“固定”(解决方法)区分符列:

@OneToMany
@JoinColumn(name = "VISIT_ID")
@Where(clause="TYPE_PERSON_ON_BOARD = 'CREW'")
private List<CrewMember> crewMembers;

The @Where fixes this but I actually find this extremely ugly. @Where解决了这个问题,但我实际上发现它非常丑陋。 Isn't there any other way to do this? 还有其他方法吗?

您需要在事务(会话)的边界内访问实体bean,否则结果将被分离,从而导致惰性异常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM