JPA select 忽略 OR 部分
JPA select ignoring OR part
我有 select 个查询:
SELECT entity
FROM EnterpriseEventLog entity
WHERE entity.enterprise.recordId = :recordIdEnterprise
OR entity.group.enterprise.recordId = :recordIdEnterprise
但是 "entity.enterprise.recordId = :recordIdEnterprise" 部分被 JPA 忽略,select 就像没有这部分一样。如果我删除这个 "OR entity.group.enterprise.recordId = :recordIdEnterprise" select 可以正常工作并提取数据。
我使用 mySql 5.5 和 EclipseLink 2.5.1
这是我的例子 类:
@Entity (name = "EnterpriseEventLog")
@Table (name = "ENTERPRISE_EVENT_LOG")
public class EnterpriseEventLogEntity {
@ManyToOne (fetch = FetchType.LAZY)
@JoinColumn (name = "RECORD_ID_ENTERPRISE", updatable = false)
private EnterpriseEntity enterprise;
@ManyToOne (fetch = FetchType.LAZY)
@JoinColumn (name = "RECORD_ID_GROUP", updatable = false)
private GroupEntity group;
}
@Entity (name = "Enterprise")
@Table (name = "ENTERPRISE")
public class EnterpriseEntity extends EntityBase {
@OneToMany (fetch = FetchType.LAZY, mappedBy = "enterprise", cascade = CascadeType.ALL)
private List<EnterpriseEventLogEntity> eventLog = new ArrayList<> ();
}
@Entity (name = "Group")
@Table (name = "VGROUP")
public class GroupEntity extends EntityBase {
@NotNull
@ManyToOne (fetch = FetchType.LAZY, optional = false)
@JoinColumn (name = "RECORD_ID_ENTERPRISE", nullable = false, updatable = false)
private EnterpriseEntity enterprise;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "group", cascade = CascadeType.ALL)
private List<EnterpriseEventLogEntity> eventLog = new ArrayList<>();
}
@MappedSuperclass
public class EntityBase {
@Id
@NotNull
@Column (name = "RECORD_ID", nullable = false)
private Long recordId;
}
正如 JB Nizet 在他的评论中所说,问题是 entity.group 是内部联接。
所以正确的查询是:
SELECT entity
FROM EnterpriseEventLog entity LEFT JOIN entity.group group LEFT JOIN entity.enterprise enterprise
WHERE enterprise.recordId = :recordIdEnterprise
OR group.enterprise.recordId = :recordIdEnterprise
更多信息在这里:EclipseLink User Guide
我有 select 个查询:
SELECT entity
FROM EnterpriseEventLog entity
WHERE entity.enterprise.recordId = :recordIdEnterprise
OR entity.group.enterprise.recordId = :recordIdEnterprise
但是 "entity.enterprise.recordId = :recordIdEnterprise" 部分被 JPA 忽略,select 就像没有这部分一样。如果我删除这个 "OR entity.group.enterprise.recordId = :recordIdEnterprise" select 可以正常工作并提取数据。
我使用 mySql 5.5 和 EclipseLink 2.5.1
这是我的例子 类:
@Entity (name = "EnterpriseEventLog")
@Table (name = "ENTERPRISE_EVENT_LOG")
public class EnterpriseEventLogEntity {
@ManyToOne (fetch = FetchType.LAZY)
@JoinColumn (name = "RECORD_ID_ENTERPRISE", updatable = false)
private EnterpriseEntity enterprise;
@ManyToOne (fetch = FetchType.LAZY)
@JoinColumn (name = "RECORD_ID_GROUP", updatable = false)
private GroupEntity group;
}
@Entity (name = "Enterprise")
@Table (name = "ENTERPRISE")
public class EnterpriseEntity extends EntityBase {
@OneToMany (fetch = FetchType.LAZY, mappedBy = "enterprise", cascade = CascadeType.ALL)
private List<EnterpriseEventLogEntity> eventLog = new ArrayList<> ();
}
@Entity (name = "Group")
@Table (name = "VGROUP")
public class GroupEntity extends EntityBase {
@NotNull
@ManyToOne (fetch = FetchType.LAZY, optional = false)
@JoinColumn (name = "RECORD_ID_ENTERPRISE", nullable = false, updatable = false)
private EnterpriseEntity enterprise;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "group", cascade = CascadeType.ALL)
private List<EnterpriseEventLogEntity> eventLog = new ArrayList<>();
}
@MappedSuperclass
public class EntityBase {
@Id
@NotNull
@Column (name = "RECORD_ID", nullable = false)
private Long recordId;
}
正如 JB Nizet 在他的评论中所说,问题是 entity.group 是内部联接。
所以正确的查询是:
SELECT entity
FROM EnterpriseEventLog entity LEFT JOIN entity.group group LEFT JOIN entity.enterprise enterprise
WHERE enterprise.recordId = :recordIdEnterprise
OR group.enterprise.recordId = :recordIdEnterprise
更多信息在这里:EclipseLink User Guide