2012-06-29 15 views
1

我想運行一個簡單的SQL查詢包含一個group by子句。使用註釋在本地休眠/ jpa查詢中標量值的問題

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的目錄型結果(在Java中調試視圖):
[1,1,1,1]

案例2:
SQL-結果:
|參考|
| 123456780678MMM |

Java的列表 - 結果:
[1]

我正在尋找一種方式來運行標量值(使用Hibernate/JPA)的一個簡單的SQL查詢來獲取的結果與值的列表的結果。

有沒有辦法做到這一點,而不使用標準api?

如果您需要更多信息,請告訴我!

非常感謝您的幫助。

馬科

+0

DataHistory中的'reference'屬性是如何註釋的? (你的問題的答案是肯定的,但目前有問題,你想要一個JPA 2.0解決方案還是Hibernate propritary一個好嗎?) – esej

+0

由於DataHistory包含來自兩個表的值,我使用NamedNativeQueris和SqlResultSetMapping填補實體。因此,屬性'reference'沒有註釋。也許我必須添加一個@EntityResult到@SqlResultSetMapping(name =「resultMapping」)。我更喜歡JPA 2.0解決方案,但是一個propritary Hibernate解決方案也可以。 – marco235

+0

如果引用沒有註釋,你會看到這個:結果「錯誤」的類型。 DataHistory不是一個實體嗎? (一個實體可以分佈在N個表中......)也許你可以給我們更多的代碼/註釋?如果您沒有使用NamedQueries/Proper Entities,那麼您正在失去使用JPA的很多好處,幾乎在很難看到使用該技術的地方。 (命名)NativeQueries只是帶有一些自動映射的jdbc-sql-queries。 – esej

回答

1

問題是在休眠中的錯誤,即鑄結果爲java.lang.String的java.lang.Character中insteat。結果中只返回每行的第一個字符。

有一個與解決方案的更詳細的說明了另一個問題:
Hibernate native query - char(3) column

如果我使用了鑄造功能在我的查詢如下,它的工作原理:

@NamedNativeQuery(name = HistoryEntity.FIND_REFERENCE_BY_LRN, 
        query = "SELECT cast(h.reference as VARCHAR(27)) as reference " 
         + "FROM dataHistory h " 
         + "WHERE h.lrn = :lrn " 
         + "GROUP BY h.reference", 
        resultSetMapping = "resultMapping") 

PS:我發現引用的問題遲到了。對不起!