Hibernate @Formula详解

来源:互联网 发布:yum强制重新安装 编辑:程序博客网 时间:2024/06/16 00:15

阅读对象:

  1.已经在使用Hibernate JPA完成持久化映射的朋友。

  2.在网上搜索Formula并通通搜到转载oralce一篇技术文章或hibernate annotations技术文档的朋友。

  3.发现@Formula不能使用并想Ctrl+Delete hibernate jar包,然后砸烂显示器的朋友。

 

文章内容

  本文将说明如何使用@Formula,并且说明在哪几种情况下@Formula会失灵。

 

1.Formula的作用

  引用hibernate annotations技术文档中的解释可以很好的说明@Formula的作用,但它确实没有说清楚怎么使用,并且给出的示例是用不了的,这让我浪费了好几个钟头的时间!

   Formula的作用就是说白了就是用一个查询语句动态的生成一个类的属性,比如java eye登陆之后 收件箱显示有几封未读邮件的数字,就是一条select count(*)...构成的虚拟列,而不是存储在数据库里的一个字段。用比较标准的说法就是:有时候,你想让数据库,而非JVM,来替你完成一些计算,也可能想创建某种虚拟列,你可以使用sql片段,而不是将属性映射(物理)列。这种属性是只读的(属性值由公式求得).Formula甚至可以包含sql子查询

   Formula真的这么强大吗?确实,它很好很强大,节省了不少代码!

 

2.使用Formula

package aa;import static javax.persistence.GenerationType.IDENTITY;import javax.persistence.*;import org.hibernate.annotations.Formula;@Entity@Table(name = "user", catalog = "test")public class User {@Id@GeneratedValue(strategy = IDENTITY)private int id;@Formula("(select COUNT(*) from user)")private int count;public int getId() {return id;}public void setId(int id) {this.id = id;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}}

数据库表:

CREATE TABLE  `test`.`user` (  `id` int(10) unsigned NOT NULL auto_increment,  PRIMARY KEY  USING BTREE (`id`)) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
细节1.你的注解要么全部在相应的属性之上,要么全部在相应的方法之上,如果混合注解,则会报错,比如把以上的java文件改为:

package aa;import static javax.persistence.GenerationType.IDENTITY;import javax.persistence.*;import org.hibernate.annotations.Formula;@Entity@Table(name = "user", catalog = "test")public class User {private int id;@Formula("(select COUNT(*) from user)")private int count;@Id@GeneratedValue(strategy = IDENTITY)public int getId() {return id;}public void setId(int id) {this.id = id;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}}

这样@Formula就不可以运行!!!

细节2.既然@Formula 是一个虚拟列,那么数据库中不需要建这一列,同样可以,如果有个列存在,hibernate也会将   其忽略。以上示例中的user就没有count列。

细节3.sql语句必须写在()中,这个以前也有人说过。

细节4.如果有where子查询,那么表需要用别名,比如 select COUNT(*) from user where id=1 是错的

而select COUNT(*) from user u where u.id=1是正确的

细节5.只要是你在数据库的sql控制台执行过的语句,并且使用了表别名,那么@Formula都应该是支持的。

 

 确实@Formula是一个很常用且好用的东西!希望这篇文章能帮助你~~






0 0
原创粉丝点击