R
R
roman_CH2018-11-07 22:56:51
Java
roman_CH, 2018-11-07 22:56:51

How to implement Enums in java Spring JPA?

Good evening everyone. Help please, I can not implement the use of enumeration as a field of an object. An object can only have one enum value.
Now I'm doing through the collection:

@ElementCollection(targetClass = Quality.class, fetch = FetchType.EAGER)
    @CollectionTable(name = "quality", joinColumns = @JoinColumn(name = "realty_id"))
    @Enumerated(EnumType.STRING)
    private Set<Quality> quality;

and everything works, but I need this
@Column(name = "quality", nullable = false, unique = false)
    @Enumerated(EnumType.STRING)
    private Quality quality;

but with this option, an error occurs when opening a web page:
No key, method or field with name 'quality' on line 16
here is the page itself:
<html>
<body>
<div>Список объектов</div>
<span><a href="newobj">Создать объект</a></span>
<div>
    <form method="post">
        <input type="number" name="price1" placeholder="цена от" value=0>
        <input type="number" name="price2" placeholder="цена до" value=0>
        <button type="submit">Применить фильтр</button>
    </form>
{{#realties}}
    <div>
        <b>{{id}}</b>
        <b>{{description}}</b>
        <b>{{price}}</b>
        <b>{{quality}}</b>
    </div>
{{/realties}}
</body>
</html>

Tell me why this implementation does not work?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mipan, 2018-11-08
@roman_CH

Add a constructor with a parameter to enum.
Should look something like this:

public enum  Quality {
    FINE("fine");
private String value;

Quality (String value) {this.value = value;}

@Override
public String toString(){
    return value;
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question