引起:javax.persistence.PersistenceException:插入对象

时间:2012-10-03 22:52:13

标签: sql jpa persistence

SRAssignment.java

@Entity
@Table(name="SR_ASSIGNEMENT", uniqueConstraints= { @UniqueConstraint
(columnNames="SRAID")})
@SequenceGenerator(name="SRASSIGNMENT_SEQUENCE", initialValue=1, allocationSize=1)
public class SRAssignment implements Serializable{

@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, 
generator="SRASSIGNMENT_SEQUENCE")
@Column(name="SRAID", nullable=false)
private long sraId;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="EMPNO")
private Employee employee;

@OneToOne(fetch=FetchType.EAGER, mappedBy="srNo", optional=false)
private ServiceRequest serviceRequest;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="assignedBy")
private Employee assignedBy;

@Column(name="ASSIGNEDON")
@Temporal(TemporalType.TIMESTAMP) 
private Date assignedOn;

@OneToOne(fetch=FetchType.LAZY, optional=true, mappedBy="slotNo")
private ScheduleSlot startSlot;

@OneToOne(fetch=FetchType.LAZY , optional=true, mappedBy="slotNo")
private ScheduleSlot expectedEndSlot;

@OneToOne(fetch=FetchType.LAZY , optional=true, mappedBy="slotNo")
private ScheduleSlot actualEndSlot;

@Column(name="SRA_COMMENT", nullable=true)
private String comment;

@Column(name="ATTACHMENT", nullable=true)
private Object attachment;

....

ServiceRequest.java

@Entity
@Table(name="SERVICE_REQUEST", uniqueConstraints = {@UniqueConstraint(columnNames=     
{"SRNO"})})
@SequenceGenerator(name="SERVICE_REQUEST_SEQUENCE", initialValue=1, allocationSize=100)
public class ServiceRequest implements Serializable {

@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, 
generator="SERVICE_REQUEST_SEQUENCE")
@Column(name="SRNO",  nullable=false)
private long srNo;

@Column(name="DESCRIPTION")
private String description;

@Column(name="STATUS") 
private int status;


@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="CUSTNO", referencedColumnName="CustNO")
private Customer customer;

@Column(name="DATECREATED")
private Date dateCreated;

@Column(name="DATEUPDATED")
private Date dateUpdated;
...


ServiceRequest request = workPersistence.getRequestById(assigner.getRequestId());
Employee employee = workPersistence.getEmployeeById(assigner.getTechnician());
Employee manager = workPersistence.getEmployeeById(assigner.getAssignedBy());
SRAssignment assignment = new SRAssignment();

assignment.setEmployee(employee);
assignment.setAssignedBy(manager);
assignment.setServiceRequest(request);
assignment.setAssignedOn(Calendar.getInstance().getTime());

EntityManager em = entityManagerFactory.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
em.persist(assignment);
em.refresh(assignment);
System.out.println("refresh assignment, now commiting...");
tx.commit();

我得到的错误是:

刷新作业,现在提交...... javax.persistence.RollbackException:事务未能提交         在org.datanucleus.api.jpa.JPAEntityTransaction.commit(JPAEntityTransaction.java:118)         在com.ge.dsp.iwork.persistence.JpaWorkPersistence.assignRequest(JpaWorkPersistence.java:417)         在com.ge.dsp.iwork.impl.WorkServiceImpl.getRequestAssigned(WorkServiceImpl.java:146)         在com.ge.dsp.iwork.impl.WorkServiceImpl.assignRequest(WorkServiceImpl.java:112)         在com.ge.dsp.iwork.test.WorkServiceTest.testServiceRequestAssignment(WorkServiceTest.java:85)         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)         在java.lang.reflect.Method.invoke(Method.java:597)         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1581)         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1522)         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)         在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:294)         在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)         在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)         在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)         at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)         在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)         在org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.access $ 1600(AbstractDelegatedExecutionApplicationContext.java:69)         at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext $ 4.run(AbstractDelegatedExecutionApplicationContext.java:355)         在org.springframework.osgi.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)         在org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:320)         在org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor $ CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:132)         在java.lang.Thread.run(Thread.java:662) 引起:javax.persistence.PersistenceException:插入对象" com.ge.dsp.iwork.entity.SRAssignment@7edab86b"使用语句" INSERT INTO SR_ASSIGNEMENT(ASSIGNEDBY,SRA_COMMENT,EMPNO,ASSIGNEDON,SRAID)VALUES(?,?,?,?,?)"失败 :ORA-01400:无法插入NULL(" DSP_QA_4"。" SR_ASSIGNEMENT"。" SRNO")

    at org.datanucleus.api.jpa.NucleusJPAHelper.getJPAExceptionForNucleusException(NucleusJPAHelper.java:270)
    at org.datanucleus.api.jpa.JPAEntityTransaction.commit(JPAEntityTransaction.java:116)
    ... 25 more

