I have two classes PLibObject and PLibEntry. A PLibObject can have several PLibEntries. A PLibEntry can have only one PLibObject.

A ConstraintViolationException is thrown when storing a PLibObject with a PLibEntry (see exception at the bottom).

```
@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;
}

```

Database tables (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)

```

Code that produces the exception (just for testing purposes, the real implementation is much more complex):

```
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();

```