2011-01-25 50 views
2

我已成功配置spring 3 + jpa 2.0。当我这样做:spring 3 + jpa 2 - 查询中完全合格的类名称

em.createQuery("from SystemUser",SystemUser.class).getResultList(); 

我收到以下异常:

java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: SystemUser is not mapped [from SystemUser] 
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1201) 
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1147) 

但是当我键入完全合格的类名:

em.createQuery("from com.aims.domain.SystemUser",SystemUser.class).getResultList(); 

它的工作原理。有没有人有什么配置我错过了。

我的persistence.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence-unit name="aims" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <properties> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/> 
     <!-- value="create" to build a new database on each run; value="update" to modify an existing database; value="create-drop" means the same as "create" but also drops tables when Hibernate closes; value="validate" makes no changes to the database --> 
     <property name="hibernate.hbm2ddl.auto" value="validate"/> 
     <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.DefaultNamingStrategy"/> 
     <property name="hibernate.connection.charSet" value="UTF-8"/> 

    </properties> 
</persistence-unit> 

我appContext.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> 


    <context:property-placeholder location="classpath*:META-INF/*.properties" ignore-unresolvable="true" /> 


    <context:annotation-config /> 

    <bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource"> 
     <property name="driverClassName" value="${database.driverClassName}"/> 
     <property name="url" value="${database.url}"/> 
     <property name="username" value="${database.username}"/> 
     <property name="password" value="${database.password}"/> 
    </bean> 
    <bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory"/> 

    </bean> 

    <bean id="entityManagerFactory" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" > 
     <property name="dataSource" ref="dataSource"></property> 
    </bean> 

    <context:component-scan base-package="com.aims.service" /> 
</beans> 

为了您的信息,我使用的JUnit与以下注释运行测试用例:

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath*:META-INF/spring/applicationContext*.xml" }) 
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false) 
@Transactional 
+0

可以请你发布你的持久性xml – Ralph 2011-01-25 09:41:19

+0

它会工作,如果你将类添加到你的持久性单元? – Ralph 2011-01-25 10:08:45

回答

1

你确定你已经将persistence.xml作为资源编译到包含你的数据库实体的jar中了吗? nd是否正确设置了持久化单元的名称(Spring的LocalContainerEntityManagerFactoryBean的persistenceUnitName属性)?

样品的persistence.xml(下META-INF /罐子里包含您的实体):

<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation=" http://java.sun.com/xml/ns/persistence 
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> 
    <persistence-unit name="persistenceUnit" transaction-type="RESOURCE_LOCAL"/> 
</persistence> 

样品Spring配置:

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="persistenceUnitName" value="persistenceUnit"/> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> 
    </property> 
</bean> 

编辑:另外,您将您的实体命名为:AMTB_SYSTEM_USERS,因此您的查询应为from AMTB_SYSTEM_USERS而不是from SystemUsers

2011-01-25 18:44:02,862 DEBUG [EntityBinder] - Import with entity name AMTB_SYSTEM_USERS