From 8f5bc492e8df14a72ae30e4b95fa9506dddce4ba Mon Sep 17 00:00:00 2001 From: Vlad Mihalcea Date: Mon, 9 Jan 2017 16:07:52 +0200 Subject: [PATCH] HHH-11236 - JPA hbm2ddl auto-generation creates ddl with invalid syntax for Unique Key with any MySQLDialect Add replicating test case that works just fine --- ...hrowsConstraintViolationExceptionTest.java | 82 +++++++++++++++++++ libraries.gradle | 2 +- 2 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/uniqueconstraint/UniqueConstraintThrowsConstraintViolationExceptionTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/uniqueconstraint/UniqueConstraintThrowsConstraintViolationExceptionTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/uniqueconstraint/UniqueConstraintThrowsConstraintViolationExceptionTest.java new file mode 100644 index 0000000000..2d4dd3bd33 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/uniqueconstraint/UniqueConstraintThrowsConstraintViolationExceptionTest.java @@ -0,0 +1,82 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.annotations.uniqueconstraint; + +import javax.persistence.Basic; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.PersistenceException; +import javax.persistence.Table; + +import org.hibernate.exception.ConstraintViolationException; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +/** + * @author Vlad Mihalcea + */ +@TestForIssue(jiraKey = "HHH-11236") +public class UniqueConstraintThrowsConstraintViolationExceptionTest extends BaseCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Customer.class }; + } + + @Test + public void testUniqueConstraintWithEmptyColumnName() { + doInHibernate( this::sessionFactory, session -> { + Customer customer1 = new Customer(); + customer1.customerId = "123"; + session.persist( customer1 ); + } ); + try { + doInHibernate( this::sessionFactory, session -> { + Customer customer1 = new Customer(); + customer1.customerId = "123"; + session.persist( customer1 ); + } ); + fail( "Should throw" ); + } + catch ( PersistenceException e ) { + assertEquals( + ConstraintViolationException.class, + e.getCause().getClass() + ); + } + } + + @Entity + @Table(name = "CUSTOMER") + public static class Customer { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + @Column(name = "CUSTOMER_ACCOUNT_NUMBER") + public Long customerAccountNumber; + + @Basic + @Column(name = "CUSTOMER_ID", unique = true) + public String customerId; + + @Basic + @Column(name = "BILLING_ADDRESS") + public String billingAddress; + + public Customer() { + } + } +} diff --git a/libraries.gradle b/libraries.gradle index 5a15681eb2..65c62cf4a4 100644 --- a/libraries.gradle +++ b/libraries.gradle @@ -95,7 +95,7 @@ ext { hsqldb: "org.hsqldb:hsqldb:2.3.2", derby: "org.apache.derby:derby:10.11.1.1", postgresql: 'org.postgresql:postgresql:9.4-1202-jdbc41', - mysql: 'mysql:mysql-connector-java:5.1.38', + mysql: 'mysql:mysql-connector-java:6.0.5', mariadb: 'org.mariadb.jdbc:mariadb-java-client:1.1.7', oracle: 'com.oracle.ojdbc:ojdbc7:12.1.0.2.0', mssql: 'com.microsoft.sqlserver:mssql-jdbc:6.1.0.jre8',