groupby句を含む単純なSQLクエリを実行しようとしています。
HistoryEntity:
@Entity
@NamedNativeQueries(value = {
@NamedNativeQuery(name = HistoryEntity.FIND_ALL_BY_REFERENCE,
query = "SELECT h.id, h.reference, h.lrn "
+ "FROM dataHistory h "
+ "WHERE h.reference = :referenceNumber OR h.lrn = :referenceNumber",
resultSetMapping = HistoryEntity.FIND_ALL_BY_REFERENCE_MAPPING),
@NamedNativeQuery(name = HistoryEntity.FIND_REFERENCE_BY_LRN,
query = "SELECT h.reference as reference "
+ "FROM dataHistory h "
+ "WHERE h.lrn = :lrn "
+ "GROUP BY h.reference",
resultSetMapping = "resultMapping")
})
@SqlResultSetMappings(value = {
@SqlResultSetMapping(name = HistoryEntity.FIND_ALL_BY_REFERENCE_MAPPING, entities = {
@EntityResult(entityClass = HistoryEntity.class, fields = {
@FieldResult(name = "id", column = "id"),
@FieldResult(name = "reference", column = "reference"),
@FieldResult(name = "lrn", column = "lrn")
})
}),
@SqlResultSetMapping(name = "resultMapping", columns = {
@ColumnResult(name = "reference")
})
})
public class HistoryEntity implements Serializable {
/**
* @param referenceNumber Referenz (LRN oder BRN)
* @param brnList Liste von BRNs
*/
public static final String FIND_ALL_BY_REFERENCE = "HistoryEntity.findAllByReference";
public static final String FIND_ALL_BY_REFERENCE_MAPPING = "HistoryEntity.findAllByReferenceMapping";
private Integer id;
private String reference;
private String lrn;
public HistoryEntity() {}
@Id
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getReference() {
return reference;
}
public void setReference(String reference) {
this.reference= reference;
}
public String getLrn() {
return lrn;
}
public void setLrn(String lrn) {
this.lrn = lrn;
}
サービスクラスでは、次のようにクエリを実行します。
Query query = entityManager.createNamedQuery("myQuery");
query.setParameter("lrn", lrn);
List resultList = query.getResultList();
クエリの結果に応じて、リストにはjava.lang.Characterが含まれます。
ケース1:
SQL-結果(SQLクライアントでSQLを実行している場合):
| リファレンス|
| 123456780678MMM |
| 123456781678MMM |
| 123456782678MMM |
| 123456783678MMM |
Java-List-Result(Javaデバッグビュー内):
[1、1、1、1]
ケース2:
SQL-結果:
| リファレンス|
| 123456780678MMM |
Java-リスト-結果:
[1]
(hibernate / jpaを使用して)スカラー値を使用して単純なSQLクエリを実行し、結果の値を含むリストを取得する方法を探しています。
基準APIを使用せずにこれを行う方法はありますか?
さらに詳しい情報が必要な場合は、お知らせください。
よろしくお願いします。
マルコ