溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Hibernate Formula有什么作用

發(fā)布時間:2021-12-05 16:55:56 來源:億速云 閱讀:246 作者:小新 欄目:編程語言

這篇文章給大家分享的是有關(guān)Hibernate Formula有什么作用的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

1.Hibernate Formula作用

引用Hibernate annotations技術(shù)文檔中的解釋可以很好的說明@Formula的作用,但它確實沒有說清楚怎么使用,并且給出的示例是用不了的,這讓我浪費了好幾個鐘頭的時間!

Hibernate Formula作用就是說白了就是用一個查詢語句動態(tài)的生成一個類的屬性,比如java eye登陸之后 收件箱顯示有幾封未讀郵件的數(shù)字,就是一條select count(*)...構(gòu)成的虛擬列,而不是存儲在數(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都應(yīng)該是支持的。

感謝各位的閱讀!關(guān)于“Hibernate Formula有什么作用”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI