1
+ package org .hibernate .orm .test .annotations .collectionelement ;
2
+
3
+ import java .sql .PreparedStatement ;
4
+ import java .util .ArrayList ;
5
+ import java .util .List ;
6
+ import java .util .UUID ;
7
+
8
+ import org .hibernate .annotations .JdbcType ;
9
+ import org .hibernate .dialect .H2Dialect ;
10
+ import org .hibernate .dialect .PostgreSQLDialect ;
11
+ import org .hibernate .type .descriptor .jdbc .UUIDJdbcType ;
12
+
13
+ import org .hibernate .testing .orm .junit .DomainModel ;
14
+ import org .hibernate .testing .orm .junit .JiraKey ;
15
+ import org .hibernate .testing .orm .junit .RequiresDialect ;
16
+ import org .hibernate .testing .orm .junit .SessionFactory ;
17
+ import org .hibernate .testing .orm .junit .SessionFactoryScope ;
18
+ import org .junit .jupiter .api .AfterEach ;
19
+ import org .junit .jupiter .api .BeforeEach ;
20
+ import org .junit .jupiter .api .Test ;
21
+
22
+ import jakarta .persistence .Column ;
23
+ import jakarta .persistence .ElementCollection ;
24
+ import jakarta .persistence .Entity ;
25
+ import jakarta .persistence .Id ;
26
+ import jakarta .persistence .IdClass ;
27
+ import jakarta .persistence .LockModeType ;
28
+ import jakarta .persistence .Table ;
29
+
30
+ import static org .assertj .core .api .Assertions .assertThat ;
31
+
32
+ @ DomainModel (
33
+ annotatedClasses = {
34
+ ElementCollectionAndCompositeKeyTest .OauthConnection .class
35
+ }
36
+ )
37
+ @ SessionFactory (exportSchema = false )
38
+ @ JiraKey ("HHH-17964" )
39
+ @ RequiresDialect ( H2Dialect .class )
40
+ @ RequiresDialect ( PostgreSQLDialect .class )
41
+ public class ElementCollectionAndCompositeKeyTest {
42
+
43
+ @ BeforeEach
44
+ public void setUp (SessionFactoryScope scope ) {
45
+ scope .inTransaction (
46
+ session ->
47
+ session .doWork (
48
+ connection -> {
49
+ PreparedStatement preparedStatement = connection .prepareStatement (
50
+ "create table oauth_connection_grantedScopes (" +
51
+ " oauth_connection_connection varchar(255) not null," +
52
+ " oauth_connection_id uuid not null," +
53
+ " grantedScopes varchar(255)" +
54
+ " )" );
55
+ try {
56
+ preparedStatement .executeUpdate ();
57
+ }
58
+ finally {
59
+ preparedStatement .close ();
60
+ }
61
+
62
+
63
+ preparedStatement = connection .prepareStatement (
64
+ "create table oauth_connections (" +
65
+ " id uuid not null," +
66
+ " connection varchar(255) not null," +
67
+ " name varchar(255)," +
68
+ " primary key (connection, id)" +
69
+ " )" );
70
+ try {
71
+ preparedStatement .executeUpdate ();
72
+ }
73
+ finally {
74
+ preparedStatement .close ();
75
+ }
76
+ }
77
+ )
78
+ );
79
+ }
80
+
81
+ @ AfterEach
82
+ public void tearDown (SessionFactoryScope scope ) {
83
+ scope .inTransaction (
84
+ session ->
85
+ session .doWork (
86
+ connection -> {
87
+ PreparedStatement preparedStatement = connection .prepareStatement (
88
+ "drop table oauth_connection_grantedScopes " );
89
+ try {
90
+ preparedStatement .executeUpdate ();
91
+ }
92
+ finally {
93
+ preparedStatement .close ();
94
+ }
95
+ preparedStatement = connection .prepareStatement (
96
+ "drop table oauth_connections " );
97
+ try {
98
+ preparedStatement .executeUpdate ();
99
+ }
100
+ finally {
101
+ preparedStatement .close ();
102
+ }
103
+ }
104
+ )
105
+ );
106
+ }
107
+
108
+ @ Test
109
+ public void testInitilizeElementCollection (SessionFactoryScope scope ) {
110
+ UUID primarySID = UUID .fromString ( "53886a8a-7082-4879-b430-25cb94415be8" );
111
+ String connection = "def" ;
112
+ OauthConnectionId id = new OauthConnectionId ( primarySID , connection );
113
+ scope .inTransaction (
114
+ session -> {
115
+ List <String > grantedScopes = new ArrayList <>();
116
+ grantedScopes .add ( "a" );
117
+ grantedScopes .add ( "b" );
118
+ grantedScopes .add ( "c" );
119
+ OauthConnection oauthConnection = new OauthConnection ( primarySID , connection , grantedScopes );
120
+ session .persist ( oauthConnection );
121
+ }
122
+ );
123
+
124
+ scope .inTransaction (
125
+ session -> {
126
+ OauthConnection con = session .find (
127
+ OauthConnection .class ,
128
+ id
129
+ );
130
+ List <String > grantedScopes = con .getGrantedScopes ();
131
+ grantedScopes .size ();
132
+ }
133
+ );
134
+
135
+ scope .inTransaction (
136
+ session -> {
137
+ OauthConnection con = session .createQuery (
138
+ "select o from oauth_connection o where o.primarySID = :primarySID and o.connection = :connection" ,
139
+ OauthConnection .class
140
+ ).setParameter ( "primarySID" , primarySID ).setParameter ( "connection" , connection ).uniqueResult ();
141
+ List <String > grantedScopes = con .getGrantedScopes ();
142
+ grantedScopes .size ();
143
+ }
144
+ );
145
+
146
+ scope .inTransaction (
147
+ session -> {
148
+ List <OauthConnection > connections = session .createQuery (
149
+ "select o from oauth_connection o " ,
150
+ OauthConnection .class
151
+ ).list ();
152
+ assertThat ( connections .size () ).isEqualTo ( 1 );
153
+ List <String > grantedScopes = connections .get ( 0 ).getGrantedScopes ();
154
+ grantedScopes .size ();
155
+ }
156
+ );
157
+
158
+ scope .inTransaction (
159
+ session -> {
160
+ List <OauthConnection > connections = session .createQuery (
161
+ "select o from oauth_connection o where o.id = :id" ,
162
+ OauthConnection .class
163
+ ).setLockMode ( LockModeType .PESSIMISTIC_WRITE ).setParameter ( "id" , id ).list ();
164
+ assertThat ( connections .size () ).isEqualTo ( 1 );
165
+ List <String > grantedScopes = connections .get ( 0 ).getGrantedScopes ();
166
+ grantedScopes .size ();
167
+ }
168
+ );
169
+
170
+ }
171
+
172
+ @ Entity (name = "oauth_connection" )
173
+ @ Table (name = "oauth_connections" )
174
+ @ IdClass (OauthConnectionId .class )
175
+ public static class OauthConnection {
176
+
177
+ @ Id
178
+ @ Column (name = "id" )
179
+ @ JdbcType (UUIDJdbcType .class )
180
+ private UUID primarySID ;
181
+
182
+ @ Id
183
+ @ Column (name = "connection" )
184
+ private String connection ;
185
+
186
+ @ Column (name = "name" )
187
+ private String name ;
188
+
189
+ @ ElementCollection
190
+ private List <String > grantedScopes ;
191
+
192
+ public OauthConnection () {
193
+ }
194
+
195
+ public OauthConnection (UUID primarySID , String connection , List <String > grantedScopes ) {
196
+ this .primarySID = primarySID ;
197
+ this .connection = connection ;
198
+ this .grantedScopes = grantedScopes ;
199
+ }
200
+
201
+ public UUID getPrimarySID () {
202
+ return primarySID ;
203
+ }
204
+
205
+ public String getConnection () {
206
+ return connection ;
207
+ }
208
+
209
+ public List <String > getGrantedScopes () {
210
+ return grantedScopes ;
211
+ }
212
+ }
213
+
214
+ public static class OauthConnectionId {
215
+
216
+
217
+ @ Id
218
+ @ Column (name = "connection" )
219
+ private String connection ;
220
+
221
+ @ Id
222
+ @ Column (name = "id" )
223
+ @ JdbcType (UUIDJdbcType .class )
224
+ private UUID primarySID ;
225
+
226
+ public OauthConnectionId () {
227
+ }
228
+
229
+ public OauthConnectionId (UUID primarySID , String connection ) {
230
+ this .primarySID = primarySID ;
231
+ this .connection = connection ;
232
+ }
233
+ }
234
+
235
+ }
0 commit comments