Hibernate学习之属性级别注解
© 版权声明:本文为博主原创文章,转载请注明出处
属性级别注解
添加方式
1. 写在属性字段上面
2. 写在属性getter方法上面
@Id:必须,定义了映射到数据库表的主键属性,一个实体可以有一个或多个属性被映射为主键(如果有多个属性定义为主键属性,则必须实现Serializable接口)
@EmbeddableId:可选,使用嵌入式主键类实现复合主键(嵌入式主键类必须实现Serializable接口、必须有默认的无参构造器、必须覆盖equals和hashCode方法)
@GeneratedValue:可选,用于定义主键生成策略(@GeneratedValue(strategy=GenerationType.*, generator=""))
strategy:定义主键生成策略
- GenerationType.AUTO:根据底层数据库自动选择(默认)
- GenerationType.IDENTITY:根据数据库的Identity字段生成
- GenerationType.SEQUENCE:根据Sequence来决定主键的取值
- GenerationType.TABLE:使用指定表来决定主键取值,结合@TableGenerator使用
generator:引用@GenericGenerator指定的主键生成策略
@Column:可选,将属性映射到列,使用该注解来覆盖默认值
常用属性:
- name:可选,表示数据库中该字段的名称,默认与属性名称一致
- nullable:可选,表示该字段是否允许为null,默认true
- unique:可选,表示该字段是否是唯一标识,默认false
- length:可选,表示该字段的长度,仅对String类型的字段有效,默认255(如果是主键则不能使用默认值)
- insertable:可选,表示在ORM框架执行插入操作时,该字段是否出现在insert语句中,默认true
- updateable:可选,表示在ORM框架执行更新操作时,该字段是否出现在update语句中,默认true
(对于一经创建就不能修改的字段,该属性非常有效。eg:出生日期、插入时间等)
@Embeddable:可选,表示该属性的类是嵌入类(嵌入类也必须标注@Embeddable注解)
@Transient:可选,表示该属性并非一个数据库表的字段的映射,ORM框架将忽略该属性
实例
1.项目结构
2.pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.hibernate</groupId> <artifactId>Hibernate-AttributeAnnotation</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <hibernate.version>5.1.7.Final</hibernate.version> </properties> <dependencies> <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- Hibernate --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>${hibernate.version}</version> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.42</version> </dependency> </dependencies> </project>
3.StudentPK.java
package org.hibernate.model; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.GeneratedValue; import org.hibernate.annotations.GenericGenerator; /** * 学生主键类 * */ public class StudentPK implements Serializable { private static final long serialVersionUID = 1L; @GeneratedValue private long sid;// 学号 @GeneratedValue(generator="custom") @GenericGenerator(name="custom", strategy="assigned") @Column(length = 18) private String id; // 身份证号 public StudentPK() { } public StudentPK(long sid, String id) { this.sid = sid; this.id = id; } public long getSid() { return sid; } public void setSid(long sid) { this.sid = sid; } public String getId() { return id; } public void setId(String id) { this.id = id; } @Override public int hashCode() { return super.hashCode(); } @Override public boolean equals(Object obj) { return super.equals(obj); } }
4.Address.java
package org.hibernate.model; import javax.persistence.Embeddable; /** * 地址类 * */ @Embeddable public class Address { private String postCode;// 邮编 private String address;// 地址 private String telephone;// 电话 public Address() { } public Address(String postCode, String address, String telephone) { this.postCode = postCode; this.address = address; this.telephone = telephone; } public String getPostCode() { return postCode; } public void setPostCode(String postCode) { this.postCode = postCode; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } }
5.Student.java
package org.hibernate.model; import java.util.Date; import javax.persistence.Embedded; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import javax.persistence.Table; import javax.persistence.Transient; @Entity @Table(name="t_student") public class Student { @EmbeddedId public StudentPK id;// 主键 public String name;// 姓名 public String sex;// 性别 public Date birthday;// 出生日期 @Embedded public Address address;// 地址 @Transient public String gf;// 女朋友 public Student(StudentPK id, String name, String sex, Date birthday, Address address, String gf) { this.id = id; this.name = name; this.sex = sex; this.birthday = birthday; this.address = address; this.gf = gf; } public StudentPK getId() { return id; } public void setId(StudentPK id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public String getGf() { return gf; } public void setGf(String gf) { this.gf = gf; } }
6.hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <!-- SessionFactory配置 --> <session-factory> <!-- 数据库配置 --> <property name="connection.username">root</property> <property name="connection.password">***</property> <property name="connection.url"> jdbc:mysql:///hibernate?useSSL=true&characterEncoding=UTF-8 </property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- 使用常用配置 --> <property name="show_sql">true</property>
<property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property> <!-- 引入映射类 --> <mapping class="org.hibernate.model.Student"/> </session-factory> </hibernate-configuration>
7.TestAttributeAnnotation.java
package org.hibernate.test; import java.util.Date; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.hibernate.model.Address; import org.hibernate.model.Student; import org.hibernate.model.StudentPK; import org.junit.After; import org.junit.Before; import org.junit.Test; public class TestAttributeAnnotation { private SessionFactory sessionFactory; private Session session; private Transaction transaction; @Before public void before() { sessionFactory = new Configuration().configure().buildSessionFactory();// 创建SessionFactory对象 session = sessionFactory.openSession();// 获取Session对象 transaction = session.beginTransaction();// 开启事务 } @After public void after() { transaction.commit();// 提交事务 session.clear();// 关闭Session sessionFactory.close();// 关闭SessionFactory } @Test public void testAnnotation() { Address address = new Address("100000", "北京市", "15812345678"); StudentPK pk = new StudentPK(1L, "123456780123456789"); Student student = new Student(pk, "张三", "男", new Date(), address, "李四"); session.save(student); } }
8.效果预览
8.1 console
8.2 自动生成的表结构
8.3 数据