程序员人生 网站导航

hibernate注解-属性级别注解

栏目:数据库应用时间:2016-07-07 08:54:09

属性级别注解

添加方式:

        写在属性字段上面
写在属性的get访问器的上面


@Id

    必须,定义了映照到数据库表的主键的属性,1个实体类可以有1个或多个属性被映照为主键,
    可置于主键属性或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:可选,表示数据库表中该字段的名称,默许情形属性名称1致
nullable:可选,表示该字段是不是允许为null,默许为true
unique:可选,表示该字段是不是为唯1标识,默许为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

      可选,表示该属性并不是1个到数据库表的字段的映照,ORM框架将疏忽该属性,如果1个属性并不是数据库表的字段映照,就
      务势必其标示为@Transient,否则ORM框架默许其注解为@Basic
实例:
        hibernate.properties配置:
         
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration⑶.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:
      
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实体类:
     
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(); } }
地址类:
   
package com.entity; import javax.persistence.Embeddable; import javax.persistence.Embedded; // 地址类 @Embeddable //表示是1个嵌入类,这个类的对象在另外一个实体类中充当属性 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; } }
测试类:
       
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,"张3丰","男",new Date(),"太极拳",add,7899); session.save(s); tx.commit(); // SchemaExport export = new SchemaExport(config); // export.create(true,true); } }

------分隔线----------------------------
------分隔线----------------------------

最新技术推荐