diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/usertype/EnhancedUserTypeTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/usertype/EnhancedUserTypeTest.java new file mode 100644 index 000000000000..f0d6f2f0c0d7 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/usertype/EnhancedUserTypeTest.java @@ -0,0 +1,17 @@ +package org.hibernate.orm.test.annotations.usertype; + +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.Test; + +@SessionFactory +@DomainModel(annotatedClasses = MyEntity.class) +public class EnhancedUserTypeTest { + + @Test + void test(SessionFactoryScope scope) { + scope.inTransaction(session -> session.persist(new MyEntity(new MyId("x1"), "hello world"))); + } + +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/usertype/MyEntity.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/usertype/MyEntity.java new file mode 100644 index 000000000000..7d4f071364f4 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/usertype/MyEntity.java @@ -0,0 +1,22 @@ +package org.hibernate.orm.test.annotations.usertype; + +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import org.hibernate.annotations.Type; + +@Entity +class MyEntity { + @Id + @Type(MyType.class) + MyId id; + + String content; + + MyEntity(MyId id, String content) { + this.id = id; + this.content = content; + } + + MyEntity() { + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/usertype/MyId.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/usertype/MyId.java new file mode 100644 index 000000000000..7c6b3271c6bb --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/usertype/MyId.java @@ -0,0 +1,9 @@ +package org.hibernate.orm.test.annotations.usertype; + +class MyId { + final String text; + + MyId(String text) { + this.text = text; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/usertype/MyType.java b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/usertype/MyType.java new file mode 100644 index 000000000000..8c06ddc0831c --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/annotations/usertype/MyType.java @@ -0,0 +1,80 @@ +package org.hibernate.orm.test.annotations.usertype; + +import org.hibernate.HibernateException; +import org.hibernate.engine.spi.SharedSessionContractImplementor; +import org.hibernate.type.SqlTypes; +import org.hibernate.usertype.EnhancedUserType; + +import java.io.Serializable; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +class MyType implements EnhancedUserType { + @Override + public String toSqlLiteral(MyId value) { + return "'" + value.text.replace("'", "''") + "'"; + } + + @Override + public String toString(MyId value) throws HibernateException { + return value.text; + } + + @Override + public MyId fromStringValue(CharSequence sequence) throws HibernateException { + return new MyId(sequence.toString()); + } + + @Override + public int getSqlType() { + return SqlTypes.VARCHAR; + } + + @Override + public Class returnedClass() { + return MyId.class; + } + + @Override + public boolean equals(MyId x, MyId y) { + return x != null && y != null && x.text.equals(y.text); + } + + @Override + public int hashCode(MyId x) { + return x.text.hashCode(); + } + + @Override + public MyId nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) + throws SQLException { + return new MyId(rs.getString(position)); + } + + @Override + public void nullSafeSet(PreparedStatement st, MyId value, int index, SharedSessionContractImplementor session) + throws SQLException { + st.setString(index, value.text); + } + + @Override + public MyId deepCopy(MyId value) { + return value; + } + + @Override + public boolean isMutable() { + return false; + } + + @Override + public Serializable disassemble(MyId value) { + return value.text; + } + + @Override + public MyId assemble(Serializable cached, Object owner) { + return new MyId((String) cached); + } +}