一尘不染

具有createSQLQuery的ResultTransformer强制在实体字段中不使用camelCase

hibernate

我有一个SQL查询,如下所示:

List<Employee> employees = getCurrentSession()
                    .createSQLQuery(
                            "select"
                                    + e.id as id,e.first_name as firstName,e.password as password
                                    + "from employee e,employee_role er,role r where e.employee_id=er.employee_id and er.role_id=r.role_id and r.name='ROLE_ADMIN' ")
                    .setResultTransformer(Transformers.aliasToBean(Employee.class))
                    .list();

我在Employee中有一个名为firstName的属性,但是当尝试在单元测试中在dao之上运行时,出现以下异常:

org.hibernate.PropertyNotFoundException: Could not find setter for firstname on class com.app.domain.Employee

我不知道从这个firstname属性进入hibernate状态?我在查询中不正确吗?

解决方法是将属性更改为firstname,getters,setters的任何方法,但是有关hibernate的原因以及如何避免这种行为的任何想法,因为我想在自己的域中使用camelCase,请提出建议。


阅读 324

收藏
2020-06-20

共1个答案

一尘不染

您可以使用addScalar(String columnAlias,Type
type)
显式声明本机SQL的列别名:

  getCurrentSession()
  .createSQLQuery( "select e.id as id,e.first_name as firstName,e.password as password from xxxxxx")
                .addScalar("id",StandardBasicTypes.INTEGER )
                .addScalar("firstName",StandardBasicTypes.STRING )
                .addScalar("password",StandardBasicTypes.STRING )
                .setResultTransformer(Transformers.aliasToBean(Employee.class))
                .list();
2020-06-20