/
RowIdTest.java
125 lines (97 loc) · 3.16 KB
/
RowIdTest.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
/*
* 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.orm.test.rowid;
import java.util.List;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import org.hibernate.annotations.RowId;
import org.hibernate.testing.jdbc.SQLStatementInspector;
import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hibernate.testing.hamcrest.CollectionMatchers.hasSize;
import static org.junit.Assert.assertThat;
/**
* @author Nathan Xu
*/
@DomainModel( annotatedClasses = RowIdTest.Product.class )
@SessionFactory(statementInspectorClass = SQLStatementInspector.class)
public class RowIdTest {
@BeforeEach
void setUp(SessionFactoryScope scope) {
scope.inTransaction( session -> {
Product product = new Product();
product.setId( "1L" );
product.setName( "Mobile phone" );
product.setNumber( "123-456-7890" );
session.persist( product );
} );
}
@Test
void testRowId(SessionFactoryScope scope) {
final String updatedName = "Smart phone";
scope.inTransaction( session -> {
String rowId = scope.getSessionFactory().getJdbcServices().getDialect().rowId();
SQLStatementInspector statementInspector = (SQLStatementInspector) scope.getStatementInspector();
statementInspector.clear();
Product product = session.find( Product.class, "1L" );
List<String> sqls = statementInspector.getSqlQueries();
assertThat( sqls, hasSize( 1 ) );
assertThat( rowId == null
|| sqls.get(0).matches( "(?i).*\\bselect\\b.+\\." + rowId + ".*\\bfrom\\s+product\\b.*" ),
is( true ) );
assertThat( product.getName(), not( is( updatedName ) ) );
product.setName( updatedName );
statementInspector.clear();
session.flush();
sqls = statementInspector.getSqlQueries();
assertThat( sqls, hasSize( 1 ) );
assertThat( rowId == null
|| sqls.get( 0 ).matches( "(?i).*\\bupdate\\s+product\\b.+?\\bwhere\\s+" + rowId + "\\s*=.*" ),
is( true ) );
} );
scope.inTransaction( session -> {
Product product = session.createQuery( "from Product", Product.class ).uniqueResult();
assertThat( product.getName(), is( updatedName ) );
} );
}
@Entity(name = "Product")
@Table(name = "product")
@RowId
public static class Product {
@Id
private String id;
@Column(name = "`name`")
private String name;
@Column(name = "`number`")
private String number;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
}