引起:java.sql.SQLIntegrityConstraintViolationException:ORA-01400:无法将NULL插入(" DSP_QA_4"。" SR_ASSIGNEMENT"。" SRNO")

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:439)
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:395)
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:802)
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:436)
    at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186)
    at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:521)
    at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:205)
    at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:1008)
    at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1307)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3449)
    at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3530)
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeUpdate(OraclePreparedStatementWrapper.java:1350)
    at org.datanucleus.store.rdbms.datasource.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    at org.datanucleus.store.rdbms.datasource.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:105)
    at org.datanucleus.store.rdbms.SQLController.executeStatementUpdate(SQLController.java:419)
    at org.datanucleus.store.rdbms.request.InsertRequest.execute(InsertRequest.java:411)
    at org.datanucleus.store.rdbms.RDBMSPersistenceHandler.insertTable(RDBMSPersistenceHandler.java:166)
    at org.datanucleus.store.rdbms.RDBMSPersistenceHandler.insertObject(RDBMSPersistenceHandler.java:142)
    at org.datanucleus.state.JDOStateManager.internalMakePersistent(JDOStateManager.java:2377)
    at org.datanucleus.state.JDOStateManager.flush(JDOStateManager.java:3769)
    at org.datanucleus.ObjectManagerImpl.flushInternalWithOrdering(ObjectManagerImpl.java:3884)
    at org.datanucleus.ObjectManagerImpl.flushInternal(ObjectManagerImpl.java:3807)
    at org.datanucleus.ObjectManagerImpl.flush(ObjectManagerImpl.java:3747)
    at org.datanucleus.ObjectManagerImpl.preCommit(ObjectManagerImpl.java:4137)
    at org.datanucleus.ObjectManagerImpl.transactionPreCommit(ObjectManagerImpl.java:428)
    at org.datanucleus.TransactionImpl.internalPreCommit(TransactionImpl.java:400)
    at org.datanucleus.TransactionImpl.commit(TransactionImpl.java:288)
    at org.datanucleus.api.jpa.JPAEntityTransaction.commit(JPAEntityTransaction.java:103)
    ... 25 more

为什么它没有获取serviceRequest信息,并且没有在作业中插入SRNO? 当我尝试跟随时也处于相同的关系:

EntityManager em = entityManagerFactory.createEntityManager();
Query query = em
.createQuery(
"SELECT assignment FROM SRAssignment assignment WHERE assignment.sraId = :sraId")
            .setParameter("sraId", new Long(assignmentId));
List<SRAssignment> assignments = query.getResultList();
SRAssignment sra = (SRAssignment) assignments.get(0);
    System.out.println(sra.getSraId());
    System.out.println(sra.getEmployee().getEmpNo());
    System.out.println("________________________________________");
    System.out.println(sra.getServiceRequest().getSrNo());
    System.out.println();
    em.close();

一旦我上线         的的System.out.println(sra.getServiceRequest()getSrNo());

