hibernate native 主鍵建置原則

來源:互聯網
上載者:User

前一次做個系統用的oracle資料庫,使用hibernate產生主鍵的策略是SEQUENCE,當時覺得很累,因為不知道怎麼的,oracle+sequence+trigger怎麼也取不到新增資料時的主索引值。

 

這次就把重點放這裡了,同時還有兩個新的問題,

1。到底哪種hibernate產生主鍵的策略,效能優一點;

2。主鍵建置原則最好是能跨資料庫的。

 

 

大家都說效能,oracle都是用的sequence,所以主鍵還是想用這個方式產生。

接著就是返回新增資料的主鍵


native
根據底層資料庫的能力選擇 identity、sequence 或者 hilo 中的一個;

也就是說,主鍵產生,由hibernate選擇。

 

對於內部支援識別欄位的資料庫(DB2、MySQL、Sybase 和 MS SQL),你可以使用 identity 關
鍵字產生。對於內部支援序列的資料庫(DB2、Oracle、PostgreSQL、Interbase、McKoi 和 SAP
DB),你可以使用 sequence 風格的關鍵字產生。這兩種方式對於插入一個新的對象都需要兩次
SQL 查詢。

 

查詢了一下更多的網上資料,不知道在哪裡看到了,這種產生主鍵的策略,如果是mysql,他會選擇auto_increment方式產生主鍵;如果是oracle,他會選擇sequence方式;只是在使用oracle資料庫時,需要建立一個hibernate_sequence,這是hibernate保留的,不建會報錯(測試過)。

 

如下是完全的代碼: Model:

package com.gwtjs.model; /** * @hibernate.class table="t_user" */ public class User { /** * @hibernate.id generator-class="native" length="32" */ private int id; /** * @hibernate.property type="string" length="32" unique="true" not-null="true" */ private String userName; /** * @hibernate.property type="string" length="32" not-null="true" */ private String userPwd; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getUserPwd() { return userPwd; } public void setUserPwd(String userPwd) { this.userPwd = userPwd; } }

  hibernate   mapping

<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class table="t_user" name="com.gwtjs.model.User"> <id access="field" name="id" length="32"> <generator class="native"/> </id> <property name="userName" length="32" access="field" unique="true" not-null="true" type="string"/> <property name="userPwd" length="32" access="field" not-null="true" type="string"/> </class> </hibernate-mapping>

   oracle表建立指令碼:

-- Create table create table T_USER ( id NUMBER(10) not null, username VARCHAR2(32) not null, userpwd VARCHAR2(32) not null ) tablespace FOUNDDB pctfree 10 initrans 1 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited ); -- Create/Recreate primary, unique and foreign key constraints alter table T_USER add primary key (ID) using index tablespace FOUNDDB pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited ); alter table T_USER add unique (USERNAME) using index tablespace FOUNDDB pctfree 10 initrans 2 maxtrans 255 storage ( initial 64K minextents 1 maxextents unlimited );

  hibernate_sequence建立指令碼

-- create user table sequence create sequence t_user_seq minvalue 1 maxvalue 9999999999 start with 1 increment by 1 nocache; -- 公用的主鍵標識seq CREATE SEQUENCE HIBERNATE_SEQUENCE MINVALUE 1 MAXVALUE 999999999999999999999999 INCREMENT BY 1 NOCYCLE; -- 插入使用者的觸發器 create or replace trigger tri_insert_user before insert on t_user for each row begin select t_user_seq.nextval into :new.id from dual; end;

  Hibernate   DAO

public class UserManagerImpl extends AbstractSearch implements UserManager { public void saveOrUpdate(User u){ getHibernateTemplate().saveOrUpdate(u); } public void delete(int id){ getHibernateTemplate().delete(getUser(id)); } public User getUser(int id){ return (User)getHibernateTemplate().get(User.class,id); } }

  DAO  TEST  Base Class

public class DaoTestBase extends AbstractTransactionalDataSourceSpringContextTests { protected String[] getConfigLocations() { this.setAutowireMode(AUTOWIRE_BY_NAME); this.setDefaultRollback(false); return new String[]{ "classpath:spring-config.xml", "classpath:/config/spring/applicationContext-dao.xml" }; } public void testConfig(){ assertNotNull("spring-mock context has bean init ",this.applicationContext); } }

  User DAO  Test

 

public class UserManagerTest extends DaoTestBase { private UserManager userManager; public void testSave(){ User u = new User(); u.setUserName("admin888"); u.setUserPwd("654123"); userManager.saveOrUpdate(u); logger.info(u.getId()); System.out.println("高度啊: "+u.getId()); userManager.delete(u.getId()); } public void setUserManager(UserManager userManager){ this.userManager = userManager; } }

  spring bean

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="userManager" class="com.gwtjs.dao.impl.UserManagerImpl"> <property name="sessionFactory" ref="sessionFactory"/> </bean> </beans>

 

完美的測試成功

 

 

 

 

 

聯繫我們

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