對Hibernate Formula作用的全析
Hibernate Formula作用很強大,下面我們具體來講述Hibernate Formula的應用,希望對大家有很大的幫助。
閱讀對象:
1.已經在使用Hibernate JPA完成持久化映射的朋友。
2.在網上搜索Formula并通通搜到轉載oralce一篇技術文章或hibernate annotations技術文檔的朋友。
3.發(fā)現(xiàn)@Formula不能使用并想Ctrl+Delete hibernate jar包,然后砸爛顯示器的朋友。
本文將說明如何使用@Formula,并且說明在哪幾種情況下@Formula會失靈。
1.Hibernate Formula作用
引用Hibernate annotations技術文檔中的解釋可以很好的說明@Formula的作用,但它確實沒有說清楚怎么使用,并且給出的示例是用不了的,這讓我浪費了好幾個鐘頭的時間!
Hibernate Formula作用就是說白了就是用一個查詢語句動態(tài)的生成一個類的屬性,比如java eye登陸之后 收件箱顯示有幾封未讀郵件的數(shù)字,就是一條select count(*)...構成的虛擬列,而不是存儲在數(shù)據(jù)庫里的一個字段。用比較標準的說法就是:有時候,你想讓數(shù)據(jù)庫,而非JVM,來替你完成一些計算,也可能想創(chuàng)建某種虛擬列,你可以使用sql片段,而不是將屬性映射(物理)列。這種屬性是只讀的(屬性值由公式求得).Formula甚至可以包含sql子查詢
Formula真的這么強大嗎?確實,它很好很強大,節(jié)省了不少代碼!
2.使用Formula
- package aa;
- import static javax.persistence.GenerationType.IDENTITY;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.Table;
- import org.hibernate.annotations.Formula;
- /**
- * 注解必須是在屬性上的,如果有任何一個注解在方法上,那么@Formula將失效
- * @author 昆明蜂鳥軟件
- * @version 0.1.0 2008-7-15 下午06:09:38
- */
- @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;
- }
- }
- package aa;
- import static javax.persistence.GenerationType.IDENTITY;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.Table;
- import org.hibernate.annotations.Formula;
- /**
- * 注解必須是在屬性上的,如果有任何一個注解在方法上,那么@Formula將失效
- * @author 昆明蜂鳥軟件
- * @version 0.1.0 2008-7-15 下午06:09:38
- */
- @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;
- }
- }
數(shù)據(jù)庫表:Sql代碼
- 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;
細節(jié)1.使用@Formula 你的注解必須是在屬性上,如果有一個注解在方法上,那么@Formula將失效。這個我是做過實驗的,比如把以上的java文件改為:
Java代碼
- package aa;
- import static javax.persistence.GenerationType.IDENTITY;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.Table;
- import org.hibernate.annotations.Formula;
- /**
- * 注解必須是在屬性上的,如果有任何一個注解在方法上,那么@Formula將失效
- * @author 昆明蜂鳥軟件
- * @version 0.1.0 2008-7-15 下午06:09:38
- */
- @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;
- }
- }
- package aa;
- import static javax.persistence.GenerationType.IDENTITY;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.Table;
- import org.hibernate.annotations.Formula;
- /**
- * 注解必須是在屬性上的,如果有任何一個注解在方法上,那么@Formula將失效
- * @author 昆明蜂鳥軟件
- * @version 0.1.0 2008-7-15 下午06:09:38
- */
- @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就不可以運行?。?!我前邊就是被Hibernate官方的文檔給搞暈了。
細節(jié)2.既然@Formula 是一個虛擬列,那么數(shù)據(jù)庫中不需要建這一列,同樣可以,如果有個列存在,hibernate也會將 其忽略。以上示例中的user就沒有count列。
細節(jié)3.sql語句必須寫在()中,這個以前也有人說過。
細節(jié)4.如果有where子查詢,那么表需要用別名,比如 select COUNT(*) from user where id=1 是錯的
而select COUNT(*) from user u where u.id=1是正確的
細節(jié)5.只要是你在數(shù)據(jù)庫的sql控制臺執(zhí)行過的語句,并且使用了表別名,那么@Formula都應該是支持的。
確實@Formula是一個很常用且好用的東西!希望這篇文章能幫助你~~
【編輯推薦】