开发者

spring 3 + jpa 2 - fully qualified class name in queries

开发者 https://www.devze.com 2023-02-07 02:02 出处:网络
I\'ve successfully configured spring 3 + jpa 2.0. When I do: em.createQuery(\"from SystemUser\",SystemUser.class).getResultList();

I've successfully configured spring 3 + jpa 2.0. When I do:

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

I receive following exception :

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)

But when i type fully qualified class name:

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

It works. Does anyone what configurations i missed out.

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>

My appContext.xml:

<?xml version="1.0" enco开发者_如何学编程ding="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> 

For your information i'm running test case using junit with following annotation:

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


Are you sure that you have compiled, as a resource, persistence.xml into the jar that contains your database entities and that the name of the persistence unit (persistenceUnitName property for Spring's LocalContainerEntityManagerFactoryBean) is set properly?

Sample persistence.xml (under META-INF/ in the jar that contained your entities):

<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>

Sample Spring Config:

<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>

EDIT: Also, you named your entity: AMTB_SYSTEM_USERS, so your query should be from AMTB_SYSTEM_USERS instead of from SystemUsers

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

精彩评论

暂无评论...
验证码 换一张
取 消