我试图在以下列方式设置的dropwizard hibernate事务中使用sql查询仅检索几列感兴趣的内容
@Transactional
public List<MyObject> getInteresting() {
Session session = sessionFactory.openSession();
Transaction tx = null;
List<Service> services = null;
try {
tx = session.beginTransaction();
String sql = "SELECT col1, col4, col7 FROM MyObject WHERE col3 = "someValue";
SQLQuery query = session.createSQLQuery(sql);
query.addEntity(MyObject.class);
services = query.list();
tx.commit();
}
catch(Exception e) {
if (tx != null) tx.rollback();
}
finally {
session.close();
}
return services;
}
执行此查询会导致找不到colum2,col3,col5等错误。基本上它是我从查询中排除的列的错误