Java 正在配置数据挖掘。我希望能够从某些调用中删除它(因此我想这是使用hibernate.initialize()完成的)fetch确保它是非延迟加载的(它添加了一个联接),而对于所有其他调用,它仍然是延迟加载的。如果您想序列化它,您必须获取它。否则,解决方案是

Java 正在配置数据挖掘。我希望能够从某些调用中删除它(因此我想这是使用hibernate.initialize()完成的)fetch确保它是非延迟加载的(它添加了一个联接),而对于所有其他调用,它仍然是延迟加载的。如果您想序列化它,您必须获取它。否则,解决方案是,java,hibernate,jpa,lazy-loading,jersey-2.0,Java,Hibernate,Jpa,Lazy Loading,Jersey 2.0,正在配置数据挖掘。我希望能够从某些调用中删除它(因此我想这是使用hibernate.initialize()完成的)fetch确保它是非延迟加载的(它添加了一个联接),而对于所有其他调用,它仍然是延迟加载的。如果您想序列化它,您必须获取它。否则,解决方案是将它从序列化中完全排除。顺便提一下,Patient Patient=this.entityManager.find(Patient.class,primaryKey);Patient.getPhoneType();return patient;


正在配置数据挖掘。我希望能够从某些调用中删除它(因此我想这是使用hibernate.initialize()完成的)fetch确保它是非延迟加载的(它添加了一个联接),而对于所有其他调用,它仍然是延迟加载的。如果您想序列化它,您必须获取它。否则,解决方案是将它从序列化中完全排除。顺便提一下,Patient Patient=this.entityManager.find(Patient.class,primaryKey);Patient.getPhoneType();return patient;也会解决您的问题,但不太优雅。还有一件事我没有明确提到:如果您需要延迟加载,您仍然会使用“this.entityManager.find(patient.class,primaryKey)”。通常,如果您使用经典的dao模式,您将添加两个实现:一个findPatient和一个findPatientWithPhoneType,并根据需要调用它们。但我的问题是,即使我使用获取类型lazy对其进行了注释,PhoneType也不会延迟加载。能否删除OpenEntityManagerViewFilter筛选器呃,看看发生了什么?如果我删除它,这就是我得到的典型错误:“未能延迟初始化角色集合:ca.chronometriq.commons.cmqmodel.Patient.TelephoneType,无法初始化代理-无会话(通过引用链:ca.chronometriq.commons.cmqmodel…”这个答案与我们在尝试找出延迟加载时所采用的解决方案非常接近。我们使用@JsonView的不同级别来告诉jersey要序列化什么或不序列化什么,这取决于我们想要什么。但是,使用此解决方案或DTO解决方案,我们无法从延迟加载给我们带来的性能优势中获益……当然可以。在您的实体将所有内容设置为延迟加载,并且在您的服务层中只获取您想要序列化的内容。好的,我将在周一进行测试
@Entity
@Table( name = "Patient" )
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn
(
                name="Discriminator",
                discriminatorType=DiscriminatorType.STRING
                )
@DiscriminatorValue(value="P")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Patient implements Serializable{

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column(name = "ID", unique = true, nullable = false)
    protected Integer ID;

    @ManyToOne(targetEntity = TelephoneType.class, fetch=FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinColumn(name="IDPhoneType")
    protected TelephoneType phoneType;


    @JsonProperty(required=false, value="phoneType")
    public TelephoneType getPhoneType() {
        return phoneType;
    }
    public void setPhoneType(TelephoneType phoneType) {
        this.phoneType = phoneType;
    }
}
@Entity
@Table( name = "TelephoneType" )
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
@JsonAutoDetect(getterVisibility=Visibility.NONE, isGetterVisibility=Visibility.NONE, fieldVisibility=Visibility.NONE)
public class TelephoneType implements Serializable{

private static final long serialVersionUID = -3125320613557609205L;

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name = "ID", unique = true, nullable = false)
private Integer ID;

@Column(name = "Name")
private String name;

@Column(name = "Description")
private String description;

public TelephoneType() {
}

@JsonProperty(value="id")
public int getID() {
    return ID;
}

public void setID(int iD) {
    ID = iD;
}

@JsonProperty(value="name")
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@JsonProperty(value="description")
public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}
return this.entityManager.find(Patient.class, primaryKey);
<filter>
    <filter-name>OpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>OpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
return this.entityManager.find(Patient.class, primaryKey);
EntityManager em = getEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Patient> query = cb.createQuery(Patient.class);
Root<Patient> c = query.from(Patient.class);
query.select(c).distinct(true);
c.fetch("phoneType");
TypedQuery<Patient> typedQuery = em.createQuery(query);
List<Patient> allPatients = typedQuery.getResultList();