Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » OneToMany - org.eclipse.persistence.exceptions.DatabaseException(null value in column "parent_id" violates not-null constraint)
OneToMany - org.eclipse.persistence.exceptions.DatabaseException [message #1838593] Tue, 02 March 2021 07:22 Go to next message
Ganesh S is currently offline Ganesh SFriend
Messages: 1
Registered: March 2021
Junior Member
Hi All,

I have two database entities in OneToMany relationship.

While inserting records in the child table (entity with many relationship) I get Internal Exception: org.postgresql.util.PSQLException: ERROR: null value in column "parent_id" violates not-null constraint.

This does not happen all the times. What could be going wrong here?

Exception is:
SEVERE: Error while trying to create a child.
javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.7.2.v20180622-f627448): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: org.postgresql.util.PSQLException: ERROR: null value in column "parent_id" violates not-null constraint


Relevant code is:

public void add(Tenant tenant, Parent parent) {
    runInTransaction(tenant, entityManager -> {
        final List<Child> children = parent.getChildren();
        parent.setChildren(Collections.emptyList());
        entityManager.persist(parent);
        children.forEach(child -> {
            child.setParent(parent);
            entityManager.persist(child);
        });
    });
}


where runInTransaction is as following:
protected void runInTransaction(Tenant tenant, Consumer<EntityManager> action) {
    final EntityManager manager = getEntityManager(tenant);
    manager.getTransaction().begin();
    manager.createNativeQuery(String.format("SET LOCAL search_path TO %s, public;", tenant.getSchemaName())).executeUpdate();
    action.accept(manager);
    manager.getTransaction().commit();
}
Re: OneToMany - org.eclipse.persistence.exceptions.DatabaseException [message #1840912 is a reply to message #1838593] Wed, 28 April 2021 16:49 Go to previous message
Chris Delahunt is currently offline Chris DelahuntFriend
Messages: 1389
Registered: July 2009
Senior Member
This is old, but thought I might comment:

The log message you showed "SEVERE: Error while trying to create a child." doesn't seem to fit with the code you've shown, and couldn't come from EclipseLink - it doesn't consider an insert as a create operation (JPA calls it persist, and it uses insert in native code). Seems likely to come from your code in something more specific to Child creation, where maybe it is created with the parent references set to null or maybe even the parent id somehow being null (though I expect something different in that case).

Assuming it is this code, you would need to turn on SQL logging (https://wiki.eclipse.org/EclipseLink/Examples/JPA/Logging ) to see the statements it issues within this transaction, to determine if other child inserts are working, if the parent was inserted, or something else is going wrong.

It seems strange that you replace the parent's children collection with an empty one - that shouldn't be necessary, and should really be avoided, as it means this parent entity instance won't reflect what you have in the database when it is done. You might want to call parent.setChildren(children) after the for loop to fix it if you can't just leave the collection there when you call persist on the parent.

Best Regards,
Chris
Previous Topic:ConcurrentModification Exception in JPA 3.0
Next Topic:ConcurrentModification Exception in JPA 3.0 Followup
Goto Forum:
  


Current Time: Sat Nov 09 00:47:48 GMT 2024

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

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

Back to the top