我有两个类 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;
例外:
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();