Created
November 14, 2019 10:37
-
-
Save iampaul83/38004c05a8452702ef9b5f5d60de3b1e to your computer and use it in GitHub Desktop.
Spring Data JPA Enum converter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.AttributeConverter; | |
/* | |
Base Enum converter for JPA | |
*/ | |
// @Converter | |
public abstract class AbstractEnumConverter<T extends Enum<T> & PersistableEnum<E>, E> | |
implements AttributeConverter<T, E> { | |
private final Class<T> clazz; | |
AbstractEnumConverter(Class<T> clazz) { | |
this.clazz = clazz; | |
} | |
@Override | |
public E convertToDatabaseColumn(T attribute) { | |
return attribute != null ? attribute.getCode() : null; | |
} | |
@Override | |
public T convertToEntityAttribute(E dbData) { | |
T[] enums = clazz.getEnumConstants(); | |
for (T e : enums) { | |
if (e.getCode().equals(dbData)) { | |
return e; | |
} | |
} | |
throw new UnsupportedOperationException( | |
String.format("%s cannot convert to %s enum value.", dbData, clazz)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CREATE TABLE CHALLENGE_LOG ( | |
ACTION NUMBER(1) NOT NULL | |
); | |
COMMENT ON COLUMN CHALLENGE_LOG.ACTION IS '0=Send OTP, 1=Verify OTP'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lombok.AllArgsConstructor; | |
import lombok.Getter; | |
@Getter | |
@AllArgsConstructor | |
public enum ChallengeAction implements PersistableEnum<Integer> { | |
// 0=Send OTP, 1=Verify OTP | |
SEND_OTP(0), | |
VERIFY_OTP(1); | |
private final Integer code; | |
// JPA converter | |
@javax.persistence.Converter(autoApply = true) | |
public static class Converter extends AbstractEnumConverter<ChallengeAction, Integer> { | |
public Converter() { | |
super(ChallengeAction.class); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
@Data | |
public class ChallengeLog { | |
private ChallengeAction action; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface PersistableEnum<T> { | |
T getCode(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment