@Formula
@Formula 计算临时属性。
相当于可以关联查询字段,然后放在实体中当做属性使用。
任务:在User实体层,增加一个额外的属性,来获取Test表中的name字段。
1 表结构
User表

Test表

2 User实体层(省略了部分字段。)
@Entity
@Table(name = "user", catalog = "testdb")
@DynamicUpdate
public class User {
private Integer id;
private String uuid; private String test; @Id
@GeneratedValue
@Column(name = "id")
public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} @Column(name = "uuid")
public String getUuid() {
return uuid;
} public void setUuid(String uuid) {
this.uuid = uuid;
} @Formula("(select t.name from test as t where t.id=id)")
public String getTest() {
return test;
} public void setTest(String test) {
this.test = test;
}
}
我们可以看到,在getTest上,增加了@Formula注解。查询表时,就会关联Test表的name到属性上。
注意:
首先 表增加别名。
其次 注意sql两段的小括号()
最后 t.id=id 的最后那个id,对应属性上的@Colun(value = "id")
3 通过jpa创建一个dao
@Repository
public interface UserDao extends JpaRepository<User,Integer> {
}
4 测试
@Autowired
private UserDao userDao; @Test
public void contextLoads() {
System.out.println(userDao.findById(2).get().getTest());
}
@Formula的更多相关文章
- redmine computed custom field formula tips
项目中要用到Computed custom field插件,公式不知道怎么写,查了些资料,记录在这里. 1.http://apidock.com/ruby/Time/strftime 查看ruby的字 ...
- salesforce 零基础开发入门学习(十五)salesforce中formula的使用(不含Date/Time)
本文参考官方的formula介绍PDF:https://resources.docs.salesforce.com/200/latest/en-us/sfdc/pdf/salesforce_usefu ...
- Hibernate @Formula 注解方式
1.Formula的作用 Formula的作用就是用一个查询语句动态的生成一个类的属性 就是一条select count(*)...构成的虚拟列,而不是存储在数据库里的一个字段.用比较标准的说法就是: ...
- Hibernate @Formula
在使用Hibernate时经常会遇到实体类某个字段存的是code值而非我们最终想要的中文具体显示的值, 如果使用Hibernate的一对一关联这种,一个属性还好说,但是如果一个实体类里有多个字段都是需 ...
- Hibernate设置派生属性(formula)
一.Customer中包含的字段: private static final long serialVersionUID = 1L; private Integer id; private ...
- JavaPersistenceWithHibernate第二版笔记-第五章-Mapping value types-001Mapping basic properties(@Basic、@Access、access="noop"、@Formula、@ColumnTransformer、@Generated、 @ColumnDefaul、@Temporal、@Enumerated)
一.简介 在JPA中,默认所有属性都会persist,属性要属于以下3种情况,Hibernate在启动时会报错 1.java基本类型或包装类 2.有注解 @Embedded 3.有实现java.io. ...
- Calculate the formula
Problem Description You just need to calculate the sum of the formula: 1^2+3^2+5^2+……+ n ^2. Input ...
- Hibernate逍遥游记-第3章对象-关系映射基础-access="field"、dynamic-insert、dynamic-update、formula、update=false
1. package mypack; import java.util.*; public class Monkey{ private Long id; private String firstnam ...
- Codeforces Gym 100610 Problem E. Explicit Formula 水题
Problem E. Explicit Formula Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
- oracle report err:REP-2103 PL/SQL formula returned invalid value or no value
好多年没用report builder做报表了,最近又开始接触这玩意了,今天修改一个report,调试半天没发现逻辑问题,一直报REP-2103: Column 'CF_report_line_po' ...
随机推荐
- JMeter轻松实现大数据量AI图像识别接口测试
****************************************************************************** 本文主要介绍利用Jmeter进行AI图像识 ...
- 【转载】Spring 源码分析之 bean 实例化原理
本次主要想写spring bean的实例化相关的内容.创建spring bean 实例是spring bean 生命周期的第一阶段.bean 的生命周期主要有如下几个步骤: 创建bean的实例 给实例 ...
- velocity 相关
Apache Velocity 是一个基于java的模板引擎(template engine) 应用场景1.Web 应用:开发者在不使用 JSP 的情况下,可以用 Velocity 让 HTML 具有 ...
- css垂直居中设置
转载自大佬的文章:纯css实现垂直居中的几种方法(https://www.cnblogs.com/hutuzhu/p/4450850.html) 总结得很棒,有好几种方法自己没用过的,拷贝过来给自己看 ...
- 笔记-Linux安装中文版man
使用环境为Ubuntu,安装中文版man,同时保留了英文原版,步骤如下: 第一种方法 sudo apt-get update # 更新你的下载源目录,此步骤可省略. sudo apt-get inst ...
- Stack&Heap的理解
Heap(堆):在英文中有杂乱的堆意思,意译中文为堆:其特点为先进先出. 堆空间分配:一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收,分配方式倒是类似于链表. Stack(栈):在英 ...
- IconFont 图标的3种引用方式
第一步:进入阿里巴巴矢量图网站:http://www.iconfont.cn/ 阿里巴巴矢量图 第二步:搜索你分类的关键字---然后加入购物车,下载到本地,然后解压,会将合并后的字体文件及自动生成 ...
- [转]C#对Excel报表进行操作(读写和基本操作)
//1.添加引用-〉com-〉microsoft excel 11.0 //2.若出现错误:命名空间“Microsoft.Office”中不存在类型或命名空间名称“Interop”(是缺少程序集引用吗 ...
- 带各位深入理解java1.8之supplier
supplier也是是用来创建对象的,但是不同于传统的创建对象语法:new,看下面代码:public class TestSupplier { private int age; (www.0831jl ...
- magento 跳转
Magento: Redirect functions 原文:http://blog.chapagain.com.np/magento-redirect-functions/ The redirect ...