我正在使用JPA EclipseLink(Java和MySQL服务器),我希望使用非独占或某些“一对一”之间的查询来执行查询。相同属性(主键)上的关系:
public class elements implements Serializable {
private static final long serialVersionUID = 1L;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "element_id", unique = true, nullable = true, insertable = false, updatable = false)
private Element eFeatures1;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "element_id", unique = true, nullable = true, insertable = false, updatable = false)
private Element eFeatures2;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "element_id", unique = true, nullable = true, insertable = false, updatable = false)
private NewCompoundsLM eFeatures3;
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 10)
@Column(name = "element_id")
private int elementId;
... some more attributes
}
public class features1 implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "element_id")
private int elementId;
@Size(max = 20)
@NotNull
@Column(name = "kegg_id")
private String keggId;
... some more attributes
}
public class features2 implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "element_id")
private int elementId;
@Size(max = 20)
@NotNull
@Column(name = "hmdb_id")
private String hmdbId;
... some more attributes
}
... some more attributes
}
public class features3 implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "element_id")
private int elementId;
... some more attributes
}
将element_id作为表features1,features2和features3
中的外键之后,我想要检索表eFeatures1或eFeatures2或eFeatures3中存在的元素,使用非独占或,但我不能执行它
我尝试了下一个查询:
SELECT e FROM elements e
WHERE (e.features1.keggId!='' or e.features2.hmdbId!='')
and (e.mass >= 236.08001738701788 and e.mass <= 236.0941826129821);
但是,当我运行时,我获得的结果是e.efetarues1.keggId!=&#39;&#39; AND e.efetarues2.hmdbId!=&#39;&#39;,结果只有一个元素。我想要检索具有e.efeatures1.keggId!=&#39;&#39;的3个元素,这3个元素具有e.efeatures2.hmdbId!=&#39;&#39;和达到两个条件的1个元素。 有没有办法用JPA EclipseLink获得它?
我在SQL中做过,我想在JPA中执行此查询:
SELECT c.compound_id,ck.kegg_id,ch.hmdb_id FROM compounds c
LEFT JOIN compounds_kegg ck
on c.compound_id=ck.compound_id
left join compounds_hmdb ch
on c.compound_id=ch.compound_id
where (ck.kegg_id is not null or ch.hmdb_id is not null)
and (c.mass >= 236.08001738701788 and c.mass <= 236.0941826129821);
+-------------+---------+-----------+
| compound_id | kegg_id | hmdb_id |
+-------------+---------+-----------+
| 50303 | NULL | AAAA |
| 68206 | NULL | BBBB |
| 89758 | AAAAAA | NULL |
| 95110 | BBBBBB | NULL |
| 91763 | CCCCCC | NULL |
| 71801 | DDDDDD | EEEE |
+-------------+---------+-----------+
但是,当我在JPA中运行查询时:
SELECT e FROM elements e
WHERE (e.features1.keggId!='' or e.features2.hmdbId!='')
and (e.mass >= 236.08001738701788 and e.mass <= 236.0941826129821);
查询仅返回71801
此外,我跑的时候
SELECT e FROM elements e
WHERE (e.ncKegg is not null or nc.ncHMDB is not null)
and (nc.mass >= 236.08001738701788 and nc.mass <= 236.0941826129821)
查询返回满足质量的最后一个条件的所有元素(8个元素而不是6个元素)。
已编辑:我已经使用了查询
SELECT c FROM NewCompounds c
LEFT JOIN NewCompoundsKegg ck on c.compoundId=ck.compoundId
LEFT JOIN NewCompoundsHMDB ch on c.compoundId=ch.compoundId
where (ck.keggId is not null or ch.hmdbId is not null)
and (c.mass >= 236.08001738701788 and c.mass <= 236.0941826129821)
但是我不知道为什么我必须在JPA中使用左连接,如果我已经在主元素中声明了关系。另外,我不知道它如何影响应用程序的性能以包括3个左连接
提前致谢。
答案 0 :(得分:0)
这是一个可能的解决方案:
{{1}}
但是我不知道为什么我必须在JPA中使用左连接,如果我已经在主元素中声明了关系。另外,我不知道它如何影响应用程序的性能以包括3个左连接