Hi,
i am trying to save an entity which is joined to another entity through a secondary table. Attached is the db model for this case.
Java classes:
@Entity
@Table(name = "table_a")
@SecondaryTables({
@SecondaryTable(name = "table_a2b", pkJoinColumns = {
@PrimaryKeyJoinColumn(name = "a_objid", referencedColumnName = "objid")})
})
@SequenceGenerator(name = "generator", sequenceName = "generator", allocationSize = 1)
public class A implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
@Column(name = "OBJID")
private Long objid;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "table_a2b", joinColumns = {
@JoinColumn(name = "a_objid") }, inverseJoinColumns = {
@JoinColumn(name = "b_class_id",referencedColumnName = "OBJID" , nullable = false, insertable = false, updatable = false) })
private List<B> classes;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "table_a2b", joinColumns = {
@JoinColumn(name = "a_objid") }, inverseJoinColumns = {
@JoinColumn(name = "b_block_id",referencedColumnName = "OBJID" , nullable = false, insertable = false, updatable = false) })
private List<B> blocks;
// getters and setters and add method for classes and blocks
}
@Entity
@Table(name = "table_b")
@SequenceGenerator(name = "gen", sequenceName = "gen", allocationSize = 1)
public class B implements Serializable{
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "gen")
@Column(name = "OBJID")
private Long objid;
// getters setters
}
There is an implementation of helper methods for the dataaccess layer.
....
a.addBlocks(b);
a.addClasses(b);
helper.insert(a);
Hibernate log:
insert into table_a ( END_DATE, IS_NEW, START_DATE, OBJID) values ( ?, ?, ?, ?)
insert into table_a2b (a_objid, b_block_id) values (?, ?)
could not execute statement [n/a]: java.sql.SQLIntegrityConstraintViolationException: ORA-01400: Cannot insert NULL into : (“table_a2b”.“b_class_id”)
I know that no values can be null in table_a2b. But i am trying to insert b_block_id and b_class_id too. So why is hibernate trying to insert them separately? How can I save a record of my entity into table_a and insert a corresponding record into table_a2b?
Thanks a lot in advance!