我试图将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;
有没有办法让休眠状态保留值而不是顺序值?
答案 0 :(得分:0)
使用此转换器进行本地测试:
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值将保持不变,而不是序数。您提供的代码示例是否正在为您重现问题?