2
我創建了一個SQL視圖,其中包含User的所有屬性和一個"is_granted"
,這是布爾值,使用自定義pl/sql函數計算得出。在我的應用程序中,我需要創建本地SQL查詢和映射值到DTO對象。問題在於,在轉換過程中拋出異常並且查詢失敗。SQL視圖中的Hibernate結果轉換
考慮仿效模式:
@Entity
@Table("quasar_user")
public class User{
private long id;
private String firstname;
private String lastname;
private User changedBy;
// a lot of others properties ...
}
public class UserDto{
private User user;
private boolean granted;
// getters setters
}
查詢 - 映射屬性DTO
@SuppressWarnings("unchecked")
public List<UserDto> getGrantedUsers(final long userId){
final String sql =
"SELECT u.* as user, u.is_granted as granted " +
"FROM user_view u " +
"WHERE u.id= :userId ";
return sessionFactory.getCurrentSession().createSQLQuery(sql)
.addEntity("user", User.class)
.addScalar("granted", StandardBasicTypes.BOOLEAN)
.setLong("userId", userId)
.setResultTransformer(Transformers.aliasToBean(UserDto.class))
.list();
}
有任何的想法,問題出在哪裏?轉型有問題嗎?
堆棧跟蹤
CustomMappingExceptionResolver:doResolveException(line 37) - java.lang.NullPointerException
at org.hibernate.loader.DefaultEntityAliases.intern(DefaultEntityAliases.java:193)
at org.hibernate.loader.DefaultEntityAliases.getSuffixedPropertyAliases(DefaultEntityAliases.java:151)
at org.hibernate.loader.DefaultEntityAliases.determinePropertyAliases(DefaultEntityAliases.java:93)
at org.hibernate.loader.DefaultEntityAliases.<init>(DefaultEntityAliases.java:65)
at org.hibernate.loader.ColumnEntityAliases.<init>(ColumnEntityAliases.java:43)
at org.hibernate.loader.custom.sql.SQLQueryReturnProcessor.generateCustomReturns(SQLQueryReturnProcessor.java:198)
at org.hibernate.loader.custom.sql.SQLCustomQuery.<init>(SQLCustomQuery.java:152)
at org.hibernate.engine.query.spi.NativeSQLQueryPlan.<init>(NativeSQLQueryPlan.java:67)
at org.hibernate.engine.query.spi.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:197)
at org.hibernate.internal.AbstractSessionImpl.getNativeSQLQueryPlan(AbstractSessionImpl.java:226)
at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:232)
at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:157)
at cz.nlfnorm.quasar.dao.impl.UseDaoImpl.getGrantedUsers(UseDaoImpl.java:234)
哇,我看了所有的地方,而這個最終被解決。謝謝!另外,值得一提的是,我沒有在DefaultEntityAliases.java:193開始體驗NPE,直到我在目標實體上添加了@JoinColumnsOrFormulas註釋。 – 2016-02-01 21:41:10