From 6c927cc6912c77aa1e41a4bdd2f0eacd1d8046d9 Mon Sep 17 00:00:00 2001 From: Vydruth Pulluri Date: Fri, 24 Aug 2018 11:33:47 -0500 Subject: [PATCH] Hibernate is both logging and throwing the error: HHH000346:Error during managed flush --- .../org/hibernate/bugs/JPAUnitTestCase.java | 29 ++++++++++++-- .../bugs/constraint/violation/Event.java | 39 +++++++++++++++++++ 2 files changed, 64 insertions(+), 4 deletions(-) create mode 100644 orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/constraint/violation/Event.java diff --git a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java index f36c69e3..c91f5860 100644 --- a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java +++ b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/JPAUnitTestCase.java @@ -3,10 +3,14 @@ import javax.persistence.EntityManager; import javax.persistence.EntityManagerFactory; import javax.persistence.Persistence; +import javax.persistence.RollbackException; +import org.hibernate.bugs.constraint.violation.Event; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; /** * This template demonstrates how to develop a test case for Hibernate ORM, using the Java Persistence API. @@ -25,14 +29,31 @@ public void destroy() { entityManagerFactory.close(); } - // Entities are auto-discovered, so just add them anywhere on class-path - // Add your tests, using standard JUnit. + @Rule + public ExpectedException exception = ExpectedException.none(); + @Test - public void hhh123Test() throws Exception { + public void hhh12923test() throws Exception { + // Expecting a rollback exception here because Hibernate only realizes that we are violating a constraint while committing + // i.e. after inserting 2 identical rows. So it throws a RollbackException which is caused by ConstraintViolationException. + exception.expect(RollbackException.class); + EntityManager entityManager = entityManagerFactory.createEntityManager(); entityManager.getTransaction().begin(); - // Do stuff... + + Event event1 = new Event("Concert", "Overland Park"); + Event event2 = new Event("Concert", "Overland Park"); + + entityManager.persist(event1); + System.out.println("Event 1 persisted."); + entityManager.persist(event2); + System.out.println("Event 2 persisted."); + entityManager.getTransaction().commit(); + + // The below message is never printed because the commit above is not successful. + System.out.println("Committed"); + entityManager.close(); } } diff --git a/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/constraint/violation/Event.java b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/constraint/violation/Event.java new file mode 100644 index 00000000..7a2c3f92 --- /dev/null +++ b/orm/hibernate-orm-5/src/test/java/org/hibernate/bugs/constraint/violation/Event.java @@ -0,0 +1,39 @@ +package org.hibernate.bugs.constraint.violation; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.persistence.UniqueConstraint; + +/** + * + * @author Vydruth Pulluri + * @author Shelley J. Baker + * + */ +@Table(name = "events_table", uniqueConstraints = { @UniqueConstraint(name = "uq1_event", columnNames = { "location", "display_name" }) }) +@Entity +public class Event { + + /** Unique auto genetared id */ + @Id + @GeneratedValue + private Long id; + + /** Display name of the event */ + @Column(name = "display_name", nullable = false) + private String displayName; + + /** Location where the event is happening */ + @Column(name = "location", nullable = false) + private String location; + + public Event() {} + + public Event(String displayName, String location) { + this.displayName = displayName; + this.location = location; + } +}