我在我的项目中使用JPA和hibernate。
我创建了一个查询,其中我在许多表上进行了连接操作。所以我创建了一个原生的。我得到的结果是在object []的列表中,但我希望将结果自动转换为java POJO类。 您可以检查下面的查询语法和POJO java类。
JPA查询
@Query(value = "SELECT obsp.Identifier, obs.phenomenontimestart, nv.value " +
"From Series s " +
"INNER JOIN Featureofinterest fi on s.featureofinterestid = fi.featureofinterestid " +
"INNER JOIN ObservableProperty obsp on s.observablepropertyid = obsp.ObservablePropertyId " +
"INNER JOIN Observation obs on s.seriesid = obs.seriesid " +
"INNER JOIN NumericValue nv on nv.observationid = obs.observationid " +
"where fi.identifier = ?1 and obs.phenomenontimestart >= ?2 AND obs.phenomenontimestart <= ?3 " +
"order by obs.phenomenontimestart",
nativeQuery = true)
List<CurrentMeasure> findCurrentMeasure(String ident, Timestamp t1, Timestamp t2);
POJO课程
public class CurrentMeasure {
private String identifier;
private Timestamp dateTime;
private BigDecimal bigDecimal;
public CurrentMeasure() {
}
public CurrentMeasure(String identifier, Timestamp dateTime, BigDecimal bigDecimal) {
this.identifier = identifier;
this.dateTime = dateTime;
this.bigDecimal = bigDecimal;
}
public String getIdentifier() {
return identifier;
}
public void setIdentifier(String identifier) {
this.identifier = identifier;
}
public Timestamp getDateTime() {
return dateTime;
}
public void setDateTime(Timestamp dateTime) {
this.dateTime = dateTime;
}
public BigDecimal getBigDecimal() {
return bigDecimal;
}
public void setBigDecimal(BigDecimal bigDecimal) {
this.bigDecimal = bigDecimal;
}
}
答案 0 :(得分:1)
使用JPA,您可以直接在HQL查询中调用类~
(?:
\G (?!\A) # contiguous to previous match, but not at the start of the string
|
{ (?=[^}]* }) # start with { and check if a closing bracket follows
|
\[ (?=[^]]* ]) # the same for square bracket
)
\K # start the match result here
[A-G] [^]A-G}]*
~xS
的构造函数。
示例:
CurrentMeasure
新法语在Jboss documentation第11.5章解释。
另一种解决方案是使用HQL Transformers来实现相同的结果,而无需使用构造函数。