Hibernate 春季3&x2B;jpa 2-查询中的完全限定类名
我已经成功地配置了Spring3+JPA2.0。当我这样做时:Hibernate 春季3&x2B;jpa 2-查询中的完全限定类名,hibernate,spring,jpa-2.0,Hibernate,Spring,Jpa 2.0,我已经成功地配置了Spring3+JPA2.0。当我这样做时: em.createQuery("from SystemUser",SystemUser.class).getResultList(); 我收到以下例外情况: java.lang.IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: SystemUser is not mapped [from SystemUser] at org.hibernat
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();
它起作用了。有人知道我错过了什么吗
My 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>
是否确实已将persistence.xml作为资源编译到包含数据库实体的jar中,并且持久化单元的名称(Spring的LocalContainerEntityManagerFactoryBean的persistenceUnitName属性)设置正确 示例persistence.xml(在包含实体的jar中的META-INF/下):
以上所有配置我都完成了。除了将资源打包为jar。既然我制作web应用程序,所有的类和配置都将在war文件中,你能试着将数据库实体分离到它自己的jar中(使用persistence.xml)并将该jar作为依赖项包含在你的war中吗?@Adelave我会重新考虑这一点。把一切都放在一场战争中是糟糕的设计。我会为持久层创建一个jar,为服务层创建一个jar,等等。我会将它们放在WEB-INF/lib中,不管设计是否糟糕,将它们分离到jar中是否能解决问题?我尝试将persistence.xml和数据访问类分离到单独的jar中,并将它们包含在我的war文件中。还是有同样的问题。@Adelave我运行了一个应用程序,其中war包含一个jar,其中包含persistence.xml和实体类,如果你把类添加到你的持久化单元中,它会工作吗?如果我添加类仍然不工作,请发布spring配置文件的相关部分。我发布了spring配置文件
<?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>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:META-INF/spring/applicationContext*.xml" })
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
@Transactional
<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>
<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>
2011-01-25 18:44:02,862 DEBUG [EntityBinder] - Import with entity name AMTB_SYSTEM_USERS