這篇文章主要介紹“Hibernate實(shí)例分析”,在日常操作中,相信很多人在Hibernate實(shí)例分析問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Hibernate實(shí)例分析”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
創(chuàng)新互聯(lián)是一家專注于網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì)與策劃設(shè)計(jì),牙克石網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10余年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:牙克石等地區(qū)。牙克石做網(wǎng)站價(jià)格咨詢:13518219792
Hibernate(目前使用的版本是3.2)中提供了多種生成主鍵的方式。然而當(dāng)前的這么多種生成方式未必能滿足我們的要求。比如increment,可以在一個(gè)Hibernate實(shí)例的應(yīng)用上很方便的時(shí)候,但是在集群的時(shí)候就不行了。再如 identity ,sequence ,native 是數(shù)據(jù)局提供的主鍵生成方式,往往也不是我們需要,而且在程序跨數(shù)據(jù)庫方面也體現(xiàn)出不足。還有基于算法的生成方式生成出來的主鍵基本都是字符串的。
我們現(xiàn)在需要一種生成方式:使用Long作為主鍵類型,自動(dòng)增,支持集群。那么我們需要自定義一個(gè)我們的主鍵生成器才能實(shí)現(xiàn)了。
Hibernate實(shí)例代碼:
package hibernate;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.id.Configurable;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.id.PersistentIdentifierGenerator;
import org.hibernate.type.Type;
public class IncrementGenerator implements IdentifierGenerator, Configurable {
private static final Log log = LogFactory.getLog(IncrementGenerator.class);
private Long next;
private String sql;
public Serializable generate(SessionImplementor session, Object object)
throws HibernateException {
if (sql!=null) {
getNext( session.connection() );
}
return next;
}
public void configure(Type type, Properties params, Dialect d)
throws MappingException {String table = params.getProperty("table");
if (table==null) table = params.
getProperty(PersistentIdentifierGenerator.TABLE);String column = params.getProperty("column");
if (column==null) column = params.
getProperty(PersistentIdentifierGenerator.PK);String schema = params.getProperty
(PersistentIdentifierGenerator.SCHEMA);sql = "select max("+column +") from " +
( schema==null ? table : schema + '.' + table );log.info(sql);
}
private void getNext(Connection conn) throws HibernateException {
try {
PreparedStatement st = conn.prepareStatement(sql);
ResultSet rs = st.executeQuery();
if ( rs.next() ) {
next = rs.getLong(1) + 1;
}
else {
next = 1l;
}
}catch(SQLException e)
{
throw new HibernateException(e);
}
finally {
try{
conn.close();
}catch(SQLException e)
{
throw new HibernateException(e);
}
}
}
}
配置:
在對應(yīng)的hbm文件里面將id的配置如下:
<id name="id" type="long" column="id" > <generator class="hibernate.IncrementGenerator" /> </id>
到此,關(guān)于“Hibernate實(shí)例分析”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
本文題目:Hibernate實(shí)例分析
文章出自:http://chinadenli.net/article4/jiidie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、域名注冊、軟件開發(fā)、Google、微信公眾號、
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)