天天看點

hibernate 3 映射學習

關鍵字: hibernate,annotation

基于注解的hibernate主鍵設定:@Id.

那麼它的生成規則是什麼呢?是由@GeneratedValue來規定的。 

注解映射必須滿足兩大條件:Hibernate3.2以上版本和JSEE 5。

@Entity 類注釋,所有要持久化的類都要有

Java代碼

hibernate 3 映射學習
  1. @Entity  
  2. public class Org  implements java.io.Serializable {   
  3. }  
@Entity public class Org  implements java.io.Serializable { }           

@Id 主鍵

Java代碼

hibernate 3 映射學習
  1. @Id  
  2.      @GeneratedValue  
  3.      private String orgId;   
  4.      private String orgName;  
@Id      @GeneratedValue      private String orgId;      private String orgName;           

@Column(name="...") 該屬性對應表中的字段是什麼,沒有name表示一樣

@Table 對象與表映射

@UniqueConstraint 唯一限制

@Version 方法和字段級,樂觀鎖用法,傳回數字和timestamp,數字為首選

@Transient 暫态屬性,表示不需要處理

@Basic 最基本的注釋。 有兩個屬性:fetch是否延遲加載,optional是否允許null

@Enumerated 枚舉類型

@Temporal 日期轉換。預設轉換Timestamp

@Lob 通常與@Basic同時使用,提高通路速度。

@Embeddable 類級,表可嵌入的

@Embedded 方法字段級,表被嵌入的對象和@Embeddable一起使用

@AttributeOverrides 屬性重寫

@AttributeOverride 屬性重寫的内容和@AttributeOverrides一起嵌套使用

@SecondaryTables 多個表格映射

@SecondaryTable 定義輔助表格映射和@SecondaryTables一起嵌套使用

@GeneratedValue 辨別符生成政策,預設Auto

表與表關系映射

@OneToOne:一對一映射。它包含五個屬性:

targetEntity:關聯的目标類

Cascade:持久化時的級聯操作,預設沒有

fetch:擷取對象的方式,預設EAGER

Optional:目标對象是否允許為null,預設允許

mappedBy:定義雙向關聯中的從屬類。

單向:

@JoinColumn:定義外鍵(主表會多一字段,做外鍵)

@OneToMany:一對多映射; @ManyToOne:多對一映射

單向一對多:

@OneToMany(cascade=CascadeType.ALL)

@JoinColumn(name="book_oid")

單向多對一:

@ManyToOne(cascade=CascadeType.ALL)

@JoinColumn(name="author_oid")

關聯表格一對多:

@OneToMany(cascade=CascadeType.ALL)

@JoinTable(joinColumn={@JoinColumn(name="BOOK_OBJECT_OID")},inverseJoinColumns={@JoinColumn(name="AUTHER_OBJECT_OID")})

雙向一對多或多對一:

不需要多一張表,隻是使用mappedBy:使用在One一方,值為One方類名表示Many的從屬類。

Java代碼

hibernate 3 映射學習
  1. @Entity  
  2. public class Org  implements java.io.Serializable {   
  3.     // Fields       
  4.     @Id  
  5.     @GeneratedValue  
  6.      private String orgId;   
  7.      private String orgName;   
  8.      @OneToMany(mappedBy = "org")   
  9.      private List<Department> departments;   
  10.     // Constructors   
  11. ...   
  12.     // Property accessors   
  13. ...   
  14. }  
