hibernate之2.CRUD

來源:互聯網
上載者:User

標籤:style   blog   http   io   os   ar   使用   java   for   

Key Generator(主鍵產生器)

可選項說明:

1) assigned


主鍵由外部程式負責產生,無需Hibernate參與。



2) hilo


通過hi/lo 演算法實現的主鍵產生機制,需要額外的資料庫表儲存主鍵產生曆史狀態。



3) seqhilo


與hilo 類似,通過hi/lo 演算法實現的主鍵產生機制,只是主鍵曆史狀態儲存在Sequence中,適用於支援Sequence的資料庫,如Oracle。



4) increment


主鍵按數值順序遞增。此方式的實現機製為在當前應用執行個體中維持一個變數,以儲存著當前的最大值,之後每次需要產生主鍵的時候將此值加1作為主鍵。

這種方式可能產生的問題是:如果當前有多個執行個體訪問同一個資料庫,那麼由於各個執行個體各自維護主鍵狀態,不同執行個體可能產生同樣的主鍵,從而造成主鍵重複異常。因此,如果同一資料庫有多個執行個體訪問,此方式必須避免使用。



5) identity


採用資料庫提供的主鍵產生機制。如DB2、SQL Server、MySQL中的主鍵產生機制。



6) sequence


採用資料庫提供的sequence 機制產生主鍵。如Oralce 中的Sequence。



7) native


由Hibernate根據底層資料庫自行判斷採用identity、hilo、sequence其中一種作為主鍵產生方式。



8) uuid.hex


由Hibernate基於128 位唯一值產生演算法產生16 進位數值(編碼後以長度32 的字串表示)作為主鍵。



9) uuid.string


與uuid.hex 類似,只是產生的主鍵未進行編碼(長度16)。在某些資料庫中可能出現問題(如PostgreSQL)。



10) foreign


使用外部表格的欄位作為主鍵。一般而言,利用uuid.hex方式產生主鍵將提供最好的效能和資料庫平台適應性。


對應檔說明:

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping package="com.demo.model">    <class name="Student" table="t_student">        <id name="studentId" column="student_id">            <generator class="sequence"/>        </id>        <property name="studentName" column="student_name"/>        <property name="age" />    </class></hibernate-mapping>

package:類的命名空間

class:資料庫類所對應的類(實體類)

id:主鍵

name:實體類的屬性

column:表的主鍵欄位

generator:主鍵產生器

class:主鍵產生的類型,參照上面的主鍵產生器的可選項說明

property:實體類的屬性

name:實體類的屬性

column:表的主鍵欄位


提示:

如果name與column的名稱相等,column可以不用設定,如:age


CRUD:

package com.demo.model;import java.util.List;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;import org.junit.Before;import org.junit.Test;import com.demo.model.Student;/** * @author wobendiankun *2014-10-19 下午08:54:13 */public class StudentTest {SessionFactory sessionFactory ;@Beforepublic void init(){sessionFactory=new Configuration().configure().buildSessionFactory();}@Testpublic void addTest(){Student student=new Student();student.setAge(20);student.setStudentName("旺財");Session session=null;try {//開啟sessionsession=sessionFactory.openSession();//開啟事務session.beginTransaction();session.save(student);//提交事務session.getTransaction().commit();} catch (HibernateException e) {e.printStackTrace();//復原事務session.getTransaction().rollback();}finally{if(session!=null){session.close();}}}@Testpublic void deleteTest(){Student student=new Student();student.setStudentId(1);Session session=null;try {session=sessionFactory.openSession();session.beginTransaction();session.delete(student);session.getTransaction().commit();} catch (HibernateException e) {e.printStackTrace();session.getTransaction().rollback();}finally{if(session!=null){session.close();}}}@Testpublic void updateTest(){Student student=new Student();student.setStudentId(2);student.setAge(35);Session session=null;try {session=sessionFactory.openSession();session.beginTransaction();//注意:update方法更新t_student的除了主鍵外的所有欄位,//student_name未設定值,預設為空白值,//表中的資料將被更新為空白值session.update(student);//update t_student set student_name=?, age=? where student_id=?session.getTransaction().commit();} catch (HibernateException e) {e.printStackTrace();session.getTransaction().rollback();}finally{if(session!=null){session.close();}}}@Testpublic void getTest(){Session session=null;Student student=null;try {session=sessionFactory.openSession();student=(Student) session.get(Student.class, 2);}finally{if(session!=null){session.close();}}System.out.println(student);}@Testpublic void listTest(){Session session=null;List<Student> list=null;try {session=sessionFactory.openSession();Query query= session.createQuery("from Student");list=query.list();}finally{if(session!=null){session.close();}}for(Student student:list){System.out.println(student);}}}


hibernate之2.CRUD

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.