- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这是我拥有的一个简化的 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),还因为如果我不这样做,我在获取队列时会出错
No serializer found for class org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: 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(一些比我在示例中显示的对象更重的对象)所以我真的很感激找到一种方法来激活这个延迟加载的东西......
最佳答案
如果您使用的是 JSON,那么我假设您是通过 REST 端点提供结果的。然后发生的事情是您将 Patient
实体传递回 REST 服务。当 REST 服务(在本例中为 Jersey)序列化
Patient
实体时,它会触及所有属性,甚至遍历它们,以便构建一棵完整的树可能的。为了做到这一点,每次 Jersey 访问一个尚未初始化的属性时,Hibernate 都会再次调用数据库。这仅在 EntityManager
尚未关闭时才有可能。
这就是您必须安装 OpenEntityManagerInViewFilter
的原因。没有它,EntityManager
会在您退出服务层时关闭,您会得到一个LazyInitializationException
。 OpenEntityManagerInViewFilter
在 View 级别打开 EntityManager
并保持打开状态,直到 HTTP 请求完成。因此,虽然它看起来像是一个修复程序,但实际上并不是,因为正如您所见,当您失去对访问实体属性的控制权时,在本例中为 Jersey
,然后您最终会加载内容你不想加载。
最好删除 OpenEntityManagerInViewFilter
并弄清楚您究竟希望 Jersey
序列化什么。一旦弄清楚了,至少有两种方法可以处理它。 IHMO,“最佳实践”是拥有 DTO,即数据传输对象。这些是不是实体但具有几乎相同字段的 POJO。在这种情况下,PatientDTO
将拥有除 phoneType
属性(或者可能只是 Id)之外的所有内容。您将在构造函数中将一个 Patient
传递给它,它会复制您希望 Jersey 序列化的字段。然后,您的服务层将负责返回 DTO 而不是 Entities
,至少对于 REST 端点而言。您的客户将获得表示这些 DTO 的 JSON 图,让您更好地控制进入 JSON 的内容,因为您将 DTO 与 Entities
分开编写。
另一种选择是使用 JSON 注释来防止 Jersey 尝试序列化您不想序列化的属性,例如 phoneType
,但这最终会成为问题。会有相互冲突的要求,你永远无法很好地解决它。
虽然一开始制作 DTO 似乎是一种可怕的痛苦,但它并没有看起来那么糟糕,甚至当您想要序列化对客户端更友好的值时它甚至会有所帮助。因此,我的建议是丢掉 OpenEntityManagerInViewFilter
并构建一个适当的服务层来返回 DTO,有时也称为 View 对象。
引用文献:What is Data Transfer Object?
Gson: How to exclude specific fields from Serialization without annotations
关于java - Jersey API + JPA/Hibernate Criteria 延迟加载不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37081105/
我有以下情况要解决,但无法正常工作(尝试了Hibernate和EclipseLink): Table_1: Column_A is Primary Key ... some other
我是 JPA 的新手,但必须在该技术中实现我的项目 我想做的是通过 CriteriaQuery 构建一些数据库查询,但不知道如何将参数列表传递给下面的代码: CriteriaBuilder qb =
我是 JPA 新手,注意到可以通过使用 @Version 注释实体中的字段来使用乐观锁定。我只是好奇,如果之前不存在,持久性提供程序是否会创建一个隐式版本字段。例如网站objectdb状态: "Whe
我有一个 JPA 查询 @Query(value = "SELECT SUM(total_price) FROM ... WHERE ...", nativeQuery = true) 当有匹配的记录
JPA 是否会尝试在已经持久(和非分离)的实体上级联持久化? 为了清楚起见,这是我的情况:我想保留一个新用户: public void addUser(){ //User is an enti
显然,OpenJPA。我也看到提到过 EclipseLink 和 Hibernate,但是在功能上有显着差异吗? 最佳答案 大多数差异来自提供者对 OSGi 的感知程度。例如,您可能需要自己将 Hib
我想将 JPA 用于 micronaut。为此,我使用 io.micronaut.data:micronaut-data-hibernate-jpa:1.0.0.M1 库。每当我运行应用程序并点击端点
我正准备为我的应用实现后端,现在我正在投影数据层。我期待着 Spring 。 最佳答案 Spring Data JPA 不是 JPA 实现。它提供了将数据访问层构建到底层 JPA 顶部的方法。您是否应
假设我有一个表 Item,其中包含一个名为 user_id 的列和一个表 User 以及另一个名为 Superuser 的列: CREATE TABLE Item(id int, user_id in
JPA 2.1 规范说: The entity class must not be final. No methods or persistent instance variables of the
我正在从事一个具有一些不寻常实体关系的项目,我在使用 JPA 时遇到了问题。有两个相关对象;用户,让我们称另一个 X。用户与 X 具有一对多和两个一对一的关系。它基本上看起来像这样 [用户实体] @O
我说的是 JavaEE 中的 JPA。在我读过的一本书中谈到: EntityManager em; em.find(Employee.class, id); “这是实体管理器在数据库中查找实例所需的所
我有 JPA 支持的 Vaadin 应用程序。此应用程序中的组件绑定(bind)到 bean 属性(通过独立的 EL 实现)。一些组件绑定(bind)到外部对象(或其字段),由@OneToOne、@O
是否可以使表中的外键唯一?假设我有实体 A 和 B。 答: @Entity class A extends Serializable { @Id private long id; @OneToOne
我在使用 JPA 时遇到了一点问题。考虑这种情况: 表 A (id_a) | 表 B (id_b, id_a) 我需要的是这样的查询: Select a.*, c.quantity from A as
我有一个由 JPA 管理的实体类,我有一个实体需要在其属性中记录更改。 JPA 是否提供任何方法来处理这种需求? 最佳答案 如果您使用 Hibernate 作为 JPA 提供程序,请查看 Hibern
我想实现以下架构: Table A: a_id (other columns) Table B: b_id (other columns) Table C: c_id (other columns)
我有一个愚蠢的问题。如果能做到的话那就太好了,但我并没有屏住呼吸。 我需要链接到我的 JPA 实体的表中的单个列作为所述 JPA 实体中的集合。有什么方法可以让我单独取回与该实体相关的列,而不必取回整
我有一个 Open JPA 实体,它成功连接了多对多关系。现在我成功地获取了整个表,但我实际上只想要该表中的 ID。我计划稍后调用数据库来重建我需要的实体(根据我的程序流程)。我只需要 ID(或该表中
这是我的一个实体的复合主键。 public class GroupMembershipPK implements Serializable{ private static final long
我是一名优秀的程序员,十分优秀!