BAEL-2900 Persisting enums in JPA (#6958)
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
ef91212d4c
commit
0d24260e9b
@@ -0,0 +1,91 @@
|
||||
package com.baeldung.jpa.enums;
|
||||
|
||||
import javax.persistence.*;
|
||||
|
||||
@Entity
|
||||
public class Article {
|
||||
|
||||
@Id
|
||||
private int id;
|
||||
|
||||
private String title;
|
||||
|
||||
@Enumerated(EnumType.ORDINAL)
|
||||
private Status status;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Type type;
|
||||
|
||||
@Basic
|
||||
private int priorityValue;
|
||||
|
||||
@Transient
|
||||
private Priority priority;
|
||||
|
||||
private Category category;
|
||||
|
||||
public Article() {
|
||||
}
|
||||
|
||||
@PostLoad
|
||||
void fillTransient() {
|
||||
if (priorityValue > 0) {
|
||||
this.priority = Priority.of(priorityValue);
|
||||
}
|
||||
}
|
||||
|
||||
@PrePersist
|
||||
void fillPersistent() {
|
||||
if (priority != null) {
|
||||
this.priorityValue = priority.getPriority();
|
||||
}
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public Status getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
public void setStatus(Status status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(Type type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Priority getPriority() {
|
||||
return priority;
|
||||
}
|
||||
|
||||
public void setPriority(Priority priority) {
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
public Category getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
public void setCategory(Category category) {
|
||||
this.category = category;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.jpa.enums;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public enum Category {
|
||||
SPORT("S"), MUSIC("M"), TECHNOLOGY("T");
|
||||
|
||||
private String code;
|
||||
|
||||
Category(String code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.jpa.enums;
|
||||
|
||||
import javax.persistence.AttributeConverter;
|
||||
import javax.persistence.Converter;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@Converter(autoApply = true)
|
||||
public class CategoryConverter implements AttributeConverter<Category, String> {
|
||||
@Override
|
||||
public String convertToDatabaseColumn(Category category) {
|
||||
if (category == null) {
|
||||
return null;
|
||||
}
|
||||
return category.getCode();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Category convertToEntityAttribute(final String code) {
|
||||
if (code == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Stream.of(Category.values())
|
||||
.filter(c -> c.getCode().equals(code))
|
||||
.findFirst()
|
||||
.orElseThrow(IllegalArgumentException::new);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.jpa.enums;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public enum Priority {
|
||||
LOW(100), MEDIUM(200), HIGH(300);
|
||||
|
||||
private int priority;
|
||||
|
||||
private Priority(int priority) {
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
public int getPriority() {
|
||||
return priority;
|
||||
}
|
||||
|
||||
public static Priority of(int priority) {
|
||||
return Stream.of(Priority.values())
|
||||
.filter(p -> p.getPriority() == priority)
|
||||
.findFirst()
|
||||
.orElseThrow(IllegalArgumentException::new);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.jpa.enums;
|
||||
|
||||
public enum Status {
|
||||
OPEN, REVIEW, APPROVED, REJECTED;
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package com.baeldung.jpa.enums;
|
||||
|
||||
enum Type {
|
||||
INTERNAL, EXTERNAL;
|
||||
}
|
||||
Reference in New Issue
Block a user