由于我对Hibernate不太熟悉,因此不确定执行此操作的最佳方法是什么。 假设我有一个实体EmployeeDAO,其字段为monthly_salary,我想知道该雇员的年薪,因此我创建了一个别名字段Annual_salary。为了处理这个别名字段,我创建了一个Employee类来扩展EmployeeDAO。如果执行以下操作,则只能加载别名字段(希望不必为每个列字段都添加addScalar),那么同时加载列字段和别名字段的最佳方法是什么?请注意,我原来的sql比较复杂,我必须使用createSQLQuery函数,以下仅是示例。
String sql = "Select e.*, e.monthly_salary*12 as annual_salary from Employee e";
List<Employee> fChk = session.createSQLQuery(sql).create
.addScalar("annual_salary", new BigDecimalType())
.setResultTransformer(Transformers.aliasToBean(Employee.class))
.list();
答案 0 :(得分:0)
Ok, lucky me figure out how to solve it. Create a new Class Employee, inside it has the alias fields and EmployeeDAO obj. Problem solved
String sql = "Select e.*, e.monthly_salary*12 as annual_salary from Employee e";
List<Employee> emp = session.createSQLQuery(sql)
.addEntity("e", EmployeeDAO.class)
.addScalar("annual_salary", new BigDecimalType())
.setResultTransformer(Transformers.aliasToBean(Employee.class))
.list();
---------
public class Employee{
private EmployeeDAO e;
private BigDecimal annual_salary;
...setter and getter
}