JPA/Hibernate:@ManyToOne和@OneToOne标记为 FetchType.LAZY 和 optional = false 的关系,而不是在 em.find() 上懒惰地加载?
我有以下实体(仅显示相关映射):
@Entity
@Table(name = "PQs")
public class PQ implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private Integer id;
@Column
private String name;
@ManyToOne(fetch = FetchType.LAZY) // lazy XToOne
@JoinColumn(name = "user_id", referencedColumnName = "person_id")
private User user;
@OneToOne(mappedBy = "pq", fetch = FetchType.LAZY) // lazy XToOne
private Group group;
@OneToOne(mappedBy = "pq", fetch = FetchType.LAZY) // lazy XToOne
private Tendering tendering;
...
}
请注意上面的注释:与其他实体有三种关系:@XToOne
用户(具有简单 ID 为 PK 的 SecurityIdentity 子类,由表示所有者的 PQ 引用):
@Entity
@Table(name = "Users")
@DiscriminatorValue(value = "user")
public class User extends SecurityIdentity
{
@Column
private String name;
@OneToMany(mappedBy = "user")
private Set<PQ> pqs = new HashSet<PQ>();
...
}
组(也是一个 SecurityIdentity 子类,其简单 ID 为 PK,引用 PQ 来表示可以与该 PQ 交互的一组用户):
@Entity
@Table(name = "Groups")
@DiscriminatorValue(value = "group")
public class Group extends SecurityIdentity
{
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "pq_id", referencedColumnName = "id")
private PQ pq;
...
}
招标:
@Entity
@Table(name = "Tenderings")
public class Tendering implements Serializable
{
@Id
@Column(name = "pq_id", insertable = false, updatable = false)
private Integer pqId;
@Column(name = "external_code")
private String externalCode;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "pq_id", referencedColumnName = "id")
private PQ pq;
...
}
不要对共享 ID 的组和用户感到困惑,只需将它们视为简单的 ID 即可。招标只是一个单独的文档对象(一对一)。
如您所见,PQ 实体上有三个关系,如果未设置提取类型,则会预先加载(JPA 默认值)。因此,为了防止这种情况,我将所有关系标记为.@XToOne
@XToOne
FetchType.LAZY
现在使用时
em.find(PQ.class, someExistingId);
我得到休眠输出:
23:53:55,815 INFO [stdout] Hibernate: select pq0_.id as id291_0_, pq0_.description as descript2_291_0_, pq0_.name as name291_0_, pq0_.submission_date as submission4_291_0_, pq0_.user_id as user5_291_0_ from PQs pq0_ where pq0_.id=?
23:53:55,818 INFO [stdout] Hibernate: select user0_.id as id280_0_, user0_1_.identity_type_id as identity2_280_0_, user0_.is_enabled as is1_297_0_, user0_.name as name297_0_, user0_.password as password297_0_, user0_.person_id as person5_297_0_ from Users user0_ inner join SecurityIdentities user0_1_ on user0_.id=user0_1_.id where user0_.person_id=?
23:53:55,821 INFO [stdout] Hibernate: select group0_.id as id280_0_, group0_1_.identity_type_id as identity2_280_0_, group0_.pq_id as pq2_281_0_ from Groups group0_ inner join SecurityIdentities group0_1_ on group0_.id=group0_1_.id where group0_.pq_id=?
23:53:55,823 INFO [stdout] Hibernate: select tendering0_.pq_id as pq1_296_0_, tendering0_.binary_file as binary2_296_0_, tendering0_.customer_id as customer6_296_0_, tendering0_.description as descript3_296_0_, tendering0_.external_code as external4_296_0_, tendering0_.title as title296_0_ from Tenderings tendering0_ where tendering0_.pq_id=?
另外三个SELECT源于@XToOne关系(如网络上许多地方所描述的那样)。我主要关注的来源是这样的:
如前所述,不应获取关系:@ManyToOne
User user
@ManyToOne(fetch=FetchType.LAZY)
应该工作得很好。
...这里从PQ到用户的关系,但它是从语句中看到的...select user0_.id as id280_0_, ...
对于其他两个和 ,两个反向映射,外键引用 PQs 表的 PK (ID),从而在 PQ 实体中产生相同的映射。Group group
Tendering tendering
@OneToOne
请注意,所有三种关系都不是可选的:PQ 始终具有所有者(用户),并且 PQ 始终由招标和组实体引用。我只是还没有在上面的JPA中建模...
因此,在添加 PQ 实体的三个关系时:optional = false
@Entity
@Table(name = "PQs")
public class PQ implements Serializable
{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column
private Integer id;
@Column
private String name;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "user_id", referencedColumnName = "person_id")
private User user;
@OneToOne(mappedBy = "pq", fetch = FetchType.LAZY, optional = false)
private Group group;
@OneToOne(mappedBy = "pq", fetch = FetchType.LAZY, optional = false)
private Tendering tendering;
...
}
...我得到以下休眠输出:
00:47:34,397 INFO [stdout] Hibernate: select pq0_.id as id361_0_, pq0_.description as descript2_361_0_, pq0_.name as name361_0_, pq0_.submission_date as submission4_361_0_, pq0_.user_id as user5_361_0_ from PQs pq0_ where pq0_.id=?
00:47:34,410 INFO [stdout] Hibernate: select user0_.id as id350_0_, user0_1_.identity_type_id as identity2_350_0_, user0_.is_enabled as is1_367_0_, user0_.name as name367_0_, user0_.password as password367_0_, user0_.person_id as person5_367_0_ from Users user0_ inner join SecurityIdentities user0_1_ on user0_.id=user0_1_.id where user0_.person_id=?
00:47:34,413 INFO [stdout] Hibernate: select group0_.id as id350_0_, group0_1_.identity_type_id as identity2_350_0_, group0_.pq_id as pq2_351_0_ from Groups group0_ inner join SecurityIdentities group0_1_ on group0_.id=group0_1_.id where group0_.pq_id=?
请注意,我只在PQ实体上玩过,因为这是我在中使用的那个。(如果这还不够,请启发我。optional = false
em.find(...)
我现在的问题是双重的:
- 为什么对实体的渴望(鉴于据说这是懒惰地工作,请参阅使一对一关系懒惰)?
@ManyToOne
User
- 为什么只提取与实体的关系?是因为实体将 PQ 的 PK 列引用为 PK 本身(in ),而实体没有(与 PQ 的 PK 的常规关系)?
OneToOne
Tendering
Tendering
@Id
Tendering
Group
怎么了?如何使这些非可选关系变得懒惰?(没有代码检测或其他黑客,只有普通的注释...)
我知道LAZY的事情只是JPA提供者对延迟加载或不做一些事情的提示,但是在这种情况下,似乎有其他事情是错误的(因为它的一部分正在工作)。
PS:我使用的是Hibernate 4.0 BETA,JBoss 7.0.0.Final附带的版本以及仅JPA注释(以上都是JPA 1.0兼容的)。