@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的更多相关文章

  1. redmine computed custom field formula tips

    项目中要用到Computed custom field插件,公式不知道怎么写,查了些资料,记录在这里. 1.http://apidock.com/ruby/Time/strftime 查看ruby的字 ...

  2. salesforce 零基础开发入门学习(十五)salesforce中formula的使用(不含Date/Time)

    本文参考官方的formula介绍PDF:https://resources.docs.salesforce.com/200/latest/en-us/sfdc/pdf/salesforce_usefu ...

  3. Hibernate @Formula 注解方式

    1.Formula的作用 Formula的作用就是用一个查询语句动态的生成一个类的属性 就是一条select count(*)...构成的虚拟列,而不是存储在数据库里的一个字段.用比较标准的说法就是: ...

  4. Hibernate @Formula

    在使用Hibernate时经常会遇到实体类某个字段存的是code值而非我们最终想要的中文具体显示的值, 如果使用Hibernate的一对一关联这种,一个属性还好说,但是如果一个实体类里有多个字段都是需 ...

  5. Hibernate设置派生属性(formula)

    一.Customer中包含的字段: private static final long serialVersionUID = 1L;    private Integer id;    private ...

  6. 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. ...

  7. Calculate the formula

    Problem Description You just need to calculate the sum of the formula: 1^2+3^2+5^2+……+ n ^2.   Input ...

  8. 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 ...

  9. Codeforces Gym 100610 Problem E. Explicit Formula 水题

    Problem E. Explicit Formula Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

  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' ...

随机推荐

  1. testNG官方文档翻译-4 运行TestNG

    TestNG可以通过不同的方法触发运行: 命令行 ant Eclipse IntelliJ's IDEA

  2. Spring Boot Restful WebAPI集成 OAuth2

    系统采用前后端分离的架构,采用OAuth2协议是很自然的事情. 下面开始实战,主要依赖以下两个组件: <dependency> <groupId>org.springframe ...

  3. 用Cython加速Python代码

    安装Cython pip install Cython 如何使用 要在我们的笔记本中使用Cython,我们将使用IPython magic命令.Magic命令以百分号开始,并提供一些额外的功能,这些功 ...

  4. (数据科学学习手札59)从抓取数据到生成shp文件并展示

    一.简介 shp格式的文件是地理信息领域最常见的文件格式之一,很好的结合了矢量数据与对应的标量数据,而在Python中我们可以使用pyshp来完成创建shp文件的过程,本文将从如何从高德地图获取矢量信 ...

  5. find out the installed and runing tomcat version in Linux

    To find out the Tomcat version, find this file – version.sh for *nix or version.bat for Windows. Thi ...

  6. Bash 脚本 set 命令教程

    http://www.ruanyifeng.com/blog/2017/11/bash-set.html set命令是 Bash 脚本的重要环节,却常常被忽视,导致脚本的安全性和可维护性出问题.本文介 ...

  7. 数组foreach

    简单使用 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  8. ruby on rails笔记

    一.新建rails项目步骤: 1.生成新项目 rails new demo cd demo vi Gemfile 末尾end前增加   gem 'execjs'   gem 'therubyracer ...

  9. thinkphp 目录安全文件

    为了避免某些服务器开启了目录浏览权限后可以直接在浏览器输入URL地址查看目录,系统默认开启了目录安全文件机制,会在自动生成目录的时候生成空白的index.html文件,当然安全文件的名称可以设置,例如 ...

  10. [Nowcoder] 保护

    题意:... 思路: \(LCA\)乱搞+启发式合并(堆) #include <bits/stdc++.h> using namespace std; const int maxn = 2 ...