0

I'm developing a Bet service with Java and MySql using Spring and Hibernate, but I have a question about a mapping:

In my database I have bet types (questions) like: ¿Who will win the match? and bet options for each type like: 1, X, 2. Each option has a boolean indicator to show if an option is a winner option or not. So, the mapping in MySql for this attribute is a boolean like this: 1 if the option is winner, 0 if the option is looser, and null if the option is not yet neither winner or looser

So, in my service layer, how can I check if the winner attribute in an option is null? I've tried option.getIsWinner() != null but boolean is a primitive type so it can be null. Any idea on this?

Thanks.

1 Answer 1

1

Java has two boolean types; lowercase-b boolean, which is a primitive, and one is capital-B Boolean, which is an object. The object variant can also be null, so it should be possible for it to be null in the event that the database column is null. If you switch over to java.lang.Boolean you should get the behavior you expect. I just looked in the Hibernate docs, and it supports both.

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

3 Comments

Hi, thanks for your answer. So I need to specify @Type(type="java.lang.Boolean") annotation in my getter in order to get a Boolean type from database in Hibernate?
I don't think you'll need an @Type declaration, but you'll need to change the type of your variable from (for example) private boolean option to private Boolean option. There will be some side effects of your interaction with that property as well - you'll need to check for nulls, for one thing.
From the docs: "@org.hibernate.annotations.Type overrides the default hibernate type used: this is generally not necessary since the type is correctly inferred by Hibernate. Please refer to the Hibernate reference guide for more informations on the Hibernate types."

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.