Skip to content

Commit 4a3e6c7

Browse files
committed
test showing two problems with user types
1. they must be public classes, or Hibernate refuses to instantiate them 2. even after making it public the EnhancedUserType fails in a trivial test
1 parent dc3ce66 commit 4a3e6c7

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.hibernate.orm.test.annotations.usertype;
2+
3+
import org.hibernate.testing.orm.junit.DomainModel;
4+
import org.hibernate.testing.orm.junit.SessionFactory;
5+
import org.hibernate.testing.orm.junit.SessionFactoryScope;
6+
import org.junit.jupiter.api.Test;
7+
8+
@SessionFactory
9+
@DomainModel(annotatedClasses = MyEntity.class)
10+
public class EnhancedUserTypeTest {
11+
12+
@Test
13+
void test(SessionFactoryScope scope) {
14+
scope.inTransaction(session -> session.persist(new MyEntity(new MyId("x1"), "hello world")));
15+
}
16+
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.hibernate.orm.test.annotations.usertype;
2+
3+
import jakarta.persistence.Entity;
4+
import jakarta.persistence.Id;
5+
import org.hibernate.annotations.Type;
6+
7+
@Entity
8+
class MyEntity {
9+
@Id
10+
@Type(MyType.class)
11+
MyId id;
12+
13+
String content;
14+
15+
MyEntity(MyId id, String content) {
16+
this.id = id;
17+
this.content = content;
18+
}
19+
20+
MyEntity() {
21+
}
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package org.hibernate.orm.test.annotations.usertype;
2+
3+
class MyId {
4+
final String text;
5+
6+
MyId(String text) {
7+
this.text = text;
8+
}
9+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.hibernate.orm.test.annotations.usertype;
2+
3+
import org.hibernate.HibernateException;
4+
import org.hibernate.engine.spi.SharedSessionContractImplementor;
5+
import org.hibernate.type.SqlTypes;
6+
import org.hibernate.usertype.EnhancedUserType;
7+
8+
import java.io.Serializable;
9+
import java.sql.PreparedStatement;
10+
import java.sql.ResultSet;
11+
import java.sql.SQLException;
12+
13+
class MyType implements EnhancedUserType<MyId> {
14+
@Override
15+
public String toSqlLiteral(MyId value) {
16+
return "'" + value.text.replace("'", "''") + "'";
17+
}
18+
19+
@Override
20+
public String toString(MyId value) throws HibernateException {
21+
return value.text;
22+
}
23+
24+
@Override
25+
public MyId fromStringValue(CharSequence sequence) throws HibernateException {
26+
return new MyId(sequence.toString());
27+
}
28+
29+
@Override
30+
public int getSqlType() {
31+
return SqlTypes.VARCHAR;
32+
}
33+
34+
@Override
35+
public Class<MyId> returnedClass() {
36+
return MyId.class;
37+
}
38+
39+
@Override
40+
public boolean equals(MyId x, MyId y) {
41+
return x != null && y != null && x.text.equals(y.text);
42+
}
43+
44+
@Override
45+
public int hashCode(MyId x) {
46+
return x.text.hashCode();
47+
}
48+
49+
@Override
50+
public MyId nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner)
51+
throws SQLException {
52+
return new MyId(rs.getString(position));
53+
}
54+
55+
@Override
56+
public void nullSafeSet(PreparedStatement st, MyId value, int index, SharedSessionContractImplementor session)
57+
throws SQLException {
58+
st.setString(index, value.text);
59+
}
60+
61+
@Override
62+
public MyId deepCopy(MyId value) {
63+
return value;
64+
}
65+
66+
@Override
67+
public boolean isMutable() {
68+
return false;
69+
}
70+
71+
@Override
72+
public Serializable disassemble(MyId value) {
73+
return value.text;
74+
}
75+
76+
@Override
77+
public MyId assemble(Serializable cached, Object owner) {
78+
return new MyId((String) cached);
79+
}
80+
}

0 commit comments

Comments
 (0)