Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » JPA ManyToMany JoinTable - integrity constraint violation: NOT NULL check constraint;
JPA ManyToMany JoinTable - integrity constraint violation: NOT NULL check constraint; [message #1724917] Sat, 27 February 2016 06:38 Go to next message
yunjie zh is currently offline yunjie zhFriend
Messages: 7
Registered: October 2015
Junior Member
We have two Entity(s), they are many-to-many relationship. One Item can have many owners and many participants. See the below domain object definition.

public class Item {
  String itemData;

  @ManyToMany
  private List<User>     owners                  = new LinkedList<>();

  @ManyToMany
  private List<User>     participants                  = new LinkedList<>();
}

public class User {
  String userId;
  String userName;
}


The JPA provider will create Item table, User table and a Join table Item_User(Item_id, owners_id, participants_id).
When we add a new Item, usually we add a user as owner or participant, would not set the two fields at same time.

So we'll have insert Item_User(owners_id, Item_id) values(xx, xx) or insert Item_User(participants_id, Item_id) values(xx, xx).

We got:
Error Code: -10
Call: INSERT INTO Item_User (owners_id, Item_id) VALUES (?, ?)
bind => [2 parameters bound]
Query: DataModifyQuery(name="owners" sql="INSERT INTO Item_User (owners_id, Item_id) VALUES (?, ?)")
at ...
Caused by: org.eclipse.persistence.exceptions.DatabaseException:
Internal Exception: java.sql.SQLIntegrityConstraintViolationException: integrity constraint violation: NOT NULL check constraint; SYS_CT_10178 table: Item_User column: participants_id

We would expect to see the data in Item_user table like:
Item_id owners_id participants_id
1 111 NULL
1 222 NULL
2 NULL 222
3 NULL 333

How should I define the domain object to let me have NULL value in join table.

BTW, previously we use OpenJPA as JPA provider, it just generated the table, no foreign key constraints defined, so the application worked properly. Now we migration to eclipselink, I saw the foreign key constraints defined and the exception threw out.
Re: JPA ManyToMany JoinTable - integrity constraint violation: NOT NULL check constraint; [message #1725836 is a reply to message #1724917] Tue, 08 March 2016 04:19 Go to previous messageGo to next message
yunjie zh is currently offline yunjie zhFriend
Messages: 7
Registered: October 2015
Junior Member
From the below application server log, I saw that the EclipseLink will create NOT NULL field for many-to-many foreign key field. Is it part of JPA specification or implementation-specific of EclipseLink? thanks.

[EL Fine]: 2016-03-03 14:54:23.181--ServerSession(915823171)--Connection(-1086626338)--Thread(Thread[main,5,main])--CREATE TABLE ITEM (ID BIGINT NOT NULL, SUMMARY VARCHAR(255), VERSION BIGINT, PRIMARY KEY (ID))
[EL Fine]: 2016-03-03 14:54:23.183--ServerSession(915823171)--Connection(-2005374478)--Thread(Thread[main,5,main])--CREATE TABLE USER (ID BIGINT NOT NULL, LASTMODIFIED TIMESTAMP, USERNAME VARCHAR(255), VERSION BIGINT, PRIMARY KEY (ID))
[EL Fine]: 2016-03-03 14:54:23.184--ServerSession(915823171)--Connection(506474722)--Thread(Thread[main,5,main])--CREATE TABLE ITEM_USER (ITEM_ID BIGINT NOT NULL, owners_ID BIGINT NOT NULL, participants_ID BIGINT NOT NULL, PRIMARY KEY (ITEM_ID, owners_ID, participants_ID))
Re: JPA ManyToMany JoinTable - integrity constraint violation: NOT NULL check constraint; [message #1734007 is a reply to message #1725836] Thu, 02 June 2016 23:04 Go to previous messageGo to next message
Will Dazey is currently offline Will DazeyFriend
Messages: 10
Registered: February 2015
Junior Member
Apologies on the late reply, I just stumbled across this use case in my own testing, tho it looks like bad news. I took a look at the JPA 2.1 spec and I do not see a mention as to whether join tables should be created with nullable columns or not, but it does seem that multiple providers default differently on this behavior. If it was in the spec, I would have assumed it to be around sections 2.10.5.2 or 11.2.1.5.
This would appear to be a bug in EclipseLink since this is a valid use case. The only work around I can think of would be to define a unique @JoinTable for each relationship, then there would not be a problem with inserting NULL.
Re: JPA ManyToMany JoinTable - integrity constraint violation: NOT NULL check constraint; [message #1734374 is a reply to message #1734007] Tue, 07 June 2016 15:57 Go to previous messageGo to next message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
This is a hole in the specification IMO, as the two relationships should get their own join tables by default, but the spec forces them into a table based on their entity names instead of the relationship it represents. Allowing nulls or mixing the collections into one table essentially means there nulls in the Item's owners collection, which is not supported in EclipseLink.

Another workaround is to generate the DDL, change it, and then have EclipseLink execute your modified DDL file that allows nulls in the join table - EclipseLink can create and run DDL through scripts as shown here http://www.eclipse.org/eclipselink/documentation/2.5/jpa/extensions/p_ddl_generation.htm

As EclipseLink does not support nulls in collections, you will need to test how EclipseLink handles the extra rows in the join tables in your applications use cases to make sure they are handled as you would expect in all cases.

[Updated on: Tue, 07 June 2016 15:59]

Report message to a moderator

Re: JPA ManyToMany JoinTable - integrity constraint violation: NOT NULL check constraint; [message #1737928 is a reply to message #1734374] Thu, 14 July 2016 08:43 Go to previous message
yunjie zh is currently offline yunjie zhFriend
Messages: 7
Registered: October 2015
Junior Member
But there are still problems after adding @JoinTable annotation.

Please check my testcase on github:
https://github.com/zyjibmcn/jpa-eclipselink-test

To run the testcase, just issue mvn clean install under the problem.
There still one case ItemObjDaoITest#testByUsers() will fail.
Previous Topic:Criteria OrderBy
Next Topic:soft delete is deleting relations
Goto Forum:
  


Current Time: Tue Sep 24 03:47:25 GMT 2024

Powered by FUDForum. Page generated in 0.04759 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top