gpt4 book ai didi

java - JPA 选择忽略 OR 部分

转载 作者:行者123 更新时间:2023-11-29 00:03:19 25 4
gpt4 key购买 nike

我有选择查询:

SELECT entity
FROM EnterpriseEventLog entity
WHERE entity.enterprise.recordId = :recordIdEnterprise
OR entity.group.enterprise.recordId = :recordIdEnterprise

但是 JPA 忽略了“entity.enterprise.recordId = :recordIdEnterprise”部分,选择工作就像没有这样的部分一样。如果我删除这个“OR entity.group.enterprise.recordId = :recordIdEnterprise”选择工作正常并提取数据。
我使用 mySql 5.5 和 EclipseLink 2.5.1
这是我的类(class)示例:

@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

关于java - JPA 选择忽略 OR 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28702578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com