Issue
I am working on an app with Hibernate 5.6, Spring 5.3 and we use xml-based configuration + Hibernate mapping files .hbm.xml for each entity. We did not annotate model classes.
After migrating from Hibernate 3 to 5.6, we had to use the Springs transactionManager, which workes fine so far. But since the Hibernate Criteria API is deprecated, I tried to migrate some of the Criteria API request to CriteriaBuilder and set up some boilerplate code.
public List<ENTITY> findByCriteria(final Map<String, Object> criteriaMap, final List<String> fields, final Class<ENTITY> entityClass) {
CriteriaBuilder cb = this.getSession().getCriteriaBuilder();
CriteriaQuery<ENTITY> cq = cb.createQuery(entityClass);
Root<ENTITY> root = cq.from(entityClass);
...
But when I execute the code, the last line throws me this error:
Caused by: java.lang.IllegalArgumentException: Not an entity: interface com.myapp.pm.common.business.PmPropertyVO
at deployment.myapp-clienta.war//org.hibernate.metamodel.internal.MetamodelImpl.entity(MetamodelImpl.java:567)
at deployment.myapp-clienta.war//org.hibernate.query.criteria.internal.QueryStructure.from(QueryStructure.java:128)
at deployment.myapp-clienta.war//org.hibernate.query.criteria.internal.CriteriaQueryImpl.from(CriteriaQueryImpl.java:158)
at deployment.myapp-clienta.war//com.myapp.integration.dao.impl.CommonHibernateDAOImpl.findByCriteria(CommonHibernateDAOImpl.java:554)
...
My configuration looks like this:
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="mappingLocations" value="classpath*:/com/*/*/**/integration/hbm/*.hbm.xml" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">${hibernate.cglib.use_reflection_optimizer}</prop>
<prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
<prop key="hibernate.connection.release_mode">${hibernate.connection.release_mode}</prop>
</props>
</property>
<property name="dataSource" ref="dataSource"/>
</bean>
I tried to add another property, but that did not help:
<property name="packagesToScan" value="classpath*:/com/*/*/**/integration/hbm/*.hbm.xml" />
The mapping file for the Entity:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.myapp.business.impl.PmPropertyVOImpl" table="pm_t_property" proxy="com.myapp.pm.common.business.PmPropertyVO">
<id name="rowguid" column="rowguid" type="java.lang.Long">
<generator class="sequence">
<param name="sequence_name">pm_t_property_rowguid_seq</param>
</generator>
</id>
<property name="key" type="java.lang.String" column="py_key" not-null="true" />
<property name="value" type="java.lang.String" column="py_value" not-null="true" />
<property name="description" type="java.lang.String" column="py_description" not-null="false" />
<property name="owner" type="java.lang.String" column="py_owner" not-null="true" />
<property name="type" type="java.lang.String" column="py_type" not-null="false" />
<property name="creationDate" type="timestamp" column="py_creation_date" />
<property name="creationUser" type="java.lang.String" column="py_creation_user" />
<property name="modificationDate" type="timestamp" column="py_modification_date" />
<property name="modificationUser" type="java.lang.String" column="py_modification_user" />
</class>
</hibernate-mapping>
My question: is it mandatory to use the persistence.xml file when using the CriteriaBuilder? We did not use this kind of configuration before, just plain Hibernate configs.
Edit: After comment from Alexander Kashpirovsky I tried to change this line to get the implementation instead of interface class. But all I get is a reference to java.lang.class
CriteriaQuery cq = cb.createQuery(entityClass.getClass());
Solution
Variable entityClass
equals to com.myapp.business.impl.PmPropertyVOImpl
or com.myapp.pm.common.business.PmPropertyVO
?
Must be com.myapp.business.impl.PmPropertyVOImpl
Answered By - Alexander Kashpirovsky
Answer Checked By - Willingham (JavaFixing Volunteer)