我收到错误:     的 _ __ _ __ _ __ _ __ _ __ _ __ _ ___ 线程中的异常&#34; SpringOsgiExtenderThread-88&#34; org.springframework.beans.factory.BeanCreationException:创建名称为&#39; testGetAssignmentById&#39;的bean时出错在URL [bundle://178.33:0 / META-INF / spring / module-context.xml]中定义:Inv init方法的ocation失败;嵌套异常是javax.persistence.PersistenceException:无法执行操作&#34; ==&#34;在org.datanucleus.store.rdbms.sql.expression.ObjectExpression@843249a和org.datanucleus.store.rdbms.sql.express上 ion.NumericExpression@7017feb2         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)         在org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:294)         在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)         在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)         在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)         at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)         在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)         在org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.access $ 1600(AbstractDelegatedExecutionApplicationContext.java:69)         at org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext $ 4.run(AbstractDelegatedExecutionApplicationContext.java:355)         在org.springframework.osgi.util.internal.PrivilegedUtils.executeWithCustomTCCL(PrivilegedUtils.java:85)         在org.springframework.osgi.context.support.AbstractDelegatedExecutionApplicationContext.completeRefresh(AbstractDelegatedExecutionApplicationContext.java:320)         在org.springframework.osgi.extender.internal.dependencies.startup.DependencyWaiterApplicationContextExecutor $ CompleteRefreshTask.run(DependencyWaiterApplicationContextExecutor.java:132)         在java.lang.Thread.run(Thread.java:662) 引起:javax.persistence.PersistenceException:无法执行操作&#34; ==&#34;在org.datanucleus.store.rdbms.sql.expression.ObjectExpression@843249a和org.datanucleus.store.rdbms.sql.expression.NumericExpression@7017feb2         at org.datanucleus.api.jpa.NucleusJPAHelper.getJPAExceptionForNucleusException(NucleusJPAHelper.java:302)         at org.datanucleus.api.jpa.JPAAdapter.getApiExceptionForNucleusException(JPAAdapter.java:898)         at org.datanucleus.state.JDOStateManager.isLoaded(JDOStateManager.java:1736)         在com.ge.dsp.iwork.entity.SRAssignment.jdoGetserviceRequest(SRAssignment.java)         在com.ge.dsp.iwork.entity.SRAssignment.getServiceRequest(SRAssignment.java:151)         在com.ge.dsp.iwork.persistence.JpaWorkPersistence.getAssignmentsByAssignmentId(JpaWorkPersistence.java:462)         在com.ge.dsp.iwork.test.WorkServiceTest.testGetAssignmentById(WorkServiceTest.java:59)         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)         在java.lang.reflect.Method.invoke(Method.java:597)         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1581)         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1522)         在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)         ......还有14个 引起:org.datanucleus.store.rdbms.sql.expression.IllegalExpressionOperationException:无法执行操作&#34; ==&#34;在org.datanucleus.store.rdbms.sql.expression.ObjectExpression@843249a和org.datanucleus.store.rdbms.sql.express上 ion.NumericExpression@7017feb2         在org.datanucleus.store.rdbms.sql.expression.SQLExpression.eq(SQLExpression.java:383)         at org.datanucleus.store.rdbms.sql.expression.ObjectExpression.eq(ObjectExpression.java:217)         在org.datanucleus.store.rdbms.sql.SQLStatement.getJoinConditionForJoin(SQLStatement.java:1232)         在org.datanucleus.store.rdbms.sql.SQLStatement.join(SQLStatement.java:1151)         在org.datanucleus.store.rdbms.sql.SQLStatement.leftOuterJoin(SQLStatement.java:892)         在org.datanucleus.store.rdbms.sql.SQLStatement.leftOuterJoin(SQLStatement.java:848)         在org.datanucleus.store.rdbms.sql.SQLStatementHelper.addJoinForOneToOneRelation(SQLStatementHelper.java:995)         at org.datanucleus.store.rdbms.sql.SQLStatementHelper.selectMemberOfSourceInStatement(SQLStatementHelper.java:839)         在org.datanucleus.store.rdbms.request.FetchRequest.processMembersOfClass(FetchRequest.java:488)         在org.datanucleus.store.rdbms.request.FetchRequest。(FetchRequest.java:166)         在org.datanucleus.store.rdbms.RDBMSPersistenceHandler.getFetchRequest(RDBMSPersistenceHandler.java:339)         在org.datanucleus.store.rdbms.RDBMSPersistenceHandler.fetchObject(RDBMSPersistenceHandler.java:316)         at org.datanucleus.state.JDOStateManager.loadFieldsFromDatastore(JDOStateManager.java:1634)         at org.datanucleus.state.JDOStateManager.loadSpecifiedFields(JDOStateManager.java:1236)         在org.datanucleus.state.JDOStateManager.isLoaded(JDOStateManager.java:1724)         ......还有25个

1 个答案:

答案 0 :(得分:0)

好的,我现在明白了...... JPA规范不允许在目标表中使用外键的单向OneToOne。它需要在两个方向上定义OneToOne。 所以我在serviceRequest中定义了onetoone以使用myppedBy ... 我将SRAssignment修改为:

@OneToOne(fetch=FetchType.LAZY )
@JoinColumn(name="SRNO", nullable=false)
private ServiceRequest serviceRequest;

和ServiceRequest as:

@OneToOne (fetch=FetchType.LAZY, optional=true,  mappedBy="serviceRequest")
private SRAssignment assignment;

由于