Tuesday, 7 February 2017

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement

The reason of throwing this exception is you are trying to save an object which has an invalid foreign key column value, eg: user_profile table contains user_id column which is a foreign key from user table, so if you are adding a record in to user_profile table with an invalid user_id value then this exception may be thrown. So, the solution is use a valid foreign key column value.

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationException: could not execute statement

at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:147)

at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155)

at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162)

at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1434)

at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:484)

at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3190)

at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2404)

at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:467)

at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:146)

at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)

at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:220)

at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)