hibernatehibernate-4.xusertype

Get LobHelper from a UserType context


I'm migrating old Hibernate 2.x code to 4.x. One of the classes is a UserType between byte[] and Blob, and the code does something like this:

public void nullSafeSet(...) {
    ...
    Blob blob = Hibernate.createBlob(bytes);
    ...
}

In Hibernate 4.x, Hibernate.createBlob doesn't exist anymore, so I need to use session.getLobHelper().createBlob(bytes), but I don't know how to get a LobHelper from a Session since I don't have a Session, only a SessionImplementor:

public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor implementor) { ...

Solution

  • I found out about the ContextualLobCreator(LobCreationContext) class, and that SessionImplementor implements LobCreationContext, so I basically converted

    Blob blob = Hibernate.createBlob((byte[]) value);
    

    to

    Blob blob = new ContextualLobCreator(implementor).createBlob((byte[]) value);