jpa oracle boolean,配置休眠(使用JPA)来存储类型为布尔值而不是0/1的Y/N.

Jim Tough..

11

我使用了@marcg发布的答案中的概念,它与JPA 2.1一起使用效果很好.他的代码不太正确,所以我发布了我的工作实现.这会将Boolean实体字段转换为数据库中的Y/N字符列.

从我的实体类:

@Convert(converter=BooleanToYNStringConverter.class)

@Column(name="LOADED", length=1)

private Boolean isLoadedSuccessfully;

我的转换器类:

/**

* Converts a Boolean entity attribute to a single-character

* Y/N string that will be stored in the database, and vice-versa

*

* @author jtough

*/

public class BooleanToYNStringConverter

implements AttributeConverter {

/**

* This implementation will return "Y" if the parameter is Boolean.TRUE,

* otherwise it will return "N" when the parameter is Boolean.FALSE.

* A null input value will yield a null return value.

* @param b Boolean

*/

@Override

public String convertToDatabaseColumn(Boolean b) {

if (b == null) {

return null;

}

if (b.booleanValue()) {

return "Y";

}

return "N";

}

/**

* This implementation will return Boolean.TRUE if the string

* is "Y" or "y", otherwise it will ignore the value and return

* Boolean.FALSE (it does not actually look for "N") for any

* other non-null string. A null input value will yield a null

* return value.

* @param s String

*/

@Override

public Boolean convertToEntityAttribute(String s) {

if (s == null) {

return null;

}

if (s.equals("Y") || s.equals("y")) {

return Boolean.TRUE;

}

return Boolean.FALSE;

}

}

如果您喜欢表情符号,并且只是厌倦了数据库中的Y/N或T/F,这个变体也很有趣.在这种情况下,您的数据库列必须是两个字符而不是一个.可能不是什么大不了的事.

/**

* Converts a Boolean entity attribute to a happy face or sad face

* that will be stored in the database, and vice-versa

*

* @author jtough

*/

public class BooleanToHappySadConverter

implements AttributeConverter {

public static final String HAPPY = ":)";

public static final String SAD = ":(";

/**

* This implementation will return ":)" if the parameter is Boolean.TRUE,

* otherwise it will return ":(" when the parameter is Boolean.FALSE.

* A null input value will yield a null return value.

* @param b Boolean

* @return String or null

*/

@Override

public String convertToDatabaseColumn(Boolean b) {

if (b == null) {

return null;

}

if (b) {

return HAPPY;

}

return SAD;

}

/**

* This implementation will return Boolean.TRUE if the string

* is ":)", otherwise it will ignore the value and return

* Boolean.FALSE (it does not actually look for ":(") for any

* other non-null string. A null input value will yield a null

* return value.

* @param s String

* @return Boolean or null

*/

@Override

public Boolean convertToEntityAttribute(String s) {

if (s == null) {

return null;

}

if (HAPPY.equals(s)) {

return Boolean.TRUE;

}

return Boolean.FALSE;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值