Hibernate注解(一)
Hibernate注解的分类:
(1)类级别的注解:@Entity、@Table、@Embeddable(嵌入类)
(2)属性级别的注解:@Id、@SequenceGenerator、@GeneratedValue、@Column、@Embedded、@EmbeddedId、@Lob、@Version、@Basic、@Transient
(3)映射级别的注解
(一)类级别的注解:
1.1 @Entity:映射实体类————》@Entity(name = "tableName")
name:可选,对应数据中的一个表。若表明与尸体类名相同,则可以省略。使用@Entity时必须指定实体类的主键属性。
1.2 @Table:@Table(name = "",catelog="",schema="")
@Entity配合使用,只能标注在实体的class定义处,表示实体对应的数据库表的信息。
name:可选,映射表的名称,默认表明和实体名称一致,只有在不一致的情况下才需要指定表明。
catalog:可选,便是Catalog名称,默认为Catalog(“”);
schema:可选,表示Schema名称,默认为Schema("")。
1.3 @Embeddable:@Embeddable(name = "",catelog="",schema="")
@Embeddable表示一个非Entity类可以嵌入到另一个Entity类中作为属性而存在的。
例子:整个demo结构如下
代码:
首先配置文件hibernate.cfg.xml:
1 <?xml version='1.0' encoding='utf-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 6 <hibernate-configuration> 7 <session-factory> 8 <property name="connection.username">root</property> 9 <property name="connection.password">mysql</property> 10 <property name="connection.driver_class">com.mysql.jdbc.Driver</property> 11 <property name="connection.url">jdbc:mysql:///hibernate?useUnicode=true&characterEncoding=UTF-8</property> 12 13 <!-- 配置数据的库的前缀 --> 14 <property name="dialect">org.hibernate.dialect.MySQLDialect</property> 15 16 <!-- 17 是否把Hibernate运行时的SQL语句输出到控制台,编码阶段便于测试 18 --> 19 <property name="show_sql">true</property> 20 <!-- 21 输出到控制台的SQL语句是否进行排版,便于阅读。建议设置true 22 --> 23 <property name="format_sql">true</property> 24 <!-- 25 可以到帮助由java代码生成数据库脚本,进而生辰具体的表结构。 26 create|update|create-drop|validate 27 --> 28 <property name="hbm2ddl.auto">create</property> 29 30 <mapping class="com.entity.Student"/> 31 <!-- <mapping class="com.entity.StudentA"/> --> 32 </session-factory> 33 34 </hibernate-configuration>
测试@Entity和@Table注解的相关类:
实体Students:
1 package com.entity; 2 3 import java.util.Date; 4 5 import javax.persistence.Entity; 6 import javax.persistence.Id; 7 import javax.persistence.Table; 8 9 /** 10 * 学生实体类 11 * 12 * @Table(name="",catalog="",schema=""),mysql不支持catalog属性, 13 * 这里的name属性和@Enity的name属性作用是一样的都是给表起一个名字 14 * 15 */ 16 @Entity//(name = "t_students") 17 @Table(name="t_students",schema="hibernate") 18 public class Students { 19 20 21 private int sid;//学号 22 private String sname;//姓名 23 private String gender;//出生日期 24 private Date birthday;//出生日期 25 private String major;//专业 26 private String address;//地址 27 28 public Students() { 29 } 30 31 public Students(int sid, String sname, String gender, Date birthday, String major, String address) { 32 this.sid = sid; 33 this.sname = sname; 34 this.gender = gender; 35 this.birthday = birthday; 36 this.major = major; 37 this.address = address; 38 } 39 40 @Id 41 public int getSid() { 42 return sid; 43 } 44 45 public void setSid(int sid) { 46 this.sid = sid; 47 } 48 49 public String getSname() { 50 return sname; 51 } 52 53 public void setSname(String sname) { 54 this.sname = sname; 55 } 56 57 public String getGender() { 58 return gender; 59 } 60 61 public void setGender(String gender) { 62 this.gender = gender; 63 } 64 65 public Date getBirthday() { 66 return birthday; 67 } 68 69 public void setBirthday(Date birthday) { 70 this.birthday = birthday; 71 } 72 73 public String getMajor() { 74 return major; 75 } 76 77 public void setMajor(String major) { 78 this.major = major; 79 } 80 81 public String getAddress() { 82 return address; 83 } 84 85 public void setAddress(String address) { 86 this.address = address; 87 } 88 89 90 91 92 93 }
测试@Embeddedable注解的相关代码(这里注意hibernate.cfg.xml文件中的<mapping class="com.entity.StudentA">):
实体StudentA:
1 package com.entity; 2 3 import java.util.Date; 4 5 import javax.persistence.Entity; 6 import javax.persistence.Id; 7 import javax.persistence.Table; 8 9 /** 10 * 学生实体类 11 * 12 * @Table(name="",catalog="",schema=""),mysql不支持catalog属性, 13 * 这里的name属性和@Enity的name属性作用是一样的都是给表起一个名字 14 * 15 */ 16 @Entity//(name = "t_students") 17 @Table(name="t_students",schema="hibernate") 18 public class StudentA { 19 20 private int sid;//学号 21 private String sname;//姓名 22 private String gender;//出生日期 23 private Date birthday;//出生日期 24 private String major;//专业 25 private Address add;//地址 26 27 public StudentA() { 28 } 29 30 public StudentA(int sid, String sname, String gender, Date birthday, String major, Address add) { 31 this.sid = sid; 32 this.sname = sname; 33 this.gender = gender; 34 this.birthday = birthday; 35 this.major = major; 36 this.add = add; 37 } 38 39 @Id 40 public int getSid() { 41 return sid; 42 } 43 44 public void setSid(int sid) { 45 this.sid = sid; 46 } 47 48 public String getSname() { 49 return sname; 50 } 51 52 public void setSname(String sname) { 53 this.sname = sname; 54 } 55 56 public String getGender() { 57 return gender; 58 } 59 60 public void setGender(String gender) { 61 this.gender = gender; 62 } 63 64 public Date getBirthday() { 65 return birthday; 66 } 67 68 public void setBirthday(Date birthday) { 69 this.birthday = birthday; 70 } 71 72 public String getMajor() { 73 return major; 74 } 75 76 public void setMajor(String major) { 77 this.major = major; 78 } 79 80 public Address getAdd() { 81 return add; 82 } 83 84 public void setAdd(Address address) { 85 this.add = address; 86 } 87 88 89 90 91 92 93 94 }
地址实体Address
1 package com.entity; 2 3 import javax.persistence.Embeddable; 4 5 /*地址类*/ 6 7 @Embeddable /*表示嵌入类,这个类的对象在另一个实体类中充当属性*/ 8 public class Address { 9 10 private String postCode;//邮编 11 private String address;//地址 12 private String phone;//联系电话 13 14 public Address() { 15 } 16 17 public String getPostCode() { 18 return postCode; 19 } 20 public void setPostCode(String postCode) { 21 this.postCode = postCode; 22 } 23 public String getAddress() { 24 return address; 25 } 26 public void setAddress(String address) { 27 this.address = address; 28 } 29 public String getPhone() { 30 return phone; 31 } 32 public void setPhone(String phone) { 33 this.phone = phone; 34 } 35 36 37 38 39 40 }
测试用例:
1 package com.entity; 2 3 import org.hibernate.SessionFactory; 4 import org.hibernate.cfg.Configuration; 5 import org.hibernate.service.ServiceRegistry; 6 import org.hibernate.service.ServiceRegistryBuilder; 7 import org.hibernate.tool.hbm2ddl.SchemaExport; 8 import org.junit.Test; 9 10 public class TestStudents { 11 12 @Test 13 public void testShemaExport(){ 14 //创建hibernate配置对象 15 Configuration config = new Configuration().configure(); 16 17 //创建服务注册对象 18 ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); 19 20 //生成sessionFactory 21 SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); 22 23 SchemaExport export = new SchemaExport(config); 24 25 export.create(true,true); 26 27 28 } 29 }
以上是类级别的注解测试,注意一定要在Student加上@Id属性注解表示主键,否则报错。