首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >配置hibernate (使用JPA)为Boolean类型存储Y/N,而不是0/1

配置hibernate (使用JPA)为Boolean类型存储Y/N,而不是0/1
EN

Stack Overflow用户
提问于 2009-07-20 17:29:23
回答 5查看 120.1K关注 0票数 80

我可以设置JPA/hibernate将Boolean类型持久化为Y/N吗?在数据库中(该列被定义为varchar2(1)。它目前将它们存储为0/1。数据库为Oracle。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2009-07-20 17:33:07

我想知道如何做到这一点的唯一方法是为我的类设置两个属性。一个作为未包含在映射中的编程API的布尔值。它的getter和setter引用一个私有char变量,即Y/N。然后,我有另一个受保护的属性,该属性包含在hibernate映射中,它的getter和setter直接引用私有char变量。

编辑:正如已经指出的那样,还有其他解决方案直接内置到Hibernate中。我之所以留下这个答案,是因为它可以在您使用的遗留字段与内置选项不能很好配合的情况下工作。最重要的是,这种方法没有严重的负面后果。

票数 9
EN

Stack Overflow用户

发布于 2009-07-20 17:42:29

Hibernate有一个内置的"yes_no“类型,它可以做你想做的事情。它映射到数据库中的CHAR(1)列。

基本映射:<property name="some_flag" type="yes_no"/>

注释映射(Hibernate扩展):

@Type(type="yes_no")
public boolean getFlag();
票数 152
EN

Stack Overflow用户

发布于 2014-05-28 20:00:58

我使用了@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<Boolean, String> {

    /**
     * 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<Boolean, String> {

    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;
    }

}
票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1154833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档