Cannot insert null into table.column ORA-01400

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.
dbmodell2
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!

The mapping makes no sense to me.

How could both the b_block_id and b_class_id reference the same OBJID column. Most likely you need a composite identifier where 2 columns in the child table (e.g. table_a2b) reference other 2 columnd in the parent table (e.g. table_b).

table_b contains records for classes and blocks stored in table_a2b
a record in table_a2b looks like this:

a_objid b_class_id b_block_id
5 268 5279

This way I can get the values separately e.g. a.getBlocks(), a.getClasses()

You need to add the actual DDL of the tables and the real data so that I can better understand your mapping.

I hope this screenshot explains it better:

data

DDL for table_a2b:

 "CREATE TABLE "SCHEMA"."table_a2b" 
   (	"a_objid" NUMBER(10,0) NOT NULL ENABLE, 
	"b_class_id" NUMBER(10,0) NOT NULL ENABLE, 
	"b_class_id" NUMBER(10,0) NOT NULL ENABLE, 
	 CONSTRAINT "FK_BLCK" FOREIGN KEY ("b_block_id")
	  REFERENCES "SCHEMA"."table_b" ("OBJID") DISABLE, 
	 CONSTRAINT "FK_A" FOREIGN KEY ("a_objid")
	  REFERENCES "SCHEMA"."table_a" ("OBJID") DISABLE, 
	 CONSTRAINT "FK_CLASS" FOREIGN KEY ("b_class_id")
	  REFERENCES "SCHEMA"."table_b" ("OBJID") DISABLE
   ) 

This design does not follow the database normalization rules, hence it cannot me mapped with standard JPA annotations.

You might want to check the @JoinFormula mapping instead and use a subselect to create the association.

Will this work with OneToMany assosiation in the table_a Entity?
Do I have to remove the JoinTable annotation? I am afraid this way I won`t be able to get the information I need from table_b.

The @JoinFormula can resolve the FK via a query, so you don’t need the @JoinTable in this case.

Just try it and see if it works.