0

I have a prepraredStatement like below and I want to refactor this code and use Spring Data JPA. Problem is I don't know how to map java.sql.Types.BINARY to hibernate types.

String query= "SELECT count(*) FROM USERS WHERE ACCOUNT_ID=?";
query.setObject(1, accountID.toString().replaceAll("-", ""), Types.BINARY);

I tried byte array but when I query it returns empty resultset.

@Column(name = "ACCOUNT_ID", nullable = false)
private byte[] accountId;
1
  • Are there any benefits using Binary type for an ID? Commented Nov 11, 2019 at 15:07

1 Answer 1

2

I'm not really sure that binary type is good for ID, may be you are using UUID and will benefit from it more?

But if you sure that this is what you actually want, then you can use org.hibernate.annotations.Type annotation, like this:

@Column(name = "ACCOUNT_ID", nullable = false)
@Type(type = "org.hibernate.type.BinaryType")
private byte[] accountId;
Sign up to request clarification or add additional context in comments.

3 Comments

@Type(type = "org.hibernate.type.BinaryType") doesn't compile for me. It tells me type isn't a possible parameter. If I try value @Type(value = ""org.hibernate.type.BinaryType"") instead then it tells me that "org.hibernate.type.BinaryType" isn't a correct type. Any help please?
@Type(type = "org.hibernate.type.BinaryType") solved the problem for me. Thanks!
@Cublax You probably use Hibernate 6. As you mentioned, you have to use the value property here and reference the corresponding UserType class. But I don't know the default/shipped UserType instances. Maybe the "new" variant is to use @JdbcType(VarbinaryJdbcType.class) (didn't test this). See stackoverflow.com/a/75361973/7171936

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.