配置休眠(使用 JPA)以存储类型 Boolean 而不是 0/1 的 Y/N [英] Configure hibernate (using JPA) to store Y/N for type Boolean instead of 0/1

查看:39
本文介绍了配置休眠(使用 JPA)以存储类型 Boolean 而不是 0/1 的 Y/N的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can I setup JPA/hibernate to persist Boolean types as Y/N? In the database (the column is defined as varchar2(1). It currently stores them as 0/1. The database is Oracle.

推荐答案

我想出的唯一方法是为我的班级设置两个属性.一个作为未包含在映射中的编程 API 的布尔值.它的 getter 和 setter 引用了一个私有的 char 变量,它是 Y/N.然后我有另一个受保护的属性,它包含在休眠映射中,它的 getter 和 setter 直接引用私有 char 变量.

The only way I've figured out how to do this is to have two properties for my class. One as the boolean for the programming API which is not included in the mapping. It's getter and setter reference a private char variable which is Y/N. I then have another protected property which is included in the hibernate mapping and it's getters and setters reference the private char variable directly.

正如已经指出的那样,还有其他直接内置到 Hibernate 中的解决方案.我要留下这个答案,因为它可以在您使用与内置选项不兼容的遗留字段的情况下工作.最重要的是,这种方法没有严重的负面影响.

As has been pointed out there are other solutions that are directly built into Hibernate. I'm leaving this answer because it can work in situations where you're working with a legacy field that doesn't play nice with the built in options. On top of that there are no serious negative consequences to this approach.

这篇关于配置休眠(使用 JPA)以存储类型 Boolean 而不是 0/1 的 Y/N的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