溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

hibernate屬性級(jí)別注解實(shí)例代碼

發(fā)布時(shí)間:2020-09-25 19:16:41 來(lái)源:腳本之家 閱讀:158 作者:黃小魚(yú)ZZZ 欄目:編程語(yǔ)言

屬性級(jí)別注解

添加方式:

        寫(xiě)在屬性字段上面
寫(xiě)在屬性的get訪(fǎng)問(wèn)器的上面

@Id

    必須,定義了映射到數(shù)據(jù)庫(kù)表的主鍵的屬性,一個(gè)實(shí)體類(lèi)可以有一個(gè)或者多個(gè)屬性被映射為主鍵,可置于主鍵屬性或者getXxx()前,注意:如果有多個(gè)屬性定義為主鍵屬性,該實(shí)體類(lèi)必須實(shí)現(xiàn)serializable接口

@SequenceGenerator
@GeneratedValue
  @GeneratedValue(strategy=GenerationType,generator=""):可選,用于定義主鍵生成策略

  strategy表示主鍵生成策略,取值有:

       GenerationType.AUTO:根據(jù)底層數(shù)據(jù)庫(kù)自動(dòng)選擇(默認(rèn))
GenerationType.INDENTITY:根據(jù)數(shù)據(jù)庫(kù)的Identity字段生成
GenerationType.SEQUENCE:使用Sequence來(lái)決定主鍵的取值
GenerationType.TABLE:使用指定表來(lái)決定主鍵取值,結(jié)合@TableGenerator使用

@Column

     @Column-可將屬性映射到列,使用該注解來(lái)覆蓋默認(rèn)值,@Column描述了數(shù)據(jù)庫(kù)表中該字段的詳細(xì)定義,這對(duì)于根據(jù)JPA
注解生成數(shù)據(jù)庫(kù)表結(jié)構(gòu)的工具非常有作用。

常用屬性:

name:可選,表示數(shù)據(jù)庫(kù)表中該字段的名稱(chēng),默認(rèn)情形屬性名稱(chēng)一致
nullable:可選,表示該字段是否允許為null,默認(rèn)為true
unique:可選,表示該字段是否為唯一標(biāo)識(shí),默認(rèn)為false
length:可選,表示該字段的大小,僅對(duì)String類(lèi)型的字段有效,默認(rèn)值225,主鍵不能使用默認(rèn)值
insertable:可選,表示在ORM框架執(zhí)行插入操作時(shí),該字段是否應(yīng)出現(xiàn)INSERTRT語(yǔ)句中,默認(rèn)為true
updateable:可選,表示在ORM框架執(zhí)行更新操作時(shí),該字段是否應(yīng)該出現(xiàn)在UPDATE語(yǔ)句中,默認(rèn)為true。對(duì)于已經(jīng)創(chuàng)建
就不可以更改的字段,該屬性非常有用

@Embedded

@Embedded是注釋屬性的,表示該屬性的類(lèi)是嵌入類(lèi)。

注意:同時(shí)嵌入類(lèi)也必須標(biāo)注@Embeddable注解

@EmbeddedId

@EmbeddedId使用嵌入式主鍵類(lèi)實(shí)現(xiàn)復(fù)合主鍵

注意:嵌入式主鍵類(lèi)必須實(shí)現(xiàn)Serializable接口、必須有默認(rèn)的public無(wú)參數(shù)的構(gòu)造方法、必須覆蓋equals和hashCode方法

@Lob
@Version
@Basic
@Transient

      可選,表示該屬性并非一個(gè)到數(shù)據(jù)庫(kù)表的字段的映射,ORM框架將忽略該屬性,如果一個(gè)屬性并非數(shù)據(jù)庫(kù)表的字段映射,就
務(wù)必將其標(biāo)示為@Transient,否則ORM框架默認(rèn)其注解為@Basic

實(shí)例:

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> 

實(shí)體類(lèi)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; 
/* 
 * 學(xué)生實(shí)體類(lèi) 
 */ 
//@Entity// 
//@Entity(name="t_students")//如果不添加名字,則默認(rèn)與實(shí)體類(lèi)名字相同,如果想要自行設(shè)置表明,就需要自己進(jìn)行添加 
@Entity 
@Table(name="t_students1",schema="mypage") 
public class Students implements Serializable{ 
     @Id 
//    @GeneratedValue 
//    @GeneratedValue(strategy=GenerationType.AUTO)//與@GeneratedValue結(jié)果相同,字符串類(lèi)型不能指定為AUTO類(lèi)型 
//     private int sid; //學(xué)號(hào) 
//    @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;//專(zhuān)業(yè) 
     private Address add; 
     @Transient   //表示該字段不會(huì)被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實(shí)體類(lèi):

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;//身份證號(hào) 
  @Column(length=8) 
    private String sid;//學(xué)號(hào) 
    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(); 
  } 
} 

地址類(lèi):

package com.entity; 
import javax.persistence.Embeddable; 
import javax.persistence.Embedded; 
// 地址類(lèi) 
@Embeddable //表示是一個(gè)嵌入類(lèi),這個(gè)類(lèi)的對(duì)象在另一個(gè)實(shí)體類(lèi)中充當(dāng)屬性  
public class Address { 
   private String postCode;//郵編 
   private String address;//地址 
   private String phone;//聯(lián)系電話(huà) 
   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; 
  } 
    
} 

測(cè)試類(lèi):

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(){ 
      //創(chuàng)建hibernate配置對(duì)象 
      Configuration config = new Configuration().configure(); 
      //創(chuàng)建服務(wù)注冊(cè)對(duì)象 
      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(){ 
     //創(chuàng)建hibernate配置對(duì)象 
    Configuration config = new Configuration().configure(); 
    //創(chuàng)建服務(wù)注冊(cè)對(duì)象 
    ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); 
    //生成SessionFactory 
    SessionFactory sessionFactory = config.buildSessionFactory(serviceRegistry); 
    //創(chuàng)建會(huì)話(huà) 
    Session session = sessionFactory.getCurrentSession(); 
    //創(chuàng)建事務(wù) 
    Transaction tx = session.beginTransaction(); 
    Address add = new Address("700005","河南理工大學(xué)","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); 
  } 
} 

總結(jié)

以上就是本文關(guān)于hibernate屬性級(jí)別注解實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:Hibernate核心思想與接口簡(jiǎn)介、Hibernate實(shí)現(xiàn)悲觀(guān)鎖和樂(lè)觀(guān)鎖代碼介紹、快速了解hibernate配置文件與映射文件等。正所謂書(shū)中自有顏如玉,下面附幾本不錯(cuò)的書(shū)籍,供大家學(xué)習(xí)參考,在此也非常希望大家對(duì)本站多多支持。

一鍵下載,免費(fèi)的哦:

hibernate官方入門(mén)教程 中文PDF版

https://www.jb51.net/books/496598.html

Java設(shè)計(jì)模式深入研究 PDF

https://www.jb51.net/books/581548.html

數(shù)據(jù)挖掘:實(shí)用機(jī)器學(xué)習(xí)技術(shù)及Java實(shí)現(xiàn)(英文第2版)高清PDF

https://www.jb51.net/books/577815.html

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI