こんにちは、私は SQL-QUERY を使用して Hibernate で開発しています。リストからデータを取得し、それをエンティティの属性に設定することができました。必要なのは 1 つだけです。次のとおりです。
Query query = sessionFactory.openSession().getNamedQuery("getListCustomers");
query.setResultTransformer(new ResultTransformer() {
public Object transformTuple(Object[] aux, String[] aliases) {
Customer item = new Customer();
item.setCustomerId(((BigDecimal) aux[0]).longValue());
item.getCompany().setCompanyId(((BigDecimal) (aux[1]!=null?aux[1]:0)).longValue()); // Failed
item.setCode(aux[2]!=null?aux[2].toString():"");
item.setName(aux[3]!=null?aux[3].toString():"");
return item;
}
public List transformList(List collection) {
return collection;
}
});
問題は、属性 company が別のエンティティとの関係を持っていることです。マッピングによると、他のエンティティ CompanyId に関連しています。
public class Customer {
private Long customerId;
private Company company;
}
コンソールに次のエラー メッセージが表示されます。
[Request processing failed; nested exception is java.lang.NullPointerException]