我有一个名为Project的实体。

public class Project {

@EmbeddedId
private ProjectID id;

@Column(name="updated_at")
private Date updatedAt;

@Column(name="sys_updated_at")
private Date sysUpdatedAt;

@OneToOne(optional=true, mappedBy="project", fetch=FetchType.EAGER)
private SysProject sysProject;
}


如您所见,它与“ SysProject”具有一对一关系

public class SysProject extends BaseSysEntity implements Serializable {

@OneToOne(optional=true, fetch= FetchType.LAZY)
@JoinColumns({
    @JoinColumn(name="sysClientId", referencedColumnName="sysClientId"),
    @JoinColumn(name="pProject", referencedColumnName="pProject")
})
private Project project;

@Column(name="sysupdate")
private Date sysupdate;


public Project getProject() {
    return project;
}

public void setProject(Project project) {
    this.project = project;
}
}


现在,我想编写一个满足两个条件的HQL查询。

1)SysProject为空的所有项目。

2)所有具有SysProject但Project.updatedAt!= SysProject.sysupdate的项目

我写了以下查询,但它只返回满足点(2)的项目

SELECT p FROM Project p WHERE (NOT EXISTS (SELECT s FROM p.sysProject s)) OR (p.sysProject.id IS NOT NULL AND p.sysProject.sysupdate != p.updatedAt)


当我从查询中删除以下部分时,它符合第(1)点:

(p.sysProject.id不为空,p.sysProject.sysupdate!= p.updatedAt)

最佳答案

尝试这个 :

String hql = "FROM Project WHERE sysProject IS NULL OR (sysProject.id IS NOT NULL AND sysProject.sysupdate != updatedAt)";
Query query = session.createQuery(hql);
List<Project> projects = query.list();

08-05 02:56