看来,JPA底层结构没有找到我的类。我猜春天就是找不到吗?我尝试过包扫描并将类添加到persistence.xml中。有谁对我还能尝试让我的选择找到那个类(和底层表)有任何想法?谢谢!
这是密码。
persistence.xml
<persistence-unit name="myApp" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:joss/datasources/myDB</jta-data-source>
<!-- get a special error with this in - see below -->
<!-- <class>com.src.dao.User</class> -->
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.archive.autodetection" value="class,hbm" />导致错误的UserDAO代码:
final String queryString="select model from " +User.class.getSimpleName() + " model where model."propertyName+"=:propertyValue";
return getJpaTemplate().executeFind(new JpaCallback() {
@Override
public Object doInJpa(EntityManager em) throws PersistenceException {
//this line causes the error below
Query query = em.createQuery(queryString);引发的错误:
201-06-10 18:59:32,736 ERROR [UserDAO] find by property name failed
org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: User is not mapped [select model from User model where model.value= :propertyValue]; nested exception is java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException:User is not mapped [select model from User model where model.value= :propertyValue]
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:286)applicationcontext.xml
<context:annotation-config />
<context:component-scan base-package="com.src.dao" />在豆子里也试过
<beans:bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" packagesToScan="com.src.dao">
<beans:property name="persistenceUnitName" value="myApp" />
<beans:property name="packagesToScan" value="com.src.dao" />
</beans:bean>用户类别:
package com.src.dao
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
/**
* User Entity
*
* @author MyEclipse Persistence Tools
*/
@Entity
@Table (name="USERS", schema="myDb")
public class User extends AbstractUser implements java.io.Serializable, Comparable<User> {向persistence.xml添加类:
When I add in <class> to my persistence.xml I get the following on app deploy. The war never deploys:
Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/configs/spring/stu-hibernate.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: myApp] Unable to build EntityManagerFactory
at org.springframework.beans.factory.annotation.AutoWiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)发布于 2016-06-13 18:22:58
终于弄明白了。看来,基于我的Spring (3.0.5),我确实需要在persistence.xml中添加类。但是,我的上述错误不允许这样做。结果发现..。我需要将包中的所有类添加到persistence.xml (减去DAO的)。一旦我这样做,应用程序启动和工作!不知道为什么他们都需要。
https://stackoverflow.com/questions/37756045
复制相似问题