Hibernate 註解案例

來源:互聯網
上載者:User

一、介紹

在以前弄hibernate的時候,每建立一個資料庫表都要在entity下 建立對應的*.hbm.xml 設定檔,當資料庫表很多之後就會嚴重影響開發效率和維護成本,所以 就有了註解,原理在於 省去之前繁瑣的設定檔,取代的是在實體類中加上以“@”開頭的標記,命之為 註解,待會一一解釋其中含義:

二、準備

jar 檔案:

hibernate-core-3.3.2.GA.jar

ejb3-persistence-1.0.2.GA.jar

hibernate-annotations-3.4.0.GA.jar

hibernate-commons-annotations-3.1.0.GA.jar

javassist-3.11.0.GA.jar

(缺一不可)

三、實現

package com.hibernate.entity;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;


/**
 * 電影詳情類
 * @author Administrator
 *
 */
@Entity  //標記為 資料庫表
@Table(name = "tb_film_info")  // 指定該實體類映射的對應的資料庫表名
public class FilmInfo implements Serializable{

 private static final long serialVersionUID = 1L;

 private Long fiId;
 private String fiName;
 private String fiActor;
 private String fiDirector;
 private Double fiTicketPrice;
 
 private FilmType filmType;
 
 @Id  //主鍵id
 @SequenceGenerator(name = "generator", sequenceName = "seq_film_info" )  //指定序列
 @GeneratedValue(generator = "generator", strategy = GenerationType.SEQUENCE) //指定主鍵建置原則 
 @Column(name = "fi_id")  //因為資料表中的結構都是以“_”分割,然後 hibernate預設識別方式沒有以任何方式分割,所以每個列都要在getter()方法標記
 public Long getFiId() {
  return fiId;
 }
 public void setFiId(Long fiId) {
  this.fiId = fiId;
 }
 @Column(name = "fi_name")
 public String getFiName() {
  return fiName;
 }
 public void setFiName(String fiName) {
  this.fiName = fiName;
 }
 @Column(name = "fi_actor")
 public String getFiActor() {
  return fiActor;
 }
 public void setFiActor(String fiActor) {
  this.fiActor = fiActor;
 }
 @Column(name = "fi_director")
 public String getFiDirector() {
  return fiDirector;
 }
 public void setFiDirector(String fiDirector) {
  this.fiDirector = fiDirector;
 }
 @Column(name = "fi_ticket_price")
 public Double getFiTicketPrice() {
  return fiTicketPrice;
 }
 public void setFiTicketPrice(Double fiTicketPrice) {
  this.fiTicketPrice = fiTicketPrice;
 }
 @ManyToOne(fetch = FetchType.LAZY)  //多對一關聯關係,採用@ManyToOne標記,指定下載入方式
 @JoinColumn(name = "fi_film_type_id")  //指定下 關聯的外鍵
 public FilmType getFilmType() {
  return filmType;
 }
 public void setFilmType(FilmType filmType) {
  this.filmType = filmType;
 }
}

 

package com.hibernate.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
 * 電影類型類
 * @author Administrator
 *
 */
@Entity//標記為 資料庫表
@Table(name = "tb_film_type")// 指定該實體類映射的對應的資料庫表名 
public class FilmType implements Serializable{

 private static final long serialVersionUID = 1L;
 
 private Long ftId;
 private String ftName;

 private List<FilmInfo> filmInfos = new ArrayList<FilmInfo>();
 
 @Id
 @SequenceGenerator(name = "generator", sequenceName = "seq_film_type")
 @GeneratedValue(generator = "generator", strategy = GenerationType.SEQUENCE)
 @Column(name = "ft_id")
 public Long getFtId() {
  return ftId;
 }
 public void setFtId(Long ftId) {
  this.ftId = ftId;
 }
 @Column(name = "ft_name")
 public String getFtName() {
  return ftName;
 }
 public void setFtName(String ftName) {
  this.ftName = ftName;
 }
 
 @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)  //一對多關聯關係,指定級聯方式
 @JoinColumn(name = "fi_film_type_id")  // 這裡指定 tb_Film_info 表的主鍵id,和引用tb_film_type表的外鍵都可以。
 public List<FilmInfo> getFilmInfos() {
  return filmInfos;
 }
 public void setFilmInfos(List<FilmInfo> filmInfos) {
  this.filmInfos = filmInfos;
 }
 
}

 

基本就是這個樣子,然後以前使用hibernate對應檔配置獲得SessionFactory 的方式是Configuration 的執行個體,但是使用hibernate註解之後要使用AnnotationConfiguration 的執行個體,如下:

 //第一次載入該類才會執行,僅僅一次
 static{
 /*  當使用的是entity.hbm.xml 對應檔時,就使用下面的方式讀取設定檔
   Configuration configuration = new Configuration().configure();
  sessionFactory = configuration.buildSessionFactory();
 */
 /*  如果實體類使用的是註解完成的,就使用下面的方式完成讀取配置資訊*/
  AnnotationConfiguration annotationConfiguration = new AnnotationConfiguration().configure();
  sessionFactory = annotationConfiguration.buildSessionFactory();
 }

其他的實現方式 和以前是一樣的。

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.