Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HHH-12923 Hibernate is both logging and throwing the error: HHH000346:Error during managed flush #23

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}