屬性級別注解
添加方式:
寫在屬性字段上面
寫在屬性的get訪問器的上面
@Id
必須,定義了映射到數據庫表的主鍵的屬性,一個實體類可以有一個或者多個屬性被映射為主鍵,可置于主鍵屬性或者getXxx()前,注意:如果有多個屬性定義為主鍵屬性,該實體類必須實現serializable接口
@SequenceGenerator
@GeneratedValue
@GeneratedValue(strategy=GenerationType,generator=""):可選,用于定義主鍵生成策略
strategy表示主鍵生成策略,取值有:
GenerationType.AUTO:根據底層數據庫自動選擇(默認)
GenerationType.INDENTITY:根據數據庫的Identity字段生成
GenerationType.SEQUENCE:使用Sequence來決定主鍵的取值
GenerationType.TABLE:使用指定表來決定主鍵取值,結合@TableGenerator使用
@Column
@Column-可將屬性映射到列,使用該注解來覆蓋默認值,@Column描述了數據庫表中該字段的詳細定義,這對于根據JPA
注解生成數據庫表結構的工具非常有作用。
常用屬性:
name:可選,表示數據庫表中該字段的名稱,默認情形屬性名稱一致
nullable:可選,表示該字段是否允許為null,默認為true
unique:可選,表示該字段是否為唯一標識,默認為false
length:可選,表示該字段的大小,僅對String類型的字段有效,默認值225,主鍵不能使用默認值
insertable:可選,表示在ORM框架執行插入操作時,該字段是否應出現INSERTRT語句中,默認為true
updateable:可選,表示在ORM框架執行更新操作時,該字段是否應該出現在UPDATE語句中,默認為true。對于已經創建
就不可以更改的字段,該屬性非常有用
@Embedded
@Embedded是注釋屬性的,表示該屬性的類是嵌入類。
注意:同時嵌入類也必須標注@Embeddable注解
@EmbeddedId
@EmbeddedId使用嵌入式主鍵類實現復合主鍵
注意:嵌入式主鍵類必須實現Serializable接口、必須有默認的public無參數的構造方法、必須覆蓋equals和hashCode方法
@Lob
@Version
@Basic
@Transient
可選,表示該屬性并非一個到數據庫表的字段的映射,ORM框架將忽略該屬性,如果一個屬性并非數據庫表的字段映射,就
務必將其標示為@Transient,否則ORM框架默認其注解為@Basic
實例:
hibernate.properties配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> < hibernate-configuration > < session-factory > < property name = "hibernate.dialect" >org.hibernate.dialect.MySQLDialect</ property > <!-- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.url">jdbc:mysql:///hibernate_struts_stumanager</property> --> < property name = "hibernate.connection.driver_class" >com.mysql.jdbc.Driver</ property > < property name = "hibernate.connection.url" >jdbc:mysql://localhost:3306/mypage</ property > < property name = "hibernate.connection.username" >root</ property > < property name = "hibernate.connection.password" >root</ property > < property name = "hibernate.show_sql" >false</ property > < property name = "hibernate.hbm2ddl.auto" >create</ property > <!-- <property name="hibernate_current_session_context_class">thread</property> --> < property name = "current_session_context_class" >thread</ property > < mapping class = "com.entity.Students" /> </ session-factory > </ hibernate-configuration > |
實體類Student:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
package com.entity; import java.io.Serializable; import java.util.Date; import javax.persistence.Column; import javax.persistence.Embedded; import javax.persistence.EmbeddedId; import javax.persistence.Entity; //JPA注解 import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; import javax.persistence.Transient; import org.hibernate.annotations.GenericGenerator; /* * 學生實體類 */ //@Entity// //@Entity(name="t_students")//如果不添加名字,則默認與實體類名字相同,如果想要自行設置表明,就需要自己進行添加 @Entity @Table (name= "t_students1" ,schema= "mypage" ) public class Students implements Serializable{ @Id // @GeneratedValue // @GeneratedValue(strategy=GenerationType.AUTO)//與@GeneratedValue結果相同,字符串類型不能指定為AUTO類型 // private int sid; //學號 // @GeneratedValue(generator="sid") // @GenericGenerator(name="sid",strategy="assigned") // @Column(length=11) // private String sid; @EmbeddedId private StudentPK pk; // @Id // // @Column(length=11) private String sname; //姓名 private String gender; //性別 private Date birthday; //出生日期 private String major; //專業 private Address add; @Transient //表示該字段不會被ORM映射到表里的字段 private double salary; //薪水 public Students(){ } public Students(StudentPK pk, String sname, String gender, Date date, String major,Address add, double salary) { // super(); this .pk = pk; this .sname = sname; this .gender = gender; this .birthday = date; this .major = major; this .add = add; this .salary = salary; } public StudentPK getPk() { return pk; } public void setPk(StudentPK pk) { this .pk = pk; } public String getSname() { return sname; } public void setSname(String sname) { this .sname = sname; } public String getGender() { return gender; } public void setGender(String gender) { this .gender = gender; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this .birthday = birthday; } public String getMajor() { return major; } public void setMajor(String major) { this .major = major; } public Address getAdd() { return add; } public void setAdd(Address add) { this .add = add; } public double getSalary() { return salary; } public void setSalary( double salary) { this .salary = salary; } } |
StudentPK實體類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
package com.entity; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Embeddable; @Embeddable public class StudentPK implements Serializable{ /** * */ private static final long serialVersionUID = 1L; @Column (length= 18 ) private String id; //身份證號 @Column (length= 8 ) private String sid; //學號 public StudentPK(){ } public StudentPK(String id, String sid) { this .id = id; this .sid = sid; } public String getId() { return id; } public void setId(String id) { this .id = id; } public String getSid() { return sid; } public void setSid(String sid) { this .sid = sid; } @Override public boolean equals(Object obj) { // TODO Auto-generated method stub return super .equals(obj); } @Override public int hashCode() { // TODO Auto-generated method stub return super .hashCode(); } } |
地址類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package com.entity; import javax.persistence.Embeddable; import javax.persistence.Embedded; // 地址類 @Embeddable //表示是一個嵌入類,這個類的對象在另一個實體類中充當屬性 public class Address { private String postCode; //郵編 private String address; //地址 private String phone; //聯系電話 public Address(){ } public Address(String postCode, String address, String phone) { this .postCode = postCode; this .address = address; this .phone = phone; } public String getPostCode() { return postCode; } public void setPostCode(String postCode) { this .postCode = postCode; } @Embedded public String getAddress() { return address; } public void setAddress(String address) { this .address = address; } public String getPhone() { return phone; } public void setPhone(String phone) { this .phone = phone; } } |
測試類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
package com.entity; import java.util.Date; import java.util.EnumSet; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.hibernate.tool.hbm2ddl.SchemaExport; import org.junit.Test; public class TestStudents { @Test public void testShemaExport(){ //創建hibernate配置對象 Configuration config = new Configuration().configure(); //創建服務注冊對象 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); //生成SessionFactory SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); SchemaExport export = new SchemaExport(config); export.create( true , true ); } @Test public void addStudents(){ //創建hibernate配置對象 Configuration config = new Configuration().configure(); //創建服務注冊對象 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); //生成SessionFactory SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); //創建會話 Session session = sessionFactory.getCurrentSession(); //創建事務 Transaction tx = session.beginTransaction(); Address add = new Address( "700005" , "河南理工大學" , "15039117115" ); StudentPK pk = new StudentPK( "012345678912345678" , "55555555" ); Students s = new Students(pk, "張三豐" , "男" , new Date(), "太極拳" ,add, 7899 ); session.save(s); tx.commit(); // SchemaExport export = new SchemaExport(config); // export.create(true,true); } } |
總結
以上就是本文關于hibernate屬性級別注解實例代碼的全部內容,希望對大家有所幫助。在此也非常希望大家對本站多多支持。
原文鏈接:http://blog.csdn.net/wojiaohuangyu/article/details/51768672