【发布时间】:2011-06-15 01:00:25
【问题描述】:
我已经成功配置了 spring 3 + jpa 2.0。当我这样做时:
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
【问题讨论】:
-
能否请您发布您的持久性 xml
-
如果你将这个类添加到你的持久化单元中会起作用吗?
-
请贴出你的spring配置文件的相关部分
-
如果我添加类仍然不起作用。我发布了 spring conf 文件