# Cannot insert null into table.column ORA-01400

**URL:** <https://discourse.hibernate.org/t/cannot-insert-null-into-table-column-ora-01400/1882>\
**Category:** Hibernate ORM\
**Created:** [December 13, 2018, 5:07pm UTC](https://discourse.hibernate.org/t/cannot-insert-null-into-table-column-ora-01400/1882 "2018-12-13T17:07:02Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![KirshiYin](https://avatars.discourse-cdn.com/v4/letter/k/977dab/32.png) [@KirshiYin](https://discourse.hibernate.org/u/KirshiYin)\
**Post date:** [December 13, 2018, 5:07pm UTC](https://discourse.hibernate.org/t/cannot-insert-null-into-table-column-ora-01400/1882/1 "2018-12-13T17:07:02Z")

</div>

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](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/1X/f0f47cb47987b2a10584f58f4c830e1e49b268a2.jpeg)  
Java classes:

```auto
@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.

```auto
....
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!

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [December 13, 2018, 5:24pm UTC](https://discourse.hibernate.org/t/cannot-insert-null-into-table-column-ora-01400/1882/2 "2018-12-13T17:24:37Z")

</div>

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`).

---

<div class="post-metadata">

**Author:** ![KirshiYin](https://avatars.discourse-cdn.com/v4/letter/k/977dab/32.png) [@KirshiYin](https://discourse.hibernate.org/u/KirshiYin)\
**Post date:** [December 13, 2018, 5:34pm UTC](https://discourse.hibernate.org/t/cannot-insert-null-into-table-column-ora-01400/1882/3 "2018-12-13T17:34:03Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [December 13, 2018, 5:35pm UTC](https://discourse.hibernate.org/t/cannot-insert-null-into-table-column-ora-01400/1882/4 "2018-12-13T17:35:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KirshiYin](https://avatars.discourse-cdn.com/v4/letter/k/977dab/32.png) [@KirshiYin](https://discourse.hibernate.org/u/KirshiYin)\
**Post date:** [December 13, 2018, 6:10pm UTC](https://discourse.hibernate.org/t/cannot-insert-null-into-table-column-ora-01400/1882/5 "2018-12-13T18:10:20Z")

</div>

I hope this screenshot explains it better:

![data](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/1X/8cbcb362da850450cbb35225c41613610066e3fe.jpeg)

DDL for table\_a2b:

```auto
 "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
   ) 

```

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [December 13, 2018, 7:10pm UTC](https://discourse.hibernate.org/t/cannot-insert-null-into-table-column-ora-01400/1882/6 "2018-12-13T19:10:18Z")

</div>

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](https://vladmihalcea.com/how-to-customize-an-entity-association-join-on-clause-with-hibernate-joinformula/) and use a subselect to create the association.

---

<div class="post-metadata">

**Author:** ![KirshiYin](https://avatars.discourse-cdn.com/v4/letter/k/977dab/32.png) [@KirshiYin](https://discourse.hibernate.org/u/KirshiYin)\
**Post date:** [December 14, 2018, 11:43am UTC](https://discourse.hibernate.org/t/cannot-insert-null-into-table-column-ora-01400/1882/7 "2018-12-14T11:43:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [December 15, 2018, 6:44am UTC](https://discourse.hibernate.org/t/cannot-insert-null-into-table-column-ora-01400/1882/8 "2018-12-15T06:44:44Z")

</div>

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.
