我正在尝试配置一个简单的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);
}
}
}
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.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
你能告诉我为什么这个例外会出现并且我做错了吗?提前谢谢。
答案 0 :(得分:1)
这是你的问题:
private Long id;
id
是Long
,null
,因为它没有实例化。
当JPA通过getId()
方法访问它时,它需要long
而不是Long
,因此访问者正在尝试将null
转换为原始值,因此NullPointerException
。
最佳解决方案是:1)将您的id
更改为long
,并为@NotNull
方法添加getId()
注释。
我希望这会有所帮助。