Skip to content

Commit 312287a

Browse files
cigalygavinking
authored andcommitted
HHH-19703 Test case -add scalar with Class parameter when UserType is registered for that class
1 parent 3926121 commit 312287a

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
* Copyright Red Hat Inc. and Hibernate Authors
4+
*/
5+
package org.hibernate.orm.test.id.usertype;
6+
7+
import jakarta.persistence.Entity;
8+
import jakarta.persistence.Id;
9+
import jakarta.persistence.Table;
10+
import org.hibernate.boot.model.TypeContributions;
11+
import org.hibernate.boot.model.TypeContributor;
12+
import org.hibernate.query.NativeQuery;
13+
import org.hibernate.service.ServiceRegistry;
14+
import org.hibernate.testing.orm.junit.DomainModel;
15+
import org.hibernate.testing.orm.junit.JiraKey;
16+
import org.hibernate.testing.orm.junit.SessionFactory;
17+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
18+
import org.hibernate.type.SqlTypes;
19+
import org.hibernate.type.descriptor.WrapperOptions;
20+
import org.hibernate.usertype.UserType;
21+
import org.junit.jupiter.api.AfterAll;
22+
import org.junit.jupiter.api.BeforeAll;
23+
import org.junit.jupiter.api.Test;
24+
25+
import java.sql.PreparedStatement;
26+
import java.sql.ResultSet;
27+
import java.sql.SQLException;
28+
import java.util.UUID;
29+
30+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
31+
32+
@DomainModel(
33+
annotatedClasses = AddScalarTest.Book.class,
34+
typeContributors = AddScalarTest.UuidTypeContributor.class
35+
)
36+
@SessionFactory
37+
@JiraKey(value = "HHH-19703")
38+
public class AddScalarTest {
39+
40+
@BeforeAll
41+
static void init(SessionFactoryScope scope) {
42+
scope.inTransaction( session ->
43+
session.persist( new Book( 1L, UUID.randomUUID().toString() ) ) );
44+
}
45+
46+
@AfterAll
47+
static void clean(SessionFactoryScope scope) {
48+
scope.inTransaction( session ->
49+
session.createMutationQuery( "delete from Book" ).executeUpdate() );
50+
}
51+
52+
@Test
53+
public void test(SessionFactoryScope scope) {
54+
final var actual = scope.fromSession( session ->
55+
session.createNativeQuery( "select uuid from book where id=:id" )
56+
.setParameter( "id", Long.valueOf( 1 ) )
57+
.unwrap( NativeQuery.class )
58+
.addScalar( "uuid", Uuid.class )
59+
.getSingleResult() );
60+
assertInstanceOf( Uuid.class, actual );
61+
}
62+
63+
@Entity(name = "Book")
64+
@Table(name = "book")
65+
static class Book {
66+
67+
@Id
68+
private Long id;
69+
70+
private String uuid;
71+
72+
public Book() {
73+
}
74+
75+
public Book(Long id, String uuid) {
76+
this.id = id;
77+
this.uuid = uuid;
78+
}
79+
}
80+
81+
record Uuid(UUID uuid) {
82+
83+
}
84+
85+
static class UuidType implements UserType<Uuid> {
86+
87+
@Override
88+
public int getSqlType() {
89+
return SqlTypes.VARCHAR;
90+
}
91+
92+
@Override
93+
public Class<Uuid> returnedClass() {
94+
return Uuid.class;
95+
}
96+
97+
@Override
98+
public Uuid deepCopy(Uuid value) {
99+
return new Uuid( value.uuid );
100+
}
101+
102+
@Override
103+
public boolean isMutable() {
104+
return false;
105+
}
106+
107+
@Override
108+
public Uuid nullSafeGet(ResultSet rs, int position, WrapperOptions options) throws SQLException {
109+
final var result = rs.getString( position );
110+
return rs.wasNull() ? null : new Uuid( UUID.fromString( result ) );
111+
}
112+
113+
@Override
114+
public void nullSafeSet(PreparedStatement st, Uuid value, int position, WrapperOptions options)
115+
throws SQLException {
116+
if ( value == null ) {
117+
st.setNull( position, getSqlType() );
118+
}
119+
else {
120+
st.setObject( position, value.uuid.toString(), getSqlType() );
121+
}
122+
}
123+
}
124+
125+
static class UuidTypeContributor implements TypeContributor {
126+
127+
@Override
128+
public void contribute(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
129+
typeContributions.contributeType( new UuidType() );
130+
}
131+
}
132+
}

0 commit comments

Comments
 (0)