我可以设置JPA/hibernate将Boolean
类型持久化为Y/N
吗?在数据库中(该列被定义为varchar2(1)
。它目前将它们存储为0/1
。数据库为Oracle。
发布于 2009-07-20 17:33:07
我想知道如何做到这一点的唯一方法是为我的类设置两个属性。一个作为未包含在映射中的编程API的布尔值。它的getter和setter引用一个私有char变量,即Y/N。然后,我有另一个受保护的属性,该属性包含在hibernate映射中,它的getter和setter直接引用私有char变量。
编辑:正如已经指出的那样,还有其他解决方案直接内置到Hibernate中。我之所以留下这个答案,是因为它可以在您使用的遗留字段与内置选项不能很好配合的情况下工作。最重要的是,这种方法没有严重的负面后果。
发布于 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();
发布于 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;
}
}
https://stackoverflow.com/questions/1154833
复制相似问题