java - Conditionally assigning a table column to a property in spring
问题描述
I am new to spring and was wondering if there was a way to conditionally map a property. For example, if column a has a value of 0, use column b as the value, else default to a.
I was thinking something along the lines of
@Column(name="ColumnA" > 0 ? "ColumnA" : "ColumnB")
private Integer myValue;
If anybody could help steer me in the right direction, that would be great!
解决方案
当您使用 Spring 时,您可能正在导入 Hibernate 实现,因此这是@Formula
注释(文档)的一个很好的用例。
定义一个公式(派生值),它是一个 SQL 片段,在大多数情况下充当 @Column 替代项。
@Formula
注释接受 native ,因此对于这种SQL
逻辑,您应该使用aSWITCH
或SQL
function 。COALESCE
例子:
@Formula("case when ColumnA > 0 then ColumnA else ColumnB end")
private Integer myValue;
推荐阅读
- vba - 无法填写某些日期输入来执行自定义搜索
- angular - 无法使用打字稿和角度为 Jasmine 添加自定义匹配器
- uml - 当两个类关联时,一个类知道另一个类的属性吗?
- r - 通过在单独的 docker 容器中运行的 R 实例在本地保存 R 模型?
- javascript - 如何在javaScript中获得多个键盘输入
- javascript - 单击 vue js 子元素更改页面上的内容
- regex - 使用正则表达式去除电子邮件
- python - Pymongo 包无法安装
- r - 有没有办法在单个情节下绘制 2 个或更多 acfs
- android - 当我的用户访问我的博客时,如何将他们重定向到特定标签?