postgresql 带有 createSQLQuery 的 ResultTransformer 强制实体字段中没有驼峰式命名法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8136225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 23:12:03  来源:igfitidea点击:

ResultTransformer with createSQLQuery forces no camelCase in entity fields

hibernatepostgresql

提问by fresh_dev

I have an sql query as follows:

我有一个 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();

I have a property in the Employee called firstName, but when trying to run above dao in a unit test, I am getting the following exception:

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

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

I don't know where hibernate get from this firstname property ? I didn't right it in my query ?

我不知道 hibernate 从这个名字属性中得到什么?我没有在我的查询中正确?

any way the workaround was to change the property to firstname, and getters,setters too but any ideas why hibernate is making such behavior, and how to avoid it, since I want to use camelCase in my domain, please advise.

解决方法是将属性更改为名字、getter、setter 的任何方式,但任何想法为什么 hibernate 会做出这种行为,以及如何避免它,因为我想在我的域中使用驼峰命名法,请提供建议。

回答by Ken Chan

You can use addScalar(String columnAlias, Type type)to explicitly declare the column alias of your native SQL:

您可以使用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();

回答by Matthew Wood

For a simpler solution, double-quote the identifier in the query as sent to the server. Instead of

对于更简单的解决方案,在发送到服务器的查询中用双引号引用标识符。代替

e.first_name as firstName

it should read

它应该读

e.first_name as "firstName"

In PostgreSQL, double-quoting an identifier forces case-sensitivity. Unquoted, it (mostly) follows the SQL standard and folds to a single case (albeit lower case where the standard is upper case).

在 PostgreSQL 中,双引号标识符强制区分大小写。未加引号,它(大部分)遵循 SQL 标准并折叠为单个大小写(尽管标准为大写时为小写)。

回答by Shivan Dragon

It might be related to how ( and if ) you configured your NamingStrategy

这可能与您如何(以及是否)配置您的 NamingStrategy

http://matthew.mceachen.us/blog/hibernate-naming-strategies-20.html

http://matthew.mceachen.us/blog/hibernate-naming-strategies-20.html

or

或者

if you're using MySQL to weather or not you've enabled case sensitive table/column names http://dev.mysql.com/doc/refman/5.0/en/identifier-case-sensitivity.html

如果您使用 MySQL 来适应天气,您是否启用了区分大小写的表/列名称http://dev.mysql.com/doc/refman/5.0/en/identifier-case-sensitive.html