Hi,
I have a one to many relationship between two classes PARENT and CHILD. The relationship is between the parent primary key (autogenerated oracle sequence when persisted) to one of the member of composite primary key of the child
Parent Child
------- -------
parentId ------------>|parentId | ----> Composite primary key of child (parentId + child_dept_key)
Name -|child_dept_key|
-dept-name
And my entity mappings are defined as below.
Here while persisting, the parentId on the parent when created, should be automatically mapped to the child entity and a record in the child should be created.
Code:
@Entity
@Table(name="PARENT")
@SequenceGenerator(name = "parent-seq", sequenceName = "PARENT_SEQUENCE")
class Parent{
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "parent-seq")
@Column(name="PARENT_ID")
Long id;
@Column(name="PARENT_NAME")
String name;
@OneToMany(mappedBy = "childCompositePK.parent", cascade=CascadeType.ALL, fetch=FetchType.EAGER)
@JoinColumn(name="PARENT_ID", nullable= false, updatable=false)
private List<Child> childList;
}
@Entity
@Table(name="CHILD")
Class Child{
@EmbeddedId
ChildPK childCompositePK;
@Column(name="DEPARTMENT_NAME")
String depantmentName;
}
@Embeddable
Class ChildPK{
@ManyToOne
@JoinColumn(name = "PARENT_ID",referencedColumnName="PARENT_ID", nullable= false, updatable=false)
Parent parent;
@Column(name="DEPARTMENT_KEY")
String departmentKey;
}
When I try to persist the parent entity, the parent record is saving properly.
But after that while persisting the child record, the 'parent-id' value is not being passed to the child and I am getting a SQL exception for NULL value.
I have got struck at this point. Can anyone help me in this issue please.
Code:
Caused by: javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:513)
at com.arjuna.ats.internal.jta.resources.arjunacore.SynchronizationImple.beforeCompletion(SynchronizationImple.java:101)
at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.beforeCompletion(TwoPhaseCoordinator.java:263)
at com.arjuna.ats.arjuna.coordinator.TwoPhaseCoordinator.end(TwoPhaseCoordinator.java:86)
at com.arjuna.ats.arjuna.AtomicAction.commit(AtomicAction.java:177)
at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.commitAndDisassociate(TransactionImple.java:1414)
... 28 more
Caused by: org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
at org.hibernate.ejb.AbstractEntityManagerImpl$1.beforeCompletion(AbstractEntityManagerImpl.java:504)
... 33 more
Caused by: java.sql.BatchUpdateException: ORA-01400: cannot insert NULL into (???)
at oracle.jdbc.driver.DatabaseError.throwBatchUpdateException(DatabaseError.java:343)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:10768)
at org.apache.commons.dbcp.DelegatingStatement.executeBatch(DelegatingStatement.java:297)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:70)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:268)
... 40 more
Thanks in advance.
Regards,
Harish