没有找到Spring / Hibernate JPA NamedQuery

时间:2015-02-12 08:54:05

标签: spring hibernate jpa

我有一个奇怪的问题:实体经理无法创建命名查询,我得到java.lang.IllegalArgumentException: Named query not found而方法(如find()merge())有效。 createQuery()也有效。我使用Hibernate作为JPA提供程序,使用Spring作为DI容器。代码和配置如下:
弹簧-beans.xml文件

<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-2.5.xsd
                    http://www.springframework.org/schema/context
                    http://www.springframework.org/schema/context/spring-context.xsd
                    http://www.springframework.org/schema/tx
                    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:component-scan base-package="org.example.testapp.database" />
<bean id="newsForm" class="org.example.testapp.presentation.form.NewsForm">
    <property name="newsMessage" ref="news" />
    <property name="newsList">
        <list>
            <ref bean="news" />
        </list>
    </property>
</bean>

<bean name="news" class="org.example.testapp.model.News">
</bean>

<bean name="/news" class="org.example.testapp.presentation.action.NewsAction"
    scope="prototype">
    <property name="dao" ref="dao" />
</bean>

<bean id="dao" class="org.example.testapp.database.dao.JpaHibernateNewsDao"
    scope="singleton">
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="org.example.testapp.model"/>
    <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>

<bean id="jpaVendorAdapter"
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="database" value="ORACLE" />
    <property name="showSql" value="true" />
    <property name="generateDdl" value="false" />
    <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
    <property name="username" value="login" />
    <property name="password" value="password" />
</bean>

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

<context:annotation-config/>
<tx:annotation-driven transaction-manager="transactionManager" />

news.java

 @Entity
@NamedQueries({
    @NamedQuery(name="News.select", query="select n from News n order by news_date" ),
    @NamedQuery(name="News.delete", query="delete from News where id in :value_list")
})
@Table(name="news")
public class News {

jpadao.java

public class JpaHibernateNewsDao implements Dao {

    @PersistenceContext
    private EntityManager entityManager;

    @SuppressWarnings("unchecked")
    @Override
    @Transactional
    public List<News> getList() throws DaoException {
        List<News> news = entityManager.createNamedQuery("News.select").getResultList();
        return news;
    }

1 个答案:

答案 0 :(得分:0)

找到答案:Spring刚刚没有看到@Entity,所以实际上没有这样的@NamedQuery。现在我必须弄清楚,为什么这个配置不起作用:

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="org.example.testapp.model"/>
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />