【发布时间】:2016-09-02 01:00:43
【问题描述】:
这是一个简化的 POJO:
@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;
}
}
现在这是我的类 TelephoneType:
@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;
}
}
我在 TelephoneType 中使用 @JsonAutoDetect 注释的原因是首先自定义 json 属性名称(我需要停用默认的 jsonautodetect),并且还因为 如果我不这样做,我在获取队列时会出错
没有找到类 org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer 的序列化程序,也没有发现创建 BeanSerializer 的属性(为避免异常,禁用 SerializationFeature.FAIL_ON_EMPTY_BEANS)(通过引用链:my.package.Patient["phoneType "]->my.package.TelephoneType_$$_jvste17_13["handler"])
因此,如果没有 @JsonAutoDetect 注释,我会收到错误消息,并且 使用注释不会发生延迟加载,并且 TelephoneType 始终加载到 json 响应中。
我使用 Criteria 进行查询:
return this.entityManager.find(Patient.class, primaryKey);
我还在我的应用程序(Jersey API)的 web.xml 中添加了以下内容:
<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>
现在不知何故,我肯定错过了我的配置中的某些内容,但无法弄清楚是什么,我们在数据库中有许多 @ManyToOne 关系大大降低了 api 的速度(一些比我在示例中显示的更重的对象)所以我真的很感激能找到一种方法来激活这个延迟加载的东西......
【问题讨论】:
-
您能否删除您的 OpenEntityManagerInViewFilter 过滤器,看看发生了什么?
-
如果我删除它,这是我得到的经典错误:“无法延迟初始化角色集合:ca.chronometriq.commons.cmqmodel.Patient.TelephoneType,无法初始化代理 - 没有会话(通过参考链:ca.chronometriq.commons.cmqmodel...."
标签: java hibernate jpa lazy-loading jersey-2.0