Hibernate @Formula (公式) 使用教程

Stella981
• 阅读 448

使用方法

private Long usersSize;

       @Formula("(id * 8888888)")
    @JsonProperty
    public Long getUsersSize(){
        return usersSize;
    }
    
    
    public void setUsersSize(Long usersSize) {
        this.usersSize = usersSize;
    }
略。。。。

注意 必须使用()来包裹sql片段,hibernate不会在数据库创建userSize对应的列

查看下生成的hibernate 生成的 sql,很明显是在数据库做了运算

select agent0_.id as id1_, agent0_.acl_code as acl2_1_, agent0_.acl_type as acl3_1_, agent0_.created_by 
as created4_1_, agent0_.created_dt as created5_1_, agent0_.updated_by as updated6_1_, agent0_.updated_dt 
as updated7_1_, agent0_.version as version1_, agent0_.agent_addres as agent9_1_, agent0_.agent_city 
as agent10_1_, agent0_.agent_county as agent11_1_, agent0_.agent_name as agent12_1_, agent0_.agent_province 
as agent13_1_, agent0_.agent_scale as agent14_1_, agent0_.zip_code as zip15_1_, agent0_.biz_lic_reg_no 
as biz16_1_, agent0_.join_date as join17_1_, agent0_.oper_org_type as oper18_1_, agent0_.org_code_cert_no 
as org19_1_, (agent0_.id * 8888888) as formula0_ from agent_agent agent0_ order by agent0_.created_dt 
desc limit 15

甚至你可以这样玩:

查询该代理下用户总数

@Formula("(select count(*) from tbl_auth_user o where o.agent_id = id)")

id 代表当前表的id

@Formula("(select count(*) from tbl_auth_user o where o.agent_id = id)")
    @JsonProperty
    public Long getUsersSize(){
        return usersSize;
    }

hibernate生成的 sql

select agent0_.id as id1_, agent0_.acl_code as acl2_1_, agent0_.acl_type as acl3_1_, agent0_.created_by 
as created4_1_, agent0_.created_dt as created5_1_, agent0_.updated_by as updated6_1_, agent0_.updated_dt 
as updated7_1_, agent0_.version as version1_, agent0_.agent_addres as agent9_1_, agent0_.agent_city 
as agent10_1_, agent0_.agent_county as agent11_1_, agent0_.agent_name as agent12_1_, agent0_.agent_province 
as agent13_1_, agent0_.agent_scale as agent14_1_, agent0_.zip_code as zip15_1_, agent0_.biz_lic_reg_no 
as biz16_1_, agent0_.join_date as join17_1_, agent0_.oper_org_type as oper18_1_, agent0_.org_code_cert_no 
as org19_1_, (select count(*) from tbl_auth_user o where o.agent_id = agent0_.id) as formula0_ 
from agent_agent agent0_ order by agent0_.created_dt desc limit 15

更多用例

@Formula("(select min(o.creation_date) from Orders o where o.customer_id = id)")
private Date firstOrderDate;

@Formula("(select max(o.creation_date) from Orders o where o.customer_id = id)")
private Date lastOrderDate;

@Formula("(select coalesce(extract ('day' from age(max(o.creation_date))), 9999) from Orders o where o.customer_id = id)")
private int daysSinceLastOrder;

 @Formula (公式)虽好,但是不要滥用哦

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Wesley13 Wesley13
2年前
java将前端的json数组字符串转换为列表
记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Souleigh ✨ Souleigh ✨
3年前
前端性能优化 - 雅虎军规
无论是在工作中,还是在面试中,web前端性能的优化都是很重要的,那么我们进行优化需要从哪些方面入手呢?可以遵循雅虎的前端优化35条军规,这样对于优化有一个比较清晰的方向.35条军规1.尽量减少HTTP请求个数——须权衡2.使用CDN(内容分发网络)3.为文件头指定Expires或CacheControl,使内容具有缓存性。4.避免空的
Stella981 Stella981
2年前
Mac OS 安装 Elasticsearch 使用 analysis
系统:MacOSXElasticsearch版本:5.6.4Analysisik5.6.4更新brew$brewupdate修改formula$cd/usr/local/Homebrew/Library/Taps/homebrew/homebrewcore/Formula
Wesley13 Wesley13
2年前
Java爬虫之JSoup使用教程
title:Java爬虫之JSoup使用教程date:201812248:00:000800update:201812248:00:000800author:mecover:https://imgblog.csdnimg.cn/20181224144920712(https://www.oschin
Stella981 Stella981
2年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Wesley13 Wesley13
2年前
35岁是技术人的天花板吗?
35岁是技术人的天花板吗?我非常不认同“35岁现象”,人类没有那么脆弱,人类的智力不会说是35岁之后就停止发展,更不是说35岁之后就没有机会了。马云35岁还在教书,任正非35岁还在工厂上班。为什么技术人员到35岁就应该退役了呢?所以35岁根本就不是一个问题,我今年已经37岁了,我发现我才刚刚找到自己的节奏,刚刚上路。
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这