Hibernate @Formula (公式) 使用教程

使用方法

1private Long usersSize; 2 3       @Formula("(id * 8888888)") 4 @JsonProperty 5 public Long getUsersSize(){ 6 return usersSize; 7 } 8 9 10 public void setUsersSize(Long usersSize) { 11 this.usersSize = usersSize; 12 } 13略。。。。

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

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

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

甚至你可以这样玩:

查询该代理下用户总数

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

id 代表当前表的id

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

hibernate生成的 sql

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

更多用例

1@Formula("(select min(o.creation_date) from Orders o where o.customer_id = id)") 2private Date firstOrderDate; 3 4@Formula("(select max(o.creation_date) from Orders o where o.customer_id = id)") 5private Date lastOrderDate; 6 7@Formula("(select coalesce(extract ('day' from age(max(o.creation_date))), 9999) from Orders o where o.customer_id = id)") 8private int daysSinceLastOrder;

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

点赞
收藏

评论区

加载中...

相关推荐

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

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java将前端的json数组字符串转换为列表

记录下在前端通过ajax提交了一个json数组的字符串,在后端如何转换为列表。前端数据转化与请求varcontracts{id:'1',name:'yanggb合同1'},{id:'2',name:'yanggb合同2'},{id:'3',name:'yang

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )