-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
MergeNotNullCollectionUsingIdentityTest.java
154 lines (126 loc) · 3.48 KB
/
MergeNotNullCollectionUsingIdentityTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
* 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.beanvalidation;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.constraints.NotNull;
import org.junit.Test;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.testing.DialectChecks;
import org.hibernate.testing.FailureExpected;
import org.hibernate.testing.RequiresDialectFeature;
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.BaseCoreFunctionalTestCase;
/**
* @author Ryan Emerson
*/
@RequiresDialectFeature( value = {
DialectChecks.SupportsIdentityColumns.class,
DialectChecks.SupportsNoColumnInsert.class
}, jiraKey = "HHH-9979")
@TestForIssue( jiraKey = "HHH-9979")
public class MergeNotNullCollectionUsingIdentityTest extends BaseCoreFunctionalTestCase {
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] {Parent.class, Child.class};
}
@Override
protected void configure(Configuration cfg) {
super.configure( cfg );
cfg.setProperty( "javax.persistence.validation.mode", "AUTO" );
}
@Test
@FailureExpected(jiraKey = "HHH-9979")
public void testOneToManyNotNullCollection() {
Parent parent = new Parent();
Child child = new Child();
List<Child> children = new ArrayList<Child>();
children.add( child );
child.setParent( parent );
parent.setChildren( children );
Session s = openSession();
Transaction t = s.beginTransaction();
parent = (Parent) s.merge( parent );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.delete( parent );
t.commit();
s.close();
}
@Test(expected = ConstraintViolationException.class)
public void testOneToManyNullCollection() {
Parent parent = new Parent();
Child child = new Child();
child.setParent( parent );
Session s = openSession();
Transaction t = s.beginTransaction();
parent = (Parent) s.merge( parent );
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
s.delete( parent );
t.commit();
s.close();
}
@Entity
@Table(name = "PARENT")
static class Parent {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@OneToMany(mappedBy = "parent", cascade = CascadeType.ALL, orphanRemoval = true)
@NotNull
private List<Child> children;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public List<Child> getChildren() {
return children;
}
public void setChildren(List<Child> children) {
this.children = children;
}
}
@Entity
@Table(name = "CHILD")
static class Child {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
private Parent parent;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Parent getParent() {
return parent;
}
public void setParent(Parent parent) {
this.parent = parent;
}
}
}