欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

hibernate属性级别注解实例代码

程序员文章站 2024-04-01 23:07:46
属性级别注解 添加方式:         写在属性字段上面 写在属性的get访问器的上面...

属性级别注解

添加方式:

        写在属性字段上面
写在属性的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配置:

<!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:

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 //表示是一个嵌入类,这个类的对象在另一个实体类中充当属性  
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,"张三丰","男",new date(),"太极拳",add,7899); 
    session.save(s); 
    tx.commit(); 
//   schemaexport export = new schemaexport(config); 
//    export.create(true,true); 
  } 
} 

总结

以上就是本文关于hibernate属性级别注解实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:hibernate核心思想与接口简介hibernate实现悲观锁和乐观锁代码介绍、等。正所谓书中自有颜如玉,下面附几本不错的书籍,供大家学习参考,在此也非常希望大家对本站多多支持。

一键下载,免费的哦:

hibernate官方入门教程 中文pdf版

java设计模式深入研究 pdf

数据挖掘:实用机器学习技术及java实现(英文第2版)高清pdf