gpt4 book ai didi

java - 存储对象时出现 ConstraintViolationException 1 :n relationship

转载 作者:行者123 更新时间:2023-12-02 00:22:48 24 4
gpt4 key购买 nike

我有两个类 PLibObject 和 PLibEntry。一个 PLibObject 可以有多个 PLibEntries。一个 PLibEntry 只能有一个 PLibObject。

使用 PLibEntry 存储 PLibObject 时会引发 ConstraintViolationException(请参阅底部的异常)。

@Entity
@Table(name = "pla_objects")
public class PLibObject {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "plibid", unique = true, nullable = false)
private String plibId;
@Column(name = "oid", nullable = false)
private String oid;
@OneToMany(mappedBy = "pLibObject", orphanRemoval = true, cascade = CascadeType.ALL)
private Set<PLibEntry> pLibEntries = new HashSet<PLibEntry>();
...


@Entity
@Table(name = "pla_entries")
public class PLibEntry {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "entryid", unique = true, nullable = false)
private String entryId;
@Column(name = "userid", nullable = false)
private int userId;
@ManyToOne(optional = false, cascade = CascadeType.ALL)
@JoinColumn(name = "plibid")
private PLibObject pLibObject;
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "creationdate", nullable = false)
private Date creationDate;
@Column(name = "sharedfrom", nullable = false)
private String sharedFrom;
@Enumerated(EnumType.STRING)
@Column(name = "sharingstatus", nullable = false)
private SharingStatus sharingStatus;
@ElementCollection
@CollectionTable(name = "pla_tags", joinColumns = @JoinColumn(name = "entryid"))
@Column(name = "tag")
private Set<String> tags;
...

public enum SharingStatus {
UNSHARED,
SHARING_PENDING,
SHARING_ACCEPTED,
SHARING_CANCELLED_BY_SHARER;
}

数据库表(MySQL):

create table pla_objects(
id int(10) not null auto_increment,
plibid varchar(255) not null,
oid varchar(255) not null,
primary key (id),
unique (plibid)
) charset=utf8 engine=innodb;

create table pla_entries(
id int(10) not null auto_increment,
entryid varchar(255) not null,
userid int(10) not null,
plibid varchar(255) not null,
creationdate datetime not null,
sharedfrom varchar(255),
sharingstatus enum('UNSHARED', 'SHARING_PENDING', 'SHARING_ACCEPTED', 'SHARING_CANCELLED_BY_SHARER') not null,
primary key (id),
unique (entryid),
foreign key (plibid) references pla_objects(plibid)
) charset=utf8 engine=innodb;

create table pla_tags(
entryid varchar(255) not null,
tag varchar(255) not null,
primary key (entryid, tag),
foreign key (entryid) references pla_entries(entryid)
) charset=utf8 engine=innodb;

异常(exception):

javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Referentielle Integrität verletzt: "CONSTRAINT_B23: PUBLIC.PLA_ENTRIES FOREIGN KEY(PLIBID) REFERENCES PUBLIC.PLA_OBJECTS(PLIBID)

产生异常的代码(仅用于测试目的,实际实现要复杂得多):

PLibObject pLibObject = new PLibObject("plibid", "oid");
new PLibEntry("entryid", 0, pLibObject, new Date(1), null, SharingStatus.UNSHARED,
Arrays.asList("tagA", "tagB")); // entry is set in object via constructor

Map<String, String> map = new HashMap<String, String>();
map.put("javax.persistence.jdbc.url", "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;LOCK_TIMEOUT=3000");
map.put("javax.persistence.jdbc.user", "sa");
map.put("javax.persistence.jdbc.password", "sa");
map.put(Environment.SHOW_SQL, "true");
map.put(Environment.FORMAT_SQL, "true");

EntityManagerFactory factory = Persistence.createEntityManagerFactory("plib", map);
EntityManager entityManager = factory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(pLibObject);
entityManager.getTransaction().commit();

最佳答案

您如何存储数据?

也许您正在为您的 PLibObject 设置一组 pLibEntries,并且该集合中的每个元素 PLibEntry 都有一个 null PLibObject,并且因为您已标记 CascadeType.ALL 它正在尝试保留一个具有 NULL Pk 的 PLibObject。

关于java - 存储对象时出现 ConstraintViolationException 1 :n relationship,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10625610/

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