Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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")));
}

}
Original file line number Diff line number Diff line change
@@ -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() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.hibernate.orm.test.annotations.usertype;

class MyId {
final String text;

MyId(String text) {
this.text = text;
}
}
Original file line number Diff line number Diff line change
@@ -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<MyId> {
@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<MyId> 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);
}
}
Loading