1

我试图配置一个简单的 JPA 项目,但我很困惑。

我有一个工作dataSource(Oracle DB),其表SIMPLE包含一个ID(NUMBER)和一个WORD(VARCHAR2).

仅当我在课堂上添加@GeneratedValue注释时,我才不断收到异常。SimpleObject如果没有注释,我可以从数据库表中获取数据,但无法保存新记录。

我试图改变GeneratedValue策略

@GeneratedValue(strategy=GenerationType.SEQUENCE) 

我的数据库中有一个序列 simple_seq 所以我尝试这样做:

@Id 
@GeneratedValue(generator="mySeq") 
@SequenceGenerator(name="mySeq",sequenceName="SIMPLE_SEQ") 
private long id;

我一直得到同样的例外..

这是我的课程:

映射表中数据的 SimpleObject 类

package lt.tomas.proSpring3;

import javax.persistence.*;

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


    private Long id;
    private String word;

    @Id
    @GeneratedValue
    @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);
        }
    }
}

上下文 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.PropertyAccessException: Exception occurred inside getter of lt.tomas.proSpring3.SimpleObject.id
    at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:175)
    at org.hibernate.engine.UnsavedValueFactory.getUnsavedIdentifierValue(UnsavedValueFactory.java:67)
    at org.hibernate.tuple.PropertyFactory.buildIdentifierProperty(PropertyFactory.java:67)
    at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:135)
    at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:485)
    at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:133)
    at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:84)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:286)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872)
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:906)
    ... 25 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:172)
    ... 34 more
Caused by: java.lang.NullPointerException
    at lt.tomas.proSpring3.SimpleObject.getId(SimpleObject.java:21)
    ... 39 more

你能告诉我为什么会出现这个异常,我做错了吗?先感谢您。

4

1 回答 1

1

这是你的问题:

private Long id;

id是 a Long,这是null因为它没有被实例化。

当 JPA 通过该getId()方法访问它时,它需要 along而不是Long,因此访问器试图将 a 转换null为原始值,因此 a NullPointerException

最好的解决方案是 1) 将您更改id为 a并向该方法long添加@NotNull注释。getId()

我希望这有帮助。

于 2013-08-03T22:06:36.400 回答