From 2ef777e3fe398ed64bb69eeb48dfaa2d2e121a72 Mon Sep 17 00:00:00 2001 From: Gail Badner Date: Fri, 13 Jul 2018 14:43:53 -0700 Subject: [PATCH] HHH-12802 : test case --- .../hibernate/test/id/NonUniqueIdTest.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/id/NonUniqueIdTest.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/id/NonUniqueIdTest.java b/hibernate-core/src/test/java/org/hibernate/test/id/NonUniqueIdTest.java new file mode 100644 index 0000000000..a9a002703e --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/id/NonUniqueIdTest.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.id; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +import org.hibernate.HibernateException; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.Before; +import org.junit.Test; + +import static org.hibernate.testing.transaction.TransactionUtil.doInHibernate; +import static org.junit.Assert.fail; + +/** + * @author Gail Badner + */ +public class NonUniqueIdTest extends BaseNonConfigCoreFunctionalTestCase { + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { Category.class }; + } + + @Before + public void setup() { + doInHibernate( + this::sessionFactory, + session -> { + // drop and recreate table so it has no primary key + + session.createNativeQuery( + "DROP TABLE CATEGORY" + ).executeUpdate(); + + session.createNativeQuery( + "create table CATEGORY( id integer not null, name varchar(255) )" + ).executeUpdate(); + + session.createNativeQuery( "insert into CATEGORY( id, name) VALUES( 1, 'clothes' )" ) + .executeUpdate(); + session.createNativeQuery( "insert into CATEGORY( id, name) VALUES( 1, 'shoes' )" ) + .executeUpdate(); + + } + ); + } + + @Test + @TestForIssue( jiraKey = "HHH-12802" ) + public void testLoadEntityWithNonUniqueId() { + doInHibernate( + this::sessionFactory, + session -> { + try { + session.get( Category.class, 1 ); + fail( "should have failed because there are 2 entities with id == 1" ); + } + catch ( HibernateException ex) { + // expected + } + } + ); + } + + @Entity + @Table(name = "CATEGORY") + public static class Category { + @Id + private int id; + + private String name; + } +}