-3

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.

15
  • 4
    To prevent NPE, always write it the other way around: "1".equals(foo.getStateClient()) Commented Feb 18, 2020 at 10:17
  • 2
    Never say never! Anyway, that's a good habit to take Commented Feb 18, 2020 at 10:19
  • 1
    if you are using ORM for mapping database table to java class entity, then you could use Boolean type for such fields.. Framework will transform it under the hood. Of course it depends on specific framework and data types Commented Feb 18, 2020 at 10:19
  • 1
    Isn't duplicate, because i am asking the best way to do my especific problem, not how do.@AndreyBelkin Commented Feb 18, 2020 at 10:39
  • 1
    Closing as opinion based as it deals with a legacy database and system, therefore being dependent on many many things as to what is the "best practice", such as "if it ain't broke, don't fix it" and others. Commented Feb 18, 2020 at 10:48

3 Answers 3

3

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;
Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

They are using varchar with 1 size, its a joke how people do this database...
@Smile then provided the solution for using boolean fields. You might non-nullable defaulting to either false or true.
1

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;
    }
}

1 Comment

I know how doing, but my question, its what the best way to do that. And if should do that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.