ResultTransformer with createSQLQuery forces no camelCase in entity fields

前端 未结 3 530
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-07 03:10

I have an sql query as follows:

List employees = getCurrentSession()
                    .createSQLQuery(
                            \"select\"
         


        
相关标签:
3条回答
  • 2021-02-07 03:23

    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).

    0 讨论(0)
  • 2021-02-07 03:40

    You can use addScalar(String columnAlias, Type type) to explicitly declare the column alias of your native 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();
    
    0 讨论(0)
  • 2021-02-07 03:44

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

    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

    0 讨论(0)
提交回复
热议问题