1

I wonder if it's posible to map Enum VARIABLE and DB. I want to save in database the yellow values (variables of enum)

Enum variable

The enum is used in this class:

@Getter
@Setter
@Table(name = "TIPOS_MOVIMIENTO")
@Entity

public class TipoMovimiento {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column
@Enumerated(EnumType.STRING)
private TipoMov tipo;

public String getTipo() {
    return tipo.getTipoNombre();
}

@OneToMany(mappedBy = "tipoMov")
private List<Movimiento> movimientos;

My DTO class:

@Getter
public class TipoMovimientoDto implements DtoEntity {

private TipoMov tipo;

}

I've tried DTO with

@Convert(converter = TipoMovEnumConverter.class) private TipoMov tipo;

But it doesn't works

Sandrituky
  • 131
  • 1
  • 3
  • 12

1 Answers1

5

Write an AttributeConverter for your enum which will convert your enum data into it's value when store in database.

@Converter(autoApply = true)
public class TipoMovConverter implements AttributeConverter<TipoMov, String> {

  @Override
  public Integer convertToDatabaseColumn(TipoMov attribute) {
    return attribute.getTipoNombre();
  }

  @Override
  public TipoMov convertToEntityAttribute(String value) {
    return value == null ? null : TipoMov.findByValue(value);
  }
}

Note: Here findByValue is a static method to get enum from value string

Eklavya
  • 17,618
  • 4
  • 28
  • 57
  • I didn't mention it but I had already tried a converter (You can see everything here: https://stackoverflow.com/questions/63237129/how-from-entity-to-dto-if-entity-has-an-enum-variable). It was working perfectly but... I had to implement a DTO class for TipoMovimiento and... I didn't find a way to make it work with DTO :( So I decided to try other way that didn't use a converter. – Sandrituky Aug 04 '20 at 16:03
  • There is a difference between converter and attribute converter, attribute converter work also if you directly fetch data from database as dto – Eklavya Aug 04 '20 at 16:06
  • What I did is an attribute converter, I'm kinda noob programmer and I may not explain well (sorry!) When I launch Postman output with Dto... it says: { "tipo": null }, { "tipo": null }, { "tipo": null },...... The only I have in my DTO is this: @Getter public class TipoMovimientoDto implements DtoEntity { private TipoMov tipo; } I've tried writing Enumerated... and other things... but doesn't works! – Sandrituky Aug 04 '20 at 16:27
  • Did you try above solution with `@Enumerated(EnumType.STRING)` removed? – Kavithakaran Kanapathippillai Aug 04 '20 at 16:39
  • Yes @KavithakaranKanapathippillai I tried that yesterday https://stackoverflow.com/questions/63237129/how-from-entity-to-dto-if-entity-has-an-enum-variable – Sandrituky Aug 04 '20 at 16:43
  • You don't need anything just use the attribute convert er and remove your custom setter getter use lombok's one. And make sure you are mapping properly from entity to dto – Eklavya Aug 04 '20 at 17:00
  • the issue with this solution is with the retrieval of data back from the database, you will need some sort of complex and tedious code to map the variable name back to the Enum value – osama yaccoub Aug 13 '21 at 10:44
  • @osamayaccoub You need to create a static map in enum and then use it to get enum from value. Ex: https://stackoverflow.com/a/14319361/4207306 – Eklavya Aug 16 '21 at 07:51