我有一个停机时间实体。我正在为停机对象设置值,并将这些对象传递给 JpaReposirtory save(downtimeobject)
方法。 save()
插入重复记录。我参加了论坛,但是没有运气,我没有任何解决方案。现在,在将对象保存到数据库中之后,我正在检查是否插入重复项。如果插入,我将通过 JpaRepository delete(downtimeId)
删除。
我的downitme实体
@Entity
@Table(name = "tDownTime")
public class DownTime {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "DowntimeId")
private int downtimeId;
@Column(name = "SiteId")
private int siteId;
@Column(name = "PlantId")
private int plantId;
@Column(name = "UnitId")
private int unitId;
@Column(name = "EquipmentId")
private int equipmentId;
@Column(name = "ShiftId")
private int shiftId;
@Column(name = "DownTimeReasonId")
private Integer downTimeReasonId;
@Column(name = "DownTimeReasonName")
private String downTimeReasonName;
@Column(name = "DownTimeReasonDescription")
private String downTimeReasonDescription;
@Column(name = "DownTimeStartDate")
private LocalDate downTimeStartDate;
@Column(name = "DownTimeStartTime")
private LocalTime downTimeStartTime;
@Column(name = "DownTimeEndDate")
private LocalDate downTimeEndDate;
@Column(name = "DownTimeEndTime")
private LocalTime downTimeEndTime;
@Column(name = "DownTimeDuration")
private Long downTimeDuration;
@Column(name = "HourResetTime")
private LocalDateTime hourResetTime;
@Column(name = "KPIDate")
private LocalDate kPIDate;
@Column(name = "CreatedBy")
private String createdBy;
@Column(name = "CreatedOn")
private LocalDateTime createdOn;
@Column(name = "ModifiedBy")
private String modifiedBy;
@Column(name = "ModifiedOn")
private LocalDateTime modifiedOn;
@Column(name = "SKUId")
private int sKUID;
@Column(name = "DownTimeStartDateTime")
private LocalDateTime downTimeStartDateTime;
@Column(name = "DownTimeEndDateTime")
private LocalDateTime downTimeEndDateTime;
@Column(name = "WorkOrderId")
private int workOrderId;
@Column(name = "IsManual")
private boolean isManual;
@Column(name = "AutoPauseResume")
private boolean isAutoPauseResume;
@Column(name = "DowntimeType")
private String downtimeType;
@Column(name = "DownTimeStatus")
private String downtimeStatus;
DownTime downTime = new DownTime();// creating object for setting values to object to save as entity
downTime.setUnitId(calculationOEEHelper.getUnitId()); downTime.setEquipmentId(calculationOEEHelper.getEquipmentId());
downTime.setShiftId(calculationOEEHelper.getShiftId());
downTime.setDownTimeReasonId(0);
downTime.setDownTimeReasonName(ParameterTags.UNASSIGNED);
.
.
.
downTime = downtimeRepository.save(downTime);
它正在数据库中插入2个整数。我正在使用MSSQL DB
DownTime [downtimeId = 2148,siteId = 1,plantId = 1,unitId = 12, equipmentId = 33,shiftId = 28,downTimeReasonId = 0, downTimeReasonName =未分配,downTimeReasonDescription =未分配, downTimeStartDate = 2019-05-11,downTimeStartTime = 06:36:42, downTimeEndDate = 2019-05-11,downTimeEndTime = 06:37:23, downTimeDuration = 41,hourResetTime = 2019-05-11T07:30:01, kPIDate = 2019-05-11,createdBy = SYSTEM, createdOn = 2019-05-12T01:06:55.865,ModifyBy = SYSTEM, modifiedOn = 2019-05-12T01:06:55.865,sKUID = 5, downTimeStartDateTime = 2019-05-11T06:36:42, downTimeEndDateTime = 2019-05-11T06:37:23,workOrderId = 74, isManual = false,isAutoPauseResume = true,downtimeType =未安排, downtimeStatus =正在运行]
DownTime [downtimeId = 2149,siteId = 1,plantId = 1,unitId = 12, equipmentId = 33,shiftId = 28,downTimeReasonId = 0, downTimeReasonName =未分配,downTimeReasonDescription =未分配, downTimeStartDate = 2019-05-11,downTimeStartTime = 06:36:42, downTimeEndDate = 2019-05-11,downTimeEndTime = 06:37:23, downTimeDuration = 41,hourResetTime = 2019-05-11T07:30:01, kPIDate = 2019-05-11,createdBy = SYSTEM, createdOn = 2019-05-12T01:06:55.865,ModifyBy = SYSTEM, modifiedOn = 2019-05-12T01:06:55.865,sKUID = 5, downTimeStartDateTime = 2019-05-11T06:36:42, downTimeEndDateTime = 2019-05-11T06:37:23,workOrderId = 74, isManual = false,isAutoPauseResume = true,downtimeType =未安排, downtimeStatus =正在运行]
downtimeId不同,其余全部相同
可以帮助我解决这个问题。
答案 0 :(得分:0)
我不确定我是否理解您要实现的目标,但这似乎是预期的行为。如果要更新对象而不是创建对象,则应从存储库中获取旧版本并在其上设置值。