Skip to content

Commit fdd7fb8

Browse files
committed
HHH-9798 - Add test for Unique constraint of @joincolumn in @jointable not generated
1 parent 905c79c commit fdd7fb8

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.annotations.onetoone.hhh9798;
8+
9+
import javax.persistence.Entity;
10+
import javax.persistence.GeneratedValue;
11+
import javax.persistence.GenerationType;
12+
import javax.persistence.Id;
13+
14+
@Entity
15+
public class Item {
16+
17+
@Id
18+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
19+
protected Long id;
20+
21+
protected String name;
22+
23+
public Item() {
24+
}
25+
26+
public Item(String name) {
27+
this.name = name;
28+
}
29+
30+
public Long getId() {
31+
return id;
32+
}
33+
34+
public String getName() {
35+
return name;
36+
}
37+
38+
public void setName(String name) {
39+
this.name = name;
40+
}
41+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.annotations.onetoone.hhh9798;
8+
9+
import org.hibernate.Session;
10+
import org.hibernate.Transaction;
11+
12+
import org.junit.Test;
13+
14+
import org.hibernate.testing.TestForIssue;
15+
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
16+
17+
@TestForIssue(jiraKey = "HHH-9798")
18+
public class OneToOneJoinTableTest extends BaseCoreFunctionalTestCase {
19+
20+
@Test(expected = org.hibernate.exception.ConstraintViolationException.class)
21+
public void storeNonUniqueRelationship() throws Throwable {
22+
Session session = null;
23+
try {
24+
session = openSession();
25+
Transaction tx = session.beginTransaction();
26+
27+
Item someItem = new Item( "Some Item" );
28+
session.save( someItem );
29+
30+
Shipment shipment1 = new Shipment( someItem );
31+
session.save( shipment1 );
32+
33+
Shipment shipment2 = new Shipment( someItem );
34+
session.save( shipment2 );
35+
36+
tx.commit();
37+
}
38+
finally {
39+
if ( session != null ) {
40+
session.close();
41+
}
42+
}
43+
}
44+
45+
@Override
46+
protected Class<?>[] getAnnotatedClasses() {
47+
return new Class<?>[] {
48+
Shipment.class,
49+
Item.class
50+
};
51+
}
52+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.annotations.onetoone.hhh9798;
8+
9+
import javax.persistence.Entity;
10+
import javax.persistence.FetchType;
11+
import javax.persistence.GeneratedValue;
12+
import javax.persistence.GenerationType;
13+
import javax.persistence.Id;
14+
import javax.persistence.JoinColumn;
15+
import javax.persistence.JoinTable;
16+
import javax.persistence.OneToOne;
17+
import javax.validation.constraints.NotNull;
18+
import java.util.Date;
19+
20+
@Entity
21+
public class Shipment {
22+
23+
@Id
24+
@GeneratedValue(strategy = GenerationType.SEQUENCE)
25+
protected Long id;
26+
27+
@NotNull
28+
protected Date createdOn = new Date();
29+
30+
@NotNull
31+
protected ShipmentState shipmentState = ShipmentState.TRANSIT;
32+
33+
@OneToOne(fetch = FetchType.LAZY)
34+
@JoinTable(
35+
name = "ITEM_SHIPMENT",
36+
joinColumns =
37+
@JoinColumn(name = "SHIPMENT_ID"),
38+
inverseJoinColumns =
39+
@JoinColumn(name = "ITEM_ID",
40+
nullable = false,
41+
unique = true)
42+
)
43+
protected Item auction;
44+
45+
public Shipment() {
46+
}
47+
48+
public Shipment(Item auction) {
49+
this.auction = auction;
50+
}
51+
52+
public Long getId() {
53+
return id;
54+
}
55+
56+
public Date getCreatedOn() {
57+
return createdOn;
58+
}
59+
60+
public void setCreatedOn(Date createdOn) {
61+
this.createdOn = createdOn;
62+
}
63+
64+
public ShipmentState getShipmentState() {
65+
return shipmentState;
66+
}
67+
68+
public void setShipmentState(ShipmentState shipmentState) {
69+
this.shipmentState = shipmentState;
70+
}
71+
72+
public Item getAuction() {
73+
return auction;
74+
}
75+
76+
public void setAuction(Item auction) {
77+
this.auction = auction;
78+
}
79+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.annotations.onetoone.hhh9798;
8+
9+
public enum ShipmentState {
10+
11+
TRANSIT,
12+
CONFIRMED
13+
14+
}

0 commit comments

Comments
 (0)