HHH-9798 - Add test for Unique constraint of @JoinColumn in @JoinTable not generated

This commit is contained in:
Andrea Boriero 2015-05-26 12:25:54 +01:00
parent 905c79c65e
commit fdd7fb8080
4 changed files with 186 additions and 0 deletions

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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
};
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
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;
}
}

View File

@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.test.annotations.onetoone.hhh9798;
public enum ShipmentState {
TRANSIT,
CONFIRMED
}