Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There is no need to write any custom code to achieve this - Hibernate will log the exception cause by default. If you can't see this, Hibernate logging must not be set up correctly. Here's an example with slf4j+log4j, and using Maven for dependency management.</p> <p><strong>src/main/java/pgextest/PGExceptionTest.java</strong></p> <pre><code>public class PGExceptionTest { public static void main(String[] args) throws Exception { EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory( "pgextest"); EntityManager entityManager = entityManagerFactory.createEntityManager(); entityManager.getTransaction().begin(); // here I attempt to persist an object with an ID that is already in use entityManager.persist(new PGExceptionTestBean(1)); entityManager.getTransaction().commit(); entityManager.close(); } } </code></pre> <p><strong>src/main/resources/log4j.properties</strong></p> <pre><code>log4j.rootLogger=ERROR, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n </code></pre> <p><strong>src/main/resources/META-INF/persistence.xml</strong></p> <pre><code>&lt;persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0"&gt; &lt;persistence-unit name="pgextest"&gt; &lt;properties&gt; &lt;property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/&gt; &lt;property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost/pgextest"/&gt; &lt;property name="javax.persistence.jdbc.user" value="postgres"/&gt; &lt;property name="javax.persistence.jdbc.password" value="postgres"/&gt; &lt;property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/&gt; &lt;property name="hibernate.jdbc.batch_size" value="5"/&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; &lt;/persistence&gt; </code></pre> <p><strong>pom.xml</strong></p> <pre><code>&lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"&gt; &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt; &lt;groupId&gt;pgextest&lt;/groupId&gt; &lt;artifactId&gt;pgextest&lt;/artifactId&gt; &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt; &lt;build&gt; &lt;plugins&gt; &lt;plugin&gt; &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt; &lt;configuration&gt; &lt;source&gt;1.6&lt;/source&gt; &lt;target&gt;1.6&lt;/target&gt; &lt;/configuration&gt; &lt;/plugin&gt; &lt;/plugins&gt; &lt;/build&gt; &lt;dependencies&gt; &lt;dependency&gt; &lt;groupId&gt;org.hibernate&lt;/groupId&gt; &lt;artifactId&gt;hibernate-entitymanager&lt;/artifactId&gt; &lt;version&gt;3.6.9.Final&lt;/version&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;postgresql&lt;/groupId&gt; &lt;artifactId&gt;postgresql&lt;/artifactId&gt; &lt;version&gt;9.1-901.jdbc4&lt;/version&gt; &lt;scope&gt;runtime&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;org.slf4j&lt;/groupId&gt; &lt;artifactId&gt;slf4j-log4j12&lt;/artifactId&gt; &lt;version&gt;1.6.1&lt;/version&gt; &lt;scope&gt;runtime&lt;/scope&gt; &lt;/dependency&gt; &lt;dependency&gt; &lt;groupId&gt;log4j&lt;/groupId&gt; &lt;artifactId&gt;log4j&lt;/artifactId&gt; &lt;version&gt;1.2.15&lt;/version&gt; &lt;scope&gt;runtime&lt;/scope&gt; &lt;/dependency&gt; &lt;/dependencies&gt; &lt;/project&gt; </code></pre> <p>Executing the main method will then log the following:</p> <pre><code>ERROR [main] - Batch entry 0 insert into PGExceptionTestBean (label, id) values (NULL, '1') was aborted. Call getNextException to see the cause. ERROR [main] - ERROR: duplicate key value violates unique constraint "pgexceptiontestbean_pkey" </code></pre> <p>It's probably worth mentioning that you can disable the JDBC batching that wraps the original exception by setting the property <code>hibernate.jdbc.batch_size</code> to <code>0</code> (needless to say you probably don't want to do this in production.)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload