Home » Java » spring – Best way to convert String to Boolean in Java-Exceptionshub

spring – Best way to convert String to Boolean in Java-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I currently have a question about the database of my work.

They use a String based on 0 and 1 to indicate whether it is active or not. But for my logic I would like to use true or false.

Why? Because have redundant code. For example:

if(foo.getStateClient().equals("1"))
   //Do things...

Imagine that with 6 or 7 fields more.

Cant modify the database. And I dont know if use Strings like the before example, or do anything to cast the String a Boolean.

And, always the state is 0 or 1

I appreciate any advice to be more optimal

Edit 1
I am not asking how doing, i am asking the Best practice to do that.

I am using Spring with Hibernate that for people ask me what ORM i am using.

How to&Answers:

You can use Boolean type for your attribute in your entity and use AttributeConverter for handling conversions between Boolean in entity and String/varchar in database.

@Converter
public class BooleanConverter implements AttributeConverter<Boolean, String> {

    @Override
    public Character convertToDatabaseColumn(Boolean value) {
        if (value != null) {
            if (value) {
                return '1';
            } else {
                return '0';
            }

        }
        return null;
    }

    @Override
    public Boolean convertToEntityAttribute(String value) {
        if (value != null) {
            return !value.equals('0');
        }
        return null;
    }

}

Entity:

@Convert(converter = BooleanConverter.class)
private boolean status;

Answer:

In the database a TINYINT 0/1 can immediately be a boolean on the java side.
So make the fields boolean (just maybe sometimes Boolean) and try whether it works.

In this way one may incrementally refactor all quasi-boolean fields to real ones.

If the fields are CHAR(1) instead, you still might succeed, possibly with some converter on the java entity side.

In this way your data model is clean, and there is no artiificial boiler plate code.

Answer:

Just create a helper method that you can reuse for this type of thing

i.e.

public class BooleanHelper {

    public static boolean fromString(String value) {
        if (value == null) {
            return false;
        }
        if ("1".equals(value) || "true".equalsIgnoreCase(value)) {
            return true;
        }
        return false;
    }

}

that way you you can use it where ever you need

if(BooleanHelper.fromString(foo.getStateClient()))
   //Do things...

You could also statically import the method, to reduce your boilerplate

EDIT: OP indicated using Spring and Hibernate

As you have indicated you are using Spring and Hibernate, you can create an AttributeConverter for this

public class BooleanAttributeConverter implements AttributeConverter<Boolean, String> {
    @Override
    public String convertToDatabaseColumn(Boolean value) {
        if (value == null) {
            return "0";
        }
        return value ? "1" : "0";
    }

    @Override
    public Boolean convertToEntityAttribute(String value) {
        if (value == null) {
            return false;
        }
        if ("1".equals(value) || "true".equalsIgnoreCase(value)) {
            return true;
        }
        return false;
    }
}

Then specify this on your entity

@Entity
public class Foo {

    @Column
    @Convert(converter = BooleanAttributeConverter.class)
    private boolean stateClient;

    public boolean isStateClient() {
        return stateClient;
    }

    public void setStateClient(boolean stateClient) {
        this.stateClient = stateClient;
    }
}