Hibernate一对一映射注释插入异常(插入到跨国表时,主表数据不应更改)

时间:2013-09-01 17:09:14

标签: java hibernate

Oracle Table DDL:

CREATE TABLE M_SERVICE
(
   SERVICE_ID    NUMBER (10) PRIMARY KEY,
   SERVICE_NM    VARCHAR2 (255),
   ACTIVE_SW     CHAR (1),
   LST_UPDT_DT   DATE,
   LST_UPDT_BY   VARCHAR2 (32)
)

CREATE TABLE T_JOB
(
   JOB_ID          NUMBER PRIMARY KEY,
   JOB_NM          VARCHAR2 (32),
   JOB_DESC        VARCHAR2 (2000),
   SERVICE_ID      NUMBER,
   DUE_DT          DATE,
   LST_UPDT_DT     DATE,
   LST_UPDT_BY     VARCHAR2 (32),
   CONSTRAINT T_JOB_FK1 FOREIGN KEY
      (SERVICE_ID)
       REFERENCES M_SERVICE (SERVICE_ID)
)

M_SERVICE是主表。 T_JOB是一个Transaction表。 我的要求是,当我尝试在T_job表中插入时,不应在M_service表中插入/更新(所有的service_Id都在M_SERVICE中可用)。但在选择时,我需要两个表数据。

实体:

@Entity
@Table(name = "M_SERVICE")
public class ServiceVO implements Serializable {
    private static final long serialVersionUID = -2684205897352720653L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_SERVICE_ID")
    @SequenceGenerator(name = "SEQ_SERVICE_ID", sequenceName = "SEQ_SERVICE_ID")
    @Column(name = "SERVICE_ID")
    private Integer serviceId;

    @Column(name = "SERVICE_NM")
    private String serviceName;

    @Column(name = "ACTIVE_SW")
    private char activeSwitch;  

    @Column(name = "LST_UPDT_DT")
    @Temporal(TemporalType.DATE)
    private Date lastUpdatedDt;

    @Column(name = "LST_UPDT_BY")
    private String lastUpdatedBy;
 //Getter & Setters.
}

@Entity
@Table(name = "T_JOB")
public class JobVO implements Serializable {
    private static final long serialVersionUID = 7167763557817486917L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_JOB_ID")
    @SequenceGenerator(name = "SEQ_JOB_ID", sequenceName = "SEQ_JOB_ID")
    @Column(name = "JOB_ID")
    private Integer jobId;

    @Column(name = "JOB_NM", nullable = false)
    private String jobName;

    @Column(name = "JOB_DESC")
    private String jobDesc;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "SERVICE_ID", referencedColumnName = "SERVICE_ID", insertable = false, updatable = false)
    private ServiceVO service;

    @Column(name = "DUE_DT", nullable = false)
    @Temporal(TemporalType.DATE)
    private Date dueDate;

    @Column(name = "LST_UPDT_DT", nullable = false)
    @Temporal(TemporalType.DATE)
    private Date lastUpdatedDt;

    @Column(name = "LST_UPDT_BY", nullable = false)
    private String lastUpdatedBy;
//Getter & Setters.
}

如果我打电话来获取基于工作ID意味着的特定工作细节,我可以按照我的预期获得,如下所示

this.sessionFactory.getCurrentSession().get(JobVO.class, id);

JobVO [jobId = 1,jobName = job name,jobDesc = desc,service = ServiceVO [serviceId = 2,serviceName = Tax Audit,activeSwitch = Y,lastUpdatedDt = 2013-08-31,lastUpdatedBy = MAHESH],dueDate = 2013-09-01,lastUpdatedDt = 2013-09-01,lastUpdatedBy = mahesh]

但如果我试图插入一份工作,

            JobVO jobVO = new JobVO();
        jobVO.setAuditorId(1);                  
        jobVO.setDueDate(new Date());
        jobVO.setJobDesc("VAT1");
        jobVO.setJobName("Account");
        jobVO.setLastUpdatedBy("Mahesh");
        jobVO.setLastUpdatedDt(new Date());
        ServiceVO service=new ServiceVO();
        service.setServiceId(getClientId());
        jobVO.setService(service);
            this.sessionFactory.getCurrentSession().persist(jobVO);

如下所示获取错误, org.springframework.orm.hibernate4.HibernateOptimisticLockingFailureException:批量更新从update [0]返回意外的行数;实际行数:0;预期:1;嵌套异常是org.hibernate.StaleStateException:批量更新从update [0]返回意外的行数;实际行数:0;预期:1     在org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:181)     在org.springframework.orm.hibernate4.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:680)

请帮我解决这个问题。

3 个答案:

答案 0 :(得分:0)

发生错误是因为有2个事务处理同一记录。如果记录由2个事务读取,并且如果记录首先由一个事务保存,则在第二个事务中抛出一个乐观锁定异常,因为系统假定没有其他人将修改记录。

您是否在批处理模式下使用多线程?

有不同的解决方案:

  1. 确保要触及要更新的记录 在任何特定时刻只有一笔交易
  2. 重试事务:确保重试之间不保留非事务状态。

答案 1 :(得分:0)

你有方向性的关系。您必须添加ServiceVO课程:

@OneToOne(fetch = FetchType.LAZY, mappedBy = "service")
private JobVO job;

此代码创建双向关系,我认为它可以解决您的问题。

答案 2 :(得分:0)

请试试这个。

    ServiceVO service=new ServiceVO();
    service.setServiceId(getClientId());
    this.sessionFactory.getCurrentSession().saveOrUpdate(service);

    JobVO jobVO = new JobVO();
    jobVO.setAuditorId(1);      
    jobVO.setClientId(getClientId());       
    jobVO.setDueDate(new Date());
    jobVO.setJobDesc("VAT1");
    jobVO.setJobName("Account");
    jobVO.setLastUpdatedBy("Mahesh");
    jobVO.setLastUpdatedDt(new Date());
    jobVO.setService(service);
    this.sessionFactory.getCurrentSession().saveOrUpdate(jobVO);