Skip to content

Commit 6d925fb

Browse files
NesrinAsanDavideD
NesrinAsan
authored andcommitted
[#1673] Test @SQLInsert and @SQLDelete with one-to-one association
1 parent e9f4513 commit 6d925fb

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed
Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/* Hibernate, Relational Persistence for Idiomatic Java
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Copyright: Red Hat Inc. and Hibernate Authors
5+
*/
6+
package org.hibernate.reactive;
7+
8+
import io.vertx.junit5.Timeout;
9+
import io.vertx.junit5.VertxTestContext;
10+
import jakarta.persistence.*;
11+
12+
import org.hibernate.annotations.SQLDelete;
13+
import org.hibernate.annotations.SQLInsert;
14+
import org.hibernate.reactive.testing.DBSelectionExtension;
15+
16+
import org.junit.jupiter.api.Assertions;
17+
import org.junit.jupiter.api.BeforeEach;
18+
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.api.extension.RegisterExtension;
20+
21+
import java.time.LocalDateTime;
22+
import java.util.Collection;
23+
import java.util.List;
24+
25+
import static java.util.concurrent.TimeUnit.*;
26+
import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.*;
27+
import static org.hibernate.reactive.testing.DBSelectionExtension.*;
28+
import static org.junit.jupiter.api.Assertions.*;
29+
30+
@Timeout(value = 10, timeUnit = MINUTES)
31+
32+
public class CustomOneToOneStoredProcedureSqlTest extends BaseReactiveTest {
33+
34+
@RegisterExtension
35+
public DBSelectionExtension dbSelection = runOnlyFor( POSTGRESQL );
36+
private IndividualPerson individualPerson;
37+
private DriverLicence driverLicence;
38+
private static final String INITIAL_LICENCE_NO = "12545KLI12";
39+
private static final String INSERT_DRIVER_LICENCE_SQL = "CREATE OR REPLACE FUNCTION PROC_INSERT_INDIVIDUAL_PERSON_DRIVER_LICENCE995 ( " +
40+
" ID_PARAM IN bigint, " +
41+
" TEXT_PARAM IN varchar(255), " +
42+
" ID_PERSON_PARAM IN bigint " +
43+
" ) " +
44+
" RETURNS void AS " +
45+
"$BODY$ " +
46+
" BEGIN " +
47+
" insert into DRIVER_LICENCE (individual_person_Id, id, licenceNo, updated) values (ID_PERSON_PARAM, ID_PARAM, TEXT_PARAM, localtimestamp); " +
48+
" END; " +
49+
"$BODY$ " +
50+
"LANGUAGE plpgsql;";
51+
52+
private static final String DELETE_DRIVER_LICENCE_SQL = "CREATE OR REPLACE FUNCTION PROC_DELETE_INDIVIDUAL_PERSON_DRIVER_LICENCE928 ( " +
53+
" ID_PARAM IN bigint" +
54+
" ) RETURNS void AS " +
55+
"$BODY$ " +
56+
" BEGIN " +
57+
" update DRIVER_LICENCE set deleted=localtimestamp where id=ID_PARAM; " +
58+
" END; " +
59+
"$BODY$ " +
60+
"LANGUAGE plpgsql;";
61+
62+
63+
@Override
64+
protected Collection<Class<?>> annotatedEntities() {
65+
return List.of( IndividualPerson.class, DriverLicence.class );
66+
}
67+
68+
@BeforeEach
69+
public void populateDb(VertxTestContext context) {
70+
individualPerson = new IndividualPerson();
71+
individualPerson.name = "Doruk";
72+
73+
driverLicence = new DriverLicence();
74+
driverLicence.licenceNo = INITIAL_LICENCE_NO;
75+
driverLicence.individualPerson = individualPerson;
76+
77+
test( context, openSession()
78+
.thenCompose( s -> s
79+
.createNativeQuery( INSERT_DRIVER_LICENCE_SQL ).executeUpdate()
80+
.thenCompose( v -> s.createNativeQuery( DELETE_DRIVER_LICENCE_SQL ).executeUpdate() )
81+
.thenCompose( v -> s.persist( individualPerson, driverLicence ) )
82+
.thenCompose( v -> s.flush() )
83+
)
84+
);
85+
}
86+
87+
@Test
88+
public void testInsertStoredProcedureDriverLicence(VertxTestContext context) {
89+
test( context, openSession().thenCompose( session -> session
90+
.find( DriverLicence.class, driverLicence.id )
91+
.thenAccept( Assertions::assertNotNull ) )
92+
);
93+
}
94+
95+
96+
@Test
97+
public void testDeleteStoredProcedure(VertxTestContext context) {
98+
test( context, openSession()
99+
.thenCompose( session -> session
100+
.find( DriverLicence.class, driverLicence.id )
101+
.thenCompose( session::remove )
102+
.thenCompose( v -> session.flush() ) )
103+
.thenCompose( v -> openSession() )
104+
.thenCompose( session -> session.find( DriverLicence.class, driverLicence.id ) )
105+
.thenAccept( foundRecord -> {
106+
assertEquals( INITIAL_LICENCE_NO, foundRecord.licenceNo );
107+
assertNotNull( foundRecord.deleted );
108+
assertNotNull( foundRecord.updated );
109+
110+
} )
111+
);
112+
}
113+
114+
@Entity
115+
@Table(name = "DRIVER_LICENCE")
116+
@SQLInsert(sql = "SELECT PROC_INSERT_INDIVIDUAL_PERSON_DRIVER_LICENCE995( $1, $2, $3 );", callable = true)
117+
@SQLDelete(sql = "SELECT PROC_DELETE_INDIVIDUAL_PERSON_DRIVER_LICENCE928( $1 );", callable = true)
118+
public static class DriverLicence {
119+
@GeneratedValue
120+
@Id
121+
long id;
122+
123+
@Basic(optional = false)
124+
String licenceNo;
125+
126+
@OneToOne
127+
@JoinColumn(name = "individual_person_Id")
128+
IndividualPerson individualPerson;
129+
130+
@Column(insertable = false, updatable = false, nullable = false)
131+
LocalDateTime updated;
132+
133+
@Column(insertable = false, updatable = false)
134+
LocalDateTime deleted;
135+
136+
}
137+
138+
@Entity(name = "INDIVIDUAL_PERSON")
139+
@Table(name = "INDIVIDUAL_PERSON")
140+
public static class IndividualPerson {
141+
@GeneratedValue
142+
@Id
143+
long id;
144+
145+
@Basic(optional = false)
146+
String name;
147+
148+
}
149+
150+
}

0 commit comments

Comments
 (0)