简体   繁体   中英

MySQL mapping null boolean to Java with Hibernate

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM