Hibernate中的PO

來源:互聯網
上載者:User

標籤:關閉   utf-8   void   idle   his   擷取   nec   java   nfa   

Hibernate中的PO

  PO就是持久化對象,它的作用就是完成持久化操作,即通過以物件導向的方式操作該對象對資料庫中的資料執行增、刪、改、查的操作。

  Hibernate是低侵入式的設計,完全採用Java對象作為持久化對象,在Hibernate中,持久化類不用繼承任何父類或者實現任何的介面,只要為其添加註解就可以使其成為一個PO類。

  樣本:

import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name="text_info")public class News_1 {    //訊息類的識別屬性    @Id    @GeneratedValue(strategy=GenerationType.IDENTITY)    private Integer id;    //訊息標題    private String title;    //訊息內容    private String content;    //setter、getter方法    public void setId(Integer id){        this.id = id;    }    public Integer getId(){        return this.id;    }    public void setTitle(String title){        this.title = title;    }    public String getTitle(){        return this.title;    }    public void setContent(String content){        this.content = content;    }    public String getContent(){        return this.content;    }}

 

  註解的簡單解釋:

  @Entity:聲明該類是一個Hibernate的持久化類。

  @Table:指定該類所映射的表。

  @Id:指定該類的識別屬性,即可以唯一標識該對象的屬性,通常映射到資料表中的主鍵欄位。

  @GenerateValue:指定主鍵的建置原則,其中Strategy屬性指定了主鍵建置原則為IDENTITY即自增長策略。

 

  使用該PO類,為資料庫中增添表以及記錄:

  (1)配置XML檔案

<?xml version=‘1.0‘ encoding=‘utf-8‘?><!DOCTYPE hibernate-configuration PUBLIC"-//Hibernate/Hibernate Configuration DTD//EN""http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration>    <session-factory>        <!-- 指定串連資料庫所用的驅動 -->        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>        <!-- 指定串連資料庫的url,其中hibernate是資料庫名 -->        <property name="hibernate.connection.url">jdbc:mysql://localhost/hibernate</property>        <!-- 指定串連資料庫的使用者名稱 -->        <property name="hibernate.connection.username">root</property>        <!-- 指定串連資料庫的密碼 -->        <property name="hibernate.connection.password">stx12345</property>        <!-- 指定串連池中的最大串連數 -->        <property name="hibernate.c3p0.max_size">20</property>        <!-- 指定串連池中的最小串連數 -->        <property name="hibernate.c3p0.min_size">1</property>        <!-- 指定串連池裡串連的逾時時間長度 -->        <property name="hibernate.c3p0.timeout">5000</property>        <!-- 指定串連池裡最大緩衝多少個Statement對象 -->        <property name="hibernate.c3p0.statements">100</property>        <property name="hibernate.c3p0.idle_test_period">3000</property>        <property name="hibernate.c3p0.acquire_increment">2</property>        <property name="hibernate.c3p0.validate">true</property>        <!-- 指定資料庫方言 -->        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>        <!-- 根據需要自動建立資料表 -->        <property name="hibernate.hbm2ddl.auto">update</property>        <!-- 顯示Hibernate持久化操作所產生的SQL -->         <property name="show_sql">true</property>        <!-- 將SQL指令碼進行格式化後再輸出 -->        <property name="hibernate.format_sql">true</property>        <!-- 羅列所有持久化類的類名 -->            <mapping class="myPOTest.News"/>    </session-factory> </hibernate-configuration>

 

  (2)主程式

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 myPOTest.News;public class NewsManager {    public static void main(String[] args)throws Exception{        //執行個體化Configuration        Configuration conf = new Configuration().configure();        ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(conf.getProperties()).build();        //用Configuration執行個體建立SessionFactory執行個體        SessionFactory sf = conf.buildSessionFactory(serviceRegistry);        //建立Session        Session sess = sf.openSession();        //開始事務        Transaction tx = sess.beginTransaction();        //建立訊息對象        News n = new News();        //設定標題和訊息內容        n.setTitle("A");        n.setContent("a");        //儲存訊息        sess.save(n);        //提交事務        tx.commit();        //關閉Session        sess.close();        sf.close();    }}

 

  注意:hibernate.cfg.xml檔案要放在src目錄下,注意,除了匯入Hibernate的包以外還需要匯入c3p0的包和mysql的包

  使用Hibernate進行持久化操作的步驟:

  (1)開發持久化類

  (2)擷取Configuration

  (3)擷取SessionFactory

  (4)擷取Session,開啟事務

  (5)使用物件導向的方式操作資料庫

  (6)關閉事物,關閉Session

  PO的三種狀態:

  (1)瞬態:PO從未與Session關聯過,該PO處於瞬態;

  (2)持久化:PO執行個體與Session關聯,且該執行個體對應到資料庫記錄,則該執行個體處於持久化狀態;

  (3)託管:PO曾與Session關聯過,由於Session的關閉或者其他原因,PO脫離了Session的管理,這種狀態被稱為託管狀態。

  對PO的操作必須在Session的管理下才能同步到資料庫。Session由SessionFactory產生。SessionFactory對象由Configyration對象產生,Configuration對象負責載入Hibernate的設定檔。

 

 

Hibernate中的PO

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.