2

I am facing the with hibernates named query and transform to bean.

Here is the code:

query = session.getNamedQuery( "LAST_ADDED_DOC" );
query.setString( "module", inNpUploads.getModuleName() );
query.setString( "mapping", inNpUploads.getMappingId() );
query.setResultTransformer( Transformers.aliasToBean( NpUploads.class ) );
dmsDb = query.list();

but the exception is thrown for query.list():

org.hibernate.PropertyNotFoundException: Could not find setter for doc_id on class com.np.upload.pojo.NpUploads
    at org.hibernate.property.ChainedPropertyAccessor.getSetter(ChainedPropertyAccessor.java:44)
    at org.hibernate.transform.AliasToBeanResultTransformer.transformTuple(AliasToBeanResultTransformer.java:57)
    at org.hibernate.hql.HolderInstantiator.instantiate(HolderInstantiator.java:69)
    at org.hibernate.loader.custom.CustomLoader.getResultList(CustomLoader.java:330)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
    at org.hibernate.loader.Loader.list(Loader.java:2099)
    at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:289)
    at org.hibernate.impl.SessionImpl.listCustomQuery(SessionImpl.java:1695)
    at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
    at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:152)
    at com.np.upload.manager.FileUploadManager.listUploadedFiles(FileUploadManager.java:116)
    at com.np.upload.spring.controller.FileUploadController.prepareUploadPage(FileUploadController.java:222)
    at com.np.upload.spring.controller.FileUploadController.uploadPage(FileUploadController.java:84)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)

The hbm:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 30, 2015 10:32:57 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.np.upload.pojo.NpUploads" table="np_document_uploads" dynamic-update="true">
        <id name="docId" type="java.lang.Integer">
            <column name="doc_id" />
            <generator class="identity" />
        </id>
        <version name="version" type="java.lang.Integer">
            <column name="version" />
        </version>
        <property name="moduleName" type="string">
            <column name="module_name" not-null="true" />
        </property>
        <property name="mappingId" type="string">
            <column name="mapping_id" not-null="true" />
        </property>
        <property name="docType" type="string">
            <column name="doc_type" not-null="true" />
        </property>
        <property name="docName" type="string">
            <column name="doc_name" not-null="true" />
        </property>
        <property name="docPath" type="string">
            <column name="doc_path" length="1024" not-null="true" />
        </property>
        <property name="createdTime" type="timestamp" insert="false" update="false">
            <column name="created_time" length="19" />
        </property>
        <property name="modifiedTime" type="timestamp" insert="false" update="false">
            <column name="modified_time" length="19" />
        </property>
    </class>
    <sql-query name="LAST_ADDED_DOC">
       <![CDATA[SELECT d.* FROM np_document_uploads d
                JOIN(
                    SELECT module_name, mapping_id, doc_type, max(version) AS version
                    FROM np_document_uploads u
                    WHERE u.module_name=:module and u.mapping_id=:mapping
                    GROUP BY module_name, mapping_id, doc_type
                )tt USING( module_name, mapping_id, doc_type, version )
                WHERE d.module_name=:module and d.mapping_id=:mapping]]>
    </sql-query>
</hibernate-mapping>

and the pojo:

public class NpUploads
    implements java.io.Serializable
{
    private static final long serialVersionUID = -5063169354511880324L;
    private Integer           docId;
    private Integer           version;
    private String            moduleName;
    private String            mappingId;
    private String            docType;
    private String            docName;
    private String            docPath;
    private Date              createdTime;
    private Date              modifiedTime;

    // getter and setters for all properties are there.
}

Database schema:

+---------------+---------------+------+-----+-------------------+-----------------------------+
| Field         | Type          | Null | Key | Default           | Extra                       |
+---------------+---------------+------+-----+-------------------+-----------------------------+
| doc_id        | int(11)       | NO   | PRI | NULL              | auto_increment              |
| module_name   | varchar(255)  | NO   |     | NULL              |                             |
| mapping_id    | varchar(255)  | NO   |     | NULL              |                             |
| doc_type      | varchar(255)  | NO   |     | NULL              |                             |
| doc_name      | varchar(255)  | NO   |     | NULL              |                             |
| doc_path      | varchar(1024) | NO   |     | NULL              |                             |
| version       | int(6)        | YES  |     | NULL              |                             |
| created_time  | timestamp     | YES  |     | CURRENT_TIMESTAMP |                             |
| modified_time | timestamp     | YES  |     | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+---------------+---------------+------+-----+-------------------+-----------------------------+
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117

3 Answers3

1

this answer is looking horrable but i was facing same issue. some time hibernate does not detect the properties setter and getter. i solved this problem by deleting all setter and getters from bean and then again create same by eclipse shorcut. it is working. you can do the same.

Musaddique S
  • 1,539
  • 2
  • 15
  • 36
