From fdd7fb80807b18b12a7c417bf15b862bf38e2efb Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Tue, 26 May 2015 12:25:54 +0100 Subject: [PATCH] HHH-9798 - Add test for Unique constraint of @JoinColumn in @JoinTable not generated --- .../annotations/onetoone/hhh9798/Item.java | 41 ++++++++++ .../hhh9798/OneToOneJoinTableTest.java | 52 ++++++++++++ .../onetoone/hhh9798/Shipment.java | 79 +++++++++++++++++++ .../onetoone/hhh9798/ShipmentState.java | 14 ++++ 4 files changed, 186 insertions(+) create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/hhh9798/Item.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/hhh9798/OneToOneJoinTableTest.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/hhh9798/Shipment.java create mode 100644 hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/hhh9798/ShipmentState.java diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/hhh9798/Item.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/hhh9798/Item.java new file mode 100644 index 0000000000..54bb633ce7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/hhh9798/Item.java @@ -0,0 +1,41 @@ +/* + * 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.onetoone.hhh9798; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Item { + + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + protected Long id; + + protected String name; + + public Item() { + } + + public Item(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/hhh9798/OneToOneJoinTableTest.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/hhh9798/OneToOneJoinTableTest.java new file mode 100644 index 0000000000..347ecf95de --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/hhh9798/OneToOneJoinTableTest.java @@ -0,0 +1,52 @@ +/* + * 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.onetoone.hhh9798; + +import org.hibernate.Session; +import org.hibernate.Transaction; + +import org.junit.Test; + +import org.hibernate.testing.TestForIssue; +import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase; + +@TestForIssue(jiraKey = "HHH-9798") +public class OneToOneJoinTableTest extends BaseCoreFunctionalTestCase { + + @Test(expected = org.hibernate.exception.ConstraintViolationException.class) + public void storeNonUniqueRelationship() throws Throwable { + Session session = null; + try { + session = openSession(); + Transaction tx = session.beginTransaction(); + + Item someItem = new Item( "Some Item" ); + session.save( someItem ); + + Shipment shipment1 = new Shipment( someItem ); + session.save( shipment1 ); + + Shipment shipment2 = new Shipment( someItem ); + session.save( shipment2 ); + + tx.commit(); + } + finally { + if ( session != null ) { + session.close(); + } + } + } + + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] { + Shipment.class, + Item.class + }; + } +} \ No newline at end of file diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/hhh9798/Shipment.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/hhh9798/Shipment.java new file mode 100644 index 0000000000..d0a99a062d --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/hhh9798/Shipment.java @@ -0,0 +1,79 @@ +/* + * 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.onetoone.hhh9798; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.JoinTable; +import javax.persistence.OneToOne; +import javax.validation.constraints.NotNull; +import java.util.Date; + +@Entity +public class Shipment { + + @Id + @GeneratedValue(strategy = GenerationType.SEQUENCE) + protected Long id; + + @NotNull + protected Date createdOn = new Date(); + + @NotNull + protected ShipmentState shipmentState = ShipmentState.TRANSIT; + + @OneToOne(fetch = FetchType.LAZY) + @JoinTable( + name = "ITEM_SHIPMENT", + joinColumns = + @JoinColumn(name = "SHIPMENT_ID"), + inverseJoinColumns = + @JoinColumn(name = "ITEM_ID", + nullable = false, + unique = true) + ) + protected Item auction; + + public Shipment() { + } + + public Shipment(Item auction) { + this.auction = auction; + } + + public Long getId() { + return id; + } + + public Date getCreatedOn() { + return createdOn; + } + + public void setCreatedOn(Date createdOn) { + this.createdOn = createdOn; + } + + public ShipmentState getShipmentState() { + return shipmentState; + } + + public void setShipmentState(ShipmentState shipmentState) { + this.shipmentState = shipmentState; + } + + public Item getAuction() { + return auction; + } + + public void setAuction(Item auction) { + this.auction = auction; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/hhh9798/ShipmentState.java b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/hhh9798/ShipmentState.java new file mode 100644 index 0000000000..1ce9062f61 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/annotations/onetoone/hhh9798/ShipmentState.java @@ -0,0 +1,14 @@ +/* + * 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.onetoone.hhh9798; + +public enum ShipmentState { + + TRANSIT, + CONFIRMED + +}