首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未能找到类- Spring 3、Hibernate 3、JPA

未能找到类- Spring 3、Hibernate 3、JPA
EN

Stack Overflow用户
提问于 2016-06-10 19:45:28
回答 1查看 226关注 0票数 0

看来,JPA底层结构没有找到我的类。我猜春天就是找不到吗?我尝试过包扫描并将类添加到persistence.xml中。有谁对我还能尝试让我的选择找到那个类(和底层表)有任何想法?谢谢!

这是密码。

persistence.xml

代码语言:javascript
复制
<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代码:

代码语言:javascript
复制
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);

引发的错误:

代码语言:javascript
复制
 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

代码语言:javascript
复制
<context:annotation-config />
<context:component-scan base-package="com.src.dao" />

在豆子里也试过

代码语言:javascript
复制
<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>

用户类别:

代码语言:javascript
复制
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添加类:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-13 18:22:58

终于弄明白了。看来,基于我的Spring (3.0.5),我确实需要在persistence.xml中添加类。但是,我的上述错误不允许这样做。结果发现..。我需要将包中的所有类添加到persistence.xml (减去DAO的)。一旦我这样做,应用程序启动和工作!不知道为什么他们都需要。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37756045

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档