在我的项目中,我有Events实体和MyLocation实体。
MyLocation id是event.eventStartTime (ISOString) + event.eventName,当我尝试更新事件名称(不再与MyLocation字符串名称相关)时遇到问题,这是错误:
Violation of PRIMARY KEY constraint 'PK__my_locat__72E12F1A88A314B4'. Cannot insert duplicate key in object 'dbo.my_location'. The duplicate key value is (2020-06-25T14:00:01.235ZWeddingT&Z).代码如下:
@Entity
@Table(name = "Events")
public class Event {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long eventID;
@Column(nullable = false)
private String eventName;
@OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private MyLocation eventLocation;
@column
private TimeStamp eventStartTime;
....@Entity
public class MyLocation {
@Id
private String name;
@Column
private double longitude;
@Column
private double latitude;
.....该问题是否与MyLocation中的@Id有关?或任何其他解决方案?
发布于 2020-07-01 20:30:38
您似乎正在尝试创建重复的MyLocation条目。如何生成位置名称,或者对唯一的位置名称是否有足够的检查逻辑?
此外,您还可以使用其他类型作为事件表的主键。比如uuid。
@Id
@GeneratedValue(generator = “UUID”)
@GenericGenerator(
name = “UUID”,
strategy = “org.hibernate.id.UUIDGenerator”,
)
@Column(name = “id”, updatable = false, nullable = false)
private UUID id;或者你可以有一个自动递增的id:
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;https://stackoverflow.com/questions/62676518
复制相似问题