1

I want to know if it is possible to map a table column "substring" to a field, here is a sample. Initially --> column named 'description' contains this string '123456789' After mapping --> java class field named 'descSummary' contains '123'.

Thanks in advance.

2 Answers 2

1

Yes using a Column Transformer.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you for your answear, it is clean ... didn't get it out when browsing hibernate reference doc.
0

One way is to use a Hibernate custom UserType.

A rough example (some methods from the interface are left out for readability):

package org.example;

public class SubstringUserType implements UserType {
    @Override
    public Class<String> returnedClass() {
        return String.class;
    }

    @Override
    public Object nullSafeGet(final ResultSet rs, final String[] names, final SessionImplementor    session, final Object owner) throws HibernateException, SQLException {
        if (rs != null || names.length != 0 || null != names[0]) {
            String desc = rs.getString(names[0]);
            return desc.substring(0, 3);
        }
    }
}

and use the following annotation on the field.

@Type(type = "org.example.SubstringUserType")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.