@Entity public class Org  implements java.io.Serializable {       // Fields     	@Id 	@GeneratedValue      private String orgId;      private String orgName;      @OneToMany(mappedBy = "org")      private List<Department> departments;      // Constructors ...     // Property accessors ... }           

Java代碼

hibernate 3 映射學習
  1. @Entity  
  2. public class Department  implements java.io.Serializable {   
  3.     // Fields       
  4.     @Id  
  5.     @GeneratedValue  
  6.      private String id;   
  7.      private String name;   
  8.      @ManyToOne(fetch=FetchType.EAGER)   
  9.      @JoinColumn(name="org_orgId")   
  10.      private Org org;   
  11.      @OneToMany(mappedBy = "department")   
  12.      private List<Employee> employees;   
  13.     // Constructors   
  14.     public List<Employee> getEmployees() {   
  15.         return employees;   
  16.     }   
  17.     public void setEmployees(List<Employee> employees) {   
  18.         this.employees = employees;   
  19.     }   
  20.     public Org getOrg() {   
  21.         return org;   
  22.     }   
  23.     public void setOrg(Org org) {   
  24.         this.org = org;   
  25.     }   
  26.              .   
  27.              .   
  28.              .   
  29. }  
@Entity public class Department  implements java.io.Serializable {       // Fields     	@Id 	@GeneratedValue      private String id;      private String name;      @ManyToOne(fetch=FetchType.EAGER)      @JoinColumn(name="org_orgId")      private Org org;      @OneToMany(mappedBy = "department")      private List<Employee> employees;      // Constructors      public List<Employee> getEmployees() { 		return employees; 	}  	public void setEmployees(List<Employee> employees) { 		this.employees = employees; 	}  	public Org getOrg() { 		return org; 	}  	public void setOrg(Org org) { 		this.org = org; 	}  	/** default constructor */              .              .              . 	 }           

Java代碼

hibernate 3 映射學習
  1. @Entity  
  2. public class Employee  implements java.io.Serializable {   
  3.     // Fields       
  4.     @Id  
  5.     @GeneratedValue  
  6.      private String employeeId;   
  7.      private String employeeName;   
  8.      private String passWord;   
  9.      private Integer age;   
  10.      private Integer sex;   
  11.      @ManyToOne(fetch=FetchType.EAGER)   
  12.      @JoinColumn(name="department_id")   
  13.      private Department department;   
  14.     public Department getDepartment() {   
  15.         return department;   
  16.     }   
  17.     public void setDepartment(Department department) {   
  18.         this.department = department;   
  19.     }   
  20.     ...   
  21.     // Property accessors   
  22.     ...   
  23. }  
@Entity public class Employee  implements java.io.Serializable {       // Fields     	@Id 	@GeneratedValue      private String employeeId;      private String employeeName;      private String passWord;      private Integer age;      private Integer sex;      @ManyToOne(fetch=FetchType.EAGER)      @JoinColumn(name="department_id")      private Department department;            public Department getDepartment() { 		return department; 	}  	public void setDepartment(Department department) { 		this.department = department; 	}  	/** default constructor */     ...     // Property accessors     ... }           

雙向多對多:@ManyToMany.單向多對多這裡不在贅述(沒有太多實際意義)

這個比較簡單,看下代碼就明白了:

Java代碼

hibernate 3 映射學習
  1. @Entity  
  2. public class Book  implements java.io.Serializable {   
  3.     @Id  
  4.     private int id;   
  5.     private String name;   
  6.     private float money;   
  7.     @ManyToMany(cascade = CascadeType.ALL)   
  8.     private List<Author> authors;   
  9.     public List<Author> getAuthors() {   
  10.         return authors;   
  11.     }   
  12.     public void setAuthors(List<Author> authors) {   
  13.         this.authors = authors;   
  14.     }   
  15.          ...   
  16. }  
@Entity public class Book  implements java.io.Serializable { 	@Id 	private int id; 	private String name; 	private float money; 	@ManyToMany(cascade = CascadeType.ALL) 	private List<Author> authors; 	  	public List<Author> getAuthors() { 		return authors; 	} 	public void setAuthors(List<Author> authors) { 		this.authors = authors; 	} 	          ... }           

Java代碼

hibernate 3 映射學習
  1. @Entity  
  2. public class Author  implements java.io.Serializable {   
  3.     @Id  
  4.     private int id;   
  5.     private String name;   
  6.     private int age;   
  7.     @ManyToMany(mappedBy="authors")   
  8.     private List<Book> books;   
  9.     public List<Book> getBooks() {   
  10.         return books;   
  11.     }   
  12.     public void setBooks(List<Book> books) {   
  13.         this.books = books;   
  14.     }   
  15.          ...   
  16. }  
@Entity public class Author  implements java.io.Serializable { 	@Id 	private int id; 	private String name; 	private int age; 	@ManyToMany(mappedBy="authors") 	private List<Book> books; 	  	public List<Book> getBooks() { 		return books; 	}  	public void setBooks(List<Book> books) { 		this.books = books; 	}           ... }           

總算弄完了,不過還是一個皮毛!

需要注意的是:注釋最好加在屬性上,不要加在get方法上,那樣做有時候就會出錯。比如:@ManyToMany的時候就會報錯!