I am getting an exception when trying to persist a one-to-many relationship. I can see that the problem occurs because the parent's (policy) auto generated id is not being used when inserting the child (link).
SQL commands:
CREATE TABLE POLICIES
(
ID INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
CURRENTLINKID INTEGER,
);
CREATE TABLE LINKS
(
ID INTEGER NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1),
ORDINAL INTEGER NOT NULL,
LINK VARCHAR(50) NOT NULL,
POLICYID INTEGER NOT NULL
);
ALTER TABLE POLICIES
ADD FOREIGN KEY(CURRENTLINKID)
REFERENCES LINKS(ID);
ALTER TABLE LINKS
ADD FOREIGN KEY(POLICYID)
REFERENCES POLICIES(ID);
Entities:
@Entity
@Table(name = "POLICIES", catalog = "", schema = "")
public class PolicyEntity
{
...
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "_policy")
private Collection<LinkEntity> _linkEntityCollection;
...
}
@Entity
@Table(name = "LINKS", catalog = "", schema = "")
public class LinkEntity
{
...
@JoinColumn(name = "POLICYID", referencedColumnName = "ID")
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL, optional = false)
private PolicyEntity _policy;
...
}
Create entities and persist:
LinkEntity link = new LinkEntity();
link.setLink("link");
link.setOrdinal(1);
List<LinkEntity> links = new ArrayList<LinkEntity>();
links.add(link);
PolicyEntity policy = new PolicyEntity();
policy.setLinks(links);
EntityManager entityManager = _emf.createEntityManager();
// Begin transaction
entityManager.getTransaction().begin();
// persist
entityManager.persist(policy);
// Commit the transaction
entityManager.getTransaction().commit();
// Close this EntityManager
entityManager.close();
Exception:
javax.persistence.RollbackException: Exception [TOPLINK-4002] (Oracle TopLink Essentials - 2006.8 (Build 060830)): oracle.toplink.essentials.exceptions.DatabaseException Internal Exception: java.sql.SQLIntegrityConstraintViolationException: Column 'POLICYID' cannot accept a NULL value.Error Code: 20000 Call:INSERT INTO LINKS (LINK, ORDINAL, POLICYID) VALUES (?, ?, ?) bind => [link, 1, null] Query:InsertObjectQuery(xxx.xxxx.xxx.xxx.datalayer.entities.LinkEntity@1bb813b)
Call:INSERT INTO LINKS (LINK, ORDINAL, POLICYID) VALUES (?, ?, ?) bind => [link, 1, null] <--- how to set the auto generated policy id here?
UPDATED per Chris's comment.
I realized I needed to actually set the policy in the link entity. Now the links are inserted without exception.
PolicyEntity policy = new PolicyEntity();
LinkEntity link = new LinkEntity();
link.setLink("link");
link.setOrdinal(1);
link.setPolicy(policy) // set policy so it wasn't null
List<LinkEntity> links = new ArrayList<LinkEntity>();
links.add(link);
policy.setLinks(links);