java 如何持久化枚举值而不是序数

sxissh06  于 2023-03-16  发布在  Java
关注(0)|答案(2)|浏览(122)

我试图在数据库中持久化ack(Enum)的值,但当它持久化时,它采用的是序数值而不是值本身。我该怎么做才能保存值而不是序数?
转换器

@Convert
public class AnomalieAckConverter implements AttributeConverter<AnomalieAck, Integer> {

    /**
     * @param attribute
     * @return
     * @see javax.persistence.AttributeConverter#convertToDatabaseColumn(java.lang.Object)
     */
    @Override
    public Integer convertToDatabaseColumn(AnomalieAck anoAck) {
        // TODO Auto-generated method stub
        // return anoAck.getValue();
        return null;
    }

    /**
     * @param dbData
     * @return
     * @see javax.persistence.AttributeConverter#convertToEntityAttribute(java.lang.Object)
     */
    @Override
    public AnomalieAck convertToEntityAttribute(Integer ack) {
        // TODO Auto-generated method stub
        // return AnomalieAck.getByValue(ack);

        return null;

    }

枚举:

public enum AnomalieAck {

    NON_ACQUITTEE(0),

    ACQUITTEE_APP1 (1),

    ACQUITTEE_APP2(2),

    /** Aacquittee en erreur. */
    ACQUITTEE_ERREUR(10),

    private int ack = 0;

  private AnomalieAck() {

    }

  private AnomalieAck(final int value) {
    this.ack = value;
   }
  public int getValue() {
        return this.ack;
    }

  public void setAck(int ack) {
        this.ack = ack;
    }

public static AnomalieAck getByValue(int ackNumero) {
    for (final AnomalieAck ackitement : AnomalieAck.values()) {

        if (ackitement.getValue() == ackNumero) {
            return ackitement;
        }
    }

Java类:

@Column(name = "ANO_ACK")
@Convert(converter = AnomalieAckConverter.class)
private AnomalieAck ack = AnomalieAck.NON_ACQUITTEE;

有没有办法让hib持久化值而不是序数值?

mzillmmw

mzillmmw1#

使用此转换器进行本地测试:

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;

@Converter
public class AnomalieAckConverter implements AttributeConverter<AnomalieAck, Integer>
    {

        @Override
        public Integer convertToDatabaseColumn(final AnomalieAck attribute)
        {
            return attribute.getValue();
        }

        @Override
        public AnomalieAck convertToEntityAttribute(final Integer dbData)
        {
            return AnomalieAck.getByValue(dbData);
        }
}

并且该实体字段定义

@Column(name = "ANO_ACK")
    @Convert(converter = AnomalieAckConverter.class)
    private AnomalieAck ack = AnomalieAck.ACQUITTEE_ERREUR;

则枚举中的ack值将被持久化,而不是序号。2您提供的代码示例是否重现了这个问题?

vzgqcmou

vzgqcmou2#

@Converter(autoApply = true)
public class AnomalieAckConverter 
implements 
AttributeConverter<AnomalieAck, 
Integer> {

    @Override
    public Integer convertToDatabaseColumn(AnomalieAck 
anoAck) {
      if(anoAck != null)
        return anoAck.getValue();
    // TODO Auto-generated method stub
    // return anoAck.getValue();
      return null;
   }

/**
 * @param dbData
 * @return
 * @see javax.persistence.AttributeConverter#convertToEntityAttribute(java.lang.Object)
 */
    @Override
    public AnomalieAck convertToEntityAttribute(Integer ack) {
    // TODO filtering and returning the enum instance
    
      if(ack != null)
        return Stream.of(AnomalieAck.values())
     .filter(data -> data.getValue() == ack).findFirst().orElseThrow(IllegalArgumentException::new);

      return null;

}

实体字段定义

private AnomalieAck ack;

使用setter方法将ack注入到服务类中。

相关问题