0

As you are using Transformers.aliasToBean which uses the alias names to find the setter methods of your resultClass (NpUploads)

In order to make Transformers.aliasToBean work properly you need to use proper alias names in your query. If the property name in your NpUploads class is docId, you should use select doc_id as docId.

Remove d.* in you query and use individual column with proper aliases

Modify below and check link for reference:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Sep 30, 2015 10:32:57 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="com.np.upload.pojo.NpUploads" table="np_document_uploads" dynamic-update="true">
        <id name="docId" type="java.lang.Integer">
            <column name="doc_id" />
            <generator class="identity" />
        </id>
        <version name="version" type="java.lang.Integer">
            <column name="version" />
        </version>
        <property name="moduleName" type="string">
            <column name="module_name" not-null="true" />
        </property>
        <property name="mappingId" type="string">
            <column name="mapping_id" not-null="true" />
        </property>
        <property name="docType" type="string">
            <column name="doc_type" not-null="true" />
        </property>
        <property name="docName" type="string">
            <column name="doc_name" not-null="true" />
        </property>
        <property name="docPath" type="string">
            <column name="doc_path" length="1024" not-null="true" />
        </property>
        <property name="createdTime" type="timestamp" insert="false" update="false">
            <column name="created_time" length="19" />
        </property>
        <property name="modifiedTime" type="timestamp" insert="false" update="false">
            <column name="modified_time" length="19" />
        </property>
        <loader query-ref="LAST_ADDED_DOC"/>
    </class>
    <sql-query name="LAST_ADDED_DOC">
        <return alias="up" class="NpUploads"/>       
        <![CDATA[SELECT d.doc_id as {up.docId},d.module_name as {up.moduleName},d.mapping_id as {up.mappingId},d.doc_type as {up.docType},d.doc_name as {up.docName},d.version as {up.version},d.doc_path as {up.docPath},d.created_time as {up.createdTime},d.modified_time as {up.modifiedTime} FROM np_document_uploads d
            JOIN(
                SELECT module_name, mapping_id, doc_type, max(version) AS version
                FROM np_document_uploads u
                WHERE u.module_name=:module and u.mapping_id=:mapping
                GROUP BY module_name, mapping_id, doc_type
            )tt USING( module_name, mapping_id, doc_type, version )
            WHERE d.module_name=:module and d.mapping_id=:mapping]]>
    </sql-query>
</hibernate-mapping>
Kunal Surana
  • 659
  • 5
  • 14
  • I have changed the `d.*` to `d.doc_id as docId,d.module_name as moduleName,d.mapping_id as mappingId,d.doc_type as docType,d.doc_name as docName,d.version,d.doc_path as docPath,d.created_time as createdTime,d.modified_time as modifiedTime` and now the exception says: `IntegerType] - could not read column value from result set: doc_id; Column 'doc_id' not found. JDBCExceptionReporter] - SQL Error: 0, SQLState: S0022 JDBCExceptionReporter] - Column 'doc_id' not found` – Mohammad Faisal Feb 22 '16 at 12:02
  • check link http://stackoverflow.com/questions/7118703/query-using-alias-on-column-give-an-error – Kunal Surana Feb 22 '16 at 12:37
  • he had used `hql` and I am using named query. how do it supposed to help? – Mohammad Faisal Feb 22 '16 at 12:44
  • what is the version of mysql connector ? – Kunal Surana Feb 22 '16 at 12:57
  • I am connecting it via `jdbc:datasource` in jboss – Mohammad Faisal Feb 22 '16 at 12:58
  • did you tried removing aliases as you mentioned in your comment. – Sindhoo Oad Feb 22 '16 at 13:01
  • check the version of mysql connector in your build path ? – Kunal Surana Feb 22 '16 at 13:02
  • check link http://stackoverflow.com/questions/13834777/hibernate-exception-propertynotfoundexception-when-using-transformer – Kunal Surana Feb 22 '16 at 13:09
  • I've just had this problem with some legacy code - when the query returns it capitalises the column names by default (using Oracle), so I had to put an explicit column alias in which preserved the case. In Oracle, this is done by using double quotes around the column alias in the query. `String SQL = "SELECT d.doc_id AS \"DocId\""` which then allowed the bean property to be derived properly. – James Jul 19 '22 at 05:22
0

The types declared and used in the mapping files are neither Java data types nor SQL database types. Instead, they are Hibernate mapping types. Hibernate mapping types are converters which translate between Java and SQL data types.

So java.lang.Integer is java type not hibernate mapping type, see Hibernate types

Try changing java.lang.Integer to integer

<id name="docId" type="integer">
     <column name="doc_id" />
     <generator class="identity" />
</id>
Sindhoo Oad
  • 1,194
  • 2
  • 13
  • 29