无法设置简单的JPA

时间:2013-08-03 21:27:21

标签: java jpa

我正在尝试配置一个简单的JPA项目而且非常困难。

我有一个工作的dataSource(Oracle数据库),其表“SIMPLE”包含ID(NUMBER)和WORD(VARCHAR2)。

我一直得到一个例外,即我的命名查询出现了问题:SimpleObject.findById,SimpleObject.findAll,但它们看似简单,我不认为我在那里犯了错误。

这是包含查询的类。我添加了所有其他类和完整的例外。

SimpleObject类,用于映射表中的数据

package lt.tomas.proSpring3;

import javax.persistence.*;

@Entity
@Table(name = "simple")
@NamedQueries({
    @NamedQuery(name="SimpleObject.findAll",query = "SELECT c FROM Simple c"),
    @NamedQuery(name="SimpleObject.findById" , query = "SELECT c FROM Simple c where c.id = :id")
})
public class SimpleObject {


    private Long id;
    private String word;

    @Id
    @Column(name = "ID")
    public long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }


    @Column(name = "WORD")
    public String getWord(){
        return word;
    }

    public void setWord(String word){
        this.word = word;
    }

    public String toString(){
        return "ID: " + id + " WORD: " + word ; 
    }


}

在simpleObject

上执行数据操作的SimpleObjectImpl类
 package lt.tomas.proSpring3;

import java.util.List;

import javax.persistence.*;

import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service("jpaSimpleService")
@Repository
@Transactional
public class SimpleObjectImpl{

    @PersistenceContext(unitName="emf")
    private EntityManager em;

    @Transactional(readOnly=true)
    public List<SimpleObject> findAll(){
        return em.createNamedQuery("SimpleObject.findAll", SimpleObject.class).getResultList();
    }

    @Transactional(readOnly=true)
    public SimpleObject findById(Long id){
        TypedQuery<SimpleObject> tq = em.createNamedQuery("SimpleObject.findById", SimpleObject.class);
        tq.setParameter("ID", id);
        return tq.getSingleResult();
    }

    public void save(SimpleObject s) {
        em.persist(s);
    }

    public void update(SimpleObject s){
        em.merge(s);
    }

    public void Delete(SimpleObject c) {
        // TODO Auto-generated method stub

    }

}

测试一切的课程

package lt.tomas.proSpring3;

import java.util.List;

import org.springframework.context.support.GenericXmlApplicationContext;

public class JPATest {

    public static void main(String[] args) {
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
        ctx.load("app-context.xml");
        ctx.refresh();

        SimpleObjectImpl si = ctx.getBean("jpaSimpleService", SimpleObjectImpl.class);
        List<SimpleObject> objects = si.findAll();
        for(SimpleObject obj : objects){
            System.out.println(obj);
        }
    }
}

Context config.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:jdbc="http://www.springframework.org/schema/jdbc" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName">
            <value>${jdbc.driverClassName}</value>
        </property>
        <property name="url">
            <value>${jdbc.url}</value>
        </property>
        <property name="username">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>
    </bean>

    <context:property-placeholder location="jdbc.properties"/>

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

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

    <bean id="emf"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="packagesToScan" value="lt.tomas.proSpring3" />
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.Oracle10gDialect
                </prop>
                <prop key="hibernate.max_fetch_depth">3</prop>
                <prop key="hibernate.jdbc.fetch_size">50</prop>
                <prop key="hibernate.jdbc.batch_size">10</prop>
                <prop key="hibernate.show_sql">true</prop>
            </props>
        </property>
    </bean>

    <context:annotation-config />

    <context:component-scan base-package="lt.tomas.proSpring3"/>
</beans>

异常

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [app-context.xml]: Cannot resolve reference to bean 'emf' while setting bean property 'entityManagerFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emf' defined in class path resource [app-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
    at lt.tomas.proSpring3.JPATest.main(JPATest.java:12)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'emf' defined in class path resource [app-context.xml]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:322)
    ... 13 more
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build EntityManagerFactory
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915)
    at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:74)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:257)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 20 more
Caused by: org.hibernate.HibernateException: Errors in named queries: SimpleObject.findById, SimpleObject.findAll
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:426)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:906)
    ... 25 more

你能告诉我为什么这个例外会出现并且我做错了吗?提前谢谢。

1 个答案:

答案 0 :(得分:1)

JPA NamedQuery不允许您对数据库表执行选择,而是使用实体类名称(区分大小写)。

因此,将SELECT c from Simple c更改为SELECT c to SimpleObject c

所以这是正确答案:

@NamedQuery(name="SimpleObject.findAll",query = "SELECT c FROM SimpleObject c"),
@NamedQuery(name="SimpleObject.findById" , query = "SELECT c FROM SimpleObject c where c.id = :id")

我希望这会有所帮助。