Created
July 6, 2021 11:44
-
-
Save jzlima/e0e5da1268be785a0fabced2232e5cb6 to your computer and use it in GitHub Desktop.
Hibernate Field List
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::: | |
.... | |
@Column | |
@Convert(converter = StringToListFlagConverter.class) | |
private List<FlagChave> flags= new ArrayList(); | |
.... | |
Class Converter. | |
@Converter | |
public class StringToListFlagConverter implements AttributeConverter<List<FlagChave>, String> { | |
@Override | |
public String convertToDatabaseColumn(List<FlagChave> flagChaves) { | |
if(!Optional.ofNullable(flagChaves).isPresent()) return ""; | |
return flagChaves.stream().map(FlagChave::toString).collect(Collectors.joining(",")); | |
} | |
@Override | |
public List<FlagChave> convertToEntityAttribute(String joined) { | |
if(!Optional.ofNullable(joined).filter(s -> !s.isEmpty()).isPresent()) return new ArrayList<>(); | |
return | |
Arrays.asList( | |
joined.split(",")) | |
.stream() | |
.map(FlagChave::valueOf).collect(toCollection(ArrayList::new) | |
); | |
} | |
} | |
Class enum. I was created with an enum, but you can do with a String. | |
public enum FlagChave { | |
CHAVE, | |
VALOR_PADRAO, | |
CONDENSAR_LINHAS; | |
@Override | |
public String toString() { | |
return super.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment