这是较旧的帖子,但也许有人可以提供帮助。我需要将字符串转换为布尔值并返回。
我写了一个转换器:
@Converter(autoApply = true)
public class BusinessEnvironmentConverter implements AttributeConverter<Boolean, String> {
enum BusinessEnvironment { TEST, PRODUCTION }
@Override
public String convertToDatabaseColumn(final Boolean production) {
return production ? BusinessEnvironment.PRODUCTION.name(): BusinessEnvironment.TEST.name();
}
@Override
public Boolean convertToEntityAttribute(final String dbField) {
final BusinessEnvironment businessEnvironment = BusinessEnvironment.valueOf(dbField);
return businessEnvironment.equals(BusinessEnvironment.PRODUCTION) ? TRUE : FALSE;
}
}
并使用它:
@Column(name=PRODUCTION, nullable=false)
@Convert(
converter=BusinessEnvironmentConverter.class,
disableConversion=false
)
private boolean production;