简体   繁体   English

Hibernate中的布尔值true

[英]Boolean true value in Hibernate

Is it possible to set the hibernate to save -1 instead of 1 as true value for a Boolean field in the database? 是否可以将hibernate设置为保存-1而不是1作为数据库中布尔字段的真值? I need -1 to maintain compatibility with other program made in Delphi. 我需要-1来保持与Delphi中的其他程序的兼容性。

@Type(type="com.sample.type.CustomClass")
@Column(name = "TEST_FLAG")
private boolean testFlag;

The @Type annotation needs a full path to the class that implements the userType interface; @Type注释需要实现userType接口的类的完整路径; this is the factory for producing the target type of the mapped column 这是用于生成映射列的目标类型的工厂

CustomClas.java which implements UserType interface provided by hibernate CustomClas.java实现了hibernate提供的UserType接口

UserType implementation provides developer to hook the custom logic/implementation and acts as a adapter between relational database and your class property. UserType实现使开发人员可以挂钩自定义逻辑/实现,并充当关系数据库和类属性之间的适配器。

You need to write the conversion logic in following methods. 您需要使用以下方法编写转换逻辑。

  • nullSafeGet() : Creates the custom object from the data returned by the result set nullSafeGet() :根据结果集返回的数据创建自定义对象
  • nullSafeSet() : Converts custom object into value which needs to be passed to prepared statement nullSafeSet() :将自定义对象转换为需要传递给nullSafeSet()准备语句的值

Check the API for detail on the hibernate site. 检查API以获取有关hibernate站点的详细信息。

Why not use Integer instead of Boolean? 为什么不使用Integer而不是Boolean?

Integer value;
public void setValue(Boolean b) {
    this.value = (b != null ? (b ? -1 : 0) : null);
}
public Boolean getValue() {
    return (this.value != null ? (this.value == -1 ? true : false) : null);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM