1

I'm using Spring Boot as a backend and PostgreSQL as a database, and I'm creating a database table named particle using the class particle.java

I have this enum class:

enum Element {
    H("Hydrogen"),
    HE("Helium"),

    NE("Neon");
 
    public final String label;
 
    private Element(String label) {
        this.label = label;
    }
}

And I have my class particle.java like this:

@Entity
@Table(name = "particle")
public class Particle {
    @Id
    private String code;
    @Enumerated(EnumType.STRING)
    private Element my_element;
    
    public Particle (String code, Element my_element){
        this.code = code;
        this.my_element=my_element
    }
    
    @Column(name = "code", nullable = false)
    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    @Column(name = "element", nullable = false)
    public Element getMy_element() {
        return my_element;
    }

    public void setMy_element(Element my_element) {
        this.my_element = my_element;
    }
}

I want that the column my_element in my database table "particle" gets the value of an element for example Hydrogen instead of H. How to achieve that?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
miro miro
  • 29
  • 1
  • 2
  • Check this article - https://thorben-janssen.com/hibernate-enum-mappings/. Second option looks like what you are looking for. – Timur Levadny Oct 24 '20 at 03:37
  • This question has been answered so many time. For instance: https://stackoverflow.com/questions/2751733/map-enum-in-jpa-with-fixed-values – Prashant Oct 24 '20 at 03:59
  • 1
    Does this answer your question? [Map enum in JPA with fixed values?](https://stackoverflow.com/questions/2751733/map-enum-in-jpa-with-fixed-values) – Prashant Oct 24 '20 at 03:59

0 Answers0