#Use enums to store data in database or just use a string?

19 messages · Page 1 of 1 (latest)

inner kayak
#

I have a project where on the frontend you select from options, Easy medium and hard and on the backend I have a enum that represents the same data. When I save the data to the database it saves in correctly, difficulty will be EASY, MEDIUM or HARD, but when I fetch the data to display it on the frontend the difficulty value may say MEDIUM but if I compare it to the string MEDIUM so i can conditionally apply some CSS they are not the same type. Is this because an enum is really a string and when I make the comparison its always false cause the types are different?

    @Enumerated(EnumType.STRING)
    private Difficulty difficulty;
public enum Difficulty {
    EASY("easy"),
    MEDIUM("medium"),
    HARD("hard");

    private String difficulty;
    Difficulty(String difficulty) {
        this.difficulty = difficulty;
    }

    public String getDifficulty() {
        return difficulty;
    }

    public void setDifficulty(String difficulty) {
        this.difficulty = difficulty;
    }
}
<th:block th:switch="${build.difficulty}">
  <span class="badge rounded-pill text-bg-success" th:case="EASY" th:text="${build.difficulty}"></span>
  <span class="badge rounded-pill text-bg-warning" th:case="MEDIUM" th:text="${build.difficulty}">       </span>
  <span class="badge rounded-pill text-bg-danger" th:case="HARD" th:text="${build.difficulty}"></span>
</th:block>
rough flareBOT
#

This post has been reserved for your question.

Hey @inner kayak! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

stone bronze
#

I'm not sure what you mean. You're actually comparing your enums to Strings here.
It's because enums aren't strings, that it wouldn't match

inner kayak
#

Doesn't this annotation save the enum's as strings in the database so when I retrieve them wouldn't they be strings?

 @Enumerated(EnumType.STRING)
    private Difficulty difficulty;
#

Maybe im over complicating this, and I should just use Strings? But then someone could open up the inspector and modify my select options and pass in w.e they want

stone bronze
#

Of course not, when you retrieve them they're back in your entity

#

Your entity uses type Difficulty, not String

stone bronze
inner kayak
#

i plan to add input validation, I decided to try to use enums in this project and they are just arent making sense to me. I thought by using them I limit that possible selections someone has access to but the way im using them is clearly wrong.

stone bronze
#

Fundamentally, it rather gives me the impression that Thymeleaf's switch/case constructs suck and that Thymeleaf's enum support in particular is atrocious

#

But maybe they supposedly support them in a saner way and it's just the examples that suck

inner kayak
#

So then in ur opinion would it be a better idea to just set difficulty as a String instead of an enum, and then just validate the input to insure that only 1 of those 3 options is passed to the backend?

stone bronze
#

Meh. In my opinion I'd wonder whether you couldn't do better than this switch/case thing.
But if it truly becomes so annoying, you can convert an enum to Stringby calling name() on it, you know

inner kayak
# stone bronze Meh. In my opinion I'd wonder whether you couldn't do better than this switch/ca...

I've been trying to get this working since yesterday and finally caved and decided to ask for help. I'm not too familiar with thymeleaf, but since I'm learning spring boot I figured id try to make a project with it instead of using React. When I googled how to conditionally render a class there was a few options, but the switch case seemed like the best option because I had more than 2 possible options I needed to handle.

#

Dumb question, what if I made a DTO that mapped the String value that is passed from the frontend to the backend and then based on the string map it to the corresponding enum?

stone bronze
#

i have no idea what you hope to gain by involving strings where an enum model is involved. But why not if you see what you gain from it

inner kayak
#

I don't understand how I can't involve strings. If thymleaf doesn't recognize the value MEDIUM that comes from the enum as being the same as "MEDIUM", how can I conditionally render my classes? JavaScript doesn't have a enum type so how would it know what a enum is. I figured it has to be converted to a string then so i can compare the same types.

rough flareBOT
# rough flare

Before your post will be closed, would you like to express your gratitude to any of the people who helped you? When you're done, click I'm done here. Close this post!.