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 |
yunjie zh 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 |
yunjie zh 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))
|
|
| | | |
Goto Forum:
Current Time: Tue Sep 24 03:47:25 GMT 2024
Powered by FUDForum. Page generated in 0.04759 seconds
|