gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:25:21 25 4
gpt4 key购买 nike

Oracle 表 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是一个事务表。我的要求是,当我尝试在 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=作业名称,jobDesc=desc,service=ServiceVO [serviceId=2,serviceName=税务审计,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:批量更新从更新[0]返回意外的行计数;实际行数:0;预期:1;嵌套异常是 org.hibernate.StaleStateException:批量更新从更新 [0] 返回意外的行计数;实际行数:0;预计:1 在org.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:181) 在 org.springframework.orm.hibernate4.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:680)

请帮我解决这个问题。

最佳答案

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

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

有不同的解决方案:

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

关于java - Hibernate一对一映射注释插入异常(插入跨国表时,主表数据不应改变),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18560832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com