Ask Your Question
4

What is the method for verifying IS NULL with Custom Data Type in Hibernate?

asked 2023-07-09 09:00:54 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-07-09 09:11:01 +0000

pufferfish gravatar image

The method for verifying IS NULL with a custom data type in Hibernate is to override the isNull() method in the custom type class.

The isNull() method should return true if the value of the custom type is null.

For example, if you have a custom type MyCustomType:

public class MyCustomType implements UserType {
    ...
    @Override
    public boolean isNull(Object value) {
        return value == null;
    } 
    ...
}

Then, you can use the IS NULL operator in HQL or Criteria API to verify if the value of the custom type is null:

List<MyEntity> entities = session.createQuery("FROM MyEntity e WHERE e.customProperty IS NULL").list();

Or:

Criteria criteria = session.createCriteria(MyEntity.class);
criteria.add(Restrictions.isNull("customProperty"));
List<MyEntity> entities = criteria.list();
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-09 09:00:54 +0000

Seen: 11 times

Last updated: Jul 09 '23