【问题标题】:spring 3 + jpa 2 - fully qualified class name in queriesspring 3 + jpa 2 - 查询中的完全限定类名
【发布时间】: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 文件

标签: hibernate spring jpa-2.0


【解决方案1】:

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

示例 persistence.xml(在包含您的实体的 jar 中的 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

【讨论】:

  • 以上所有配置我已经完成了。除了将资源打包为 jar。由于我制作 Web 应用程序,所有类和配置都将在 war 文件中
  • 您可以尝试将数据库实体分离到自己的 jar 中(使用 persistence.xml),并将该 jar 作为依赖项包含在您的战争中吗?
  • @Adelave 我会重新考虑。把所有东西都放在一场战争中是糟糕的设计。我会为持久层创建一个 jar,为服务层等创建一个 jar,然后将它们放在 WEB-INF/lib
  • 无论设计是否糟糕,将其分离到 jar 中是否可以解决问题?我试图将persistence.xml 和数据访问类分离到单独的jar 中,并包含在我的war 文件中。还是有同样的问题。
  • @Adelave 我正在运行一个应用程序,其中战争包含一个包含 persistence.xml 和实体类的 jar,它可以在没有完整限定名的情况下工作 - 所以我猜这不是原因。
猜你喜欢
  • 1970-01-01
  • 2020-11-12
  • 1970-01-01
  • 2011-11-08
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-04
相关资源
最近更新 更多