我正在使用Hibernate 5.6和Spring 5.3开发一个应用程序,我们使用基于xml的配置+ HibernateMap文件.hbm.xml作为每个实体。
在从Hibernate 3迁移到5.6之后,我们不得不使用Springs transactionManager,目前为止它工作得很好。但是由于Hibernate Criteria API已被弃用,我尝试将一些Criteria API请求迁移到CriteriaBuilder并设置一些样板代码。
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);
...
但是当我执行代码时,最后一行抛出了这个错误:
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)
...
我的配置如下所示:
<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>
我尝试添加另一个属性,但没有帮助:
<property name="packagesToScan" value="classpath*:/com/*/*/**/integration/hbm/*.hbm.xml" />
实体的Map文件:
<?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>
我的问题:在使用CriteriaBuilder时是否必须使用persistence.xml文件?我们以前没有使用过这种配置,只是使用普通的Hibernate配置。
**编辑:**在 Alexandria Kashpirovsky的注解之后,我试图修改此行以获得实现而不是接口类。但我得到的只是对java.lang.class的引用
CriteriaQuery cq = cb.createQuery(entityClass.getClass());
1条答案
按热度按时间fykwrbwg1#
变量
entityClass
等于com.myapp.business.impl.PmPropertyVOImpl
还是com.myapp.pm.common.business.PmPropertyVO
?必须为
com.myapp.business.impl.PmPropertyVOImpl