日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
簡(jiǎn)單講述Hibernate實(shí)例

在向大家詳細(xì)介紹Hibernate實(shí)例之前,首先讓大家了解下Hibernate提供了多種生成主鍵的方式,然后全面介紹Hibernate實(shí)例。

Hibernate(目前使用的版本是3.2)中提供了多種生成主鍵的方式。然而當(dāng)前的這么多種生成方式未必能滿足我們的要求。比如increment,可以在一個(gè)Hibernate實(shí)例的應(yīng)用上很方便的時(shí)候,但是在集群的時(shí)候就不行了。再如 identity ,sequence ,native 是數(shù)據(jù)局提供的主鍵生成方式,往往也不是我們需要,而且在程序跨數(shù)據(jù)庫(kù)方面也體現(xiàn)出不足。還有基于算法的生成方式生成出來(lái)的主鍵基本都是字符串的。

我們現(xiàn)在需要一種生成方式:使用Long作為主鍵類型,自動(dòng)增,支持集群。那么我們需要自定義一個(gè)我們的主鍵生成器才能實(shí)現(xiàn)了。

Hibernate實(shí)例代碼:

 
 
 
  1. package hibernate;
  2. import java.io.Serializable;
  3. import java.sql.Connection;
  4. import java.sql.PreparedStatement;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.Properties;
  8. import org.apache.commons.logging.Log;
  9. import org.apache.commons.logging.LogFactory;
  10. import org.hibernate.HibernateException;
  11. import org.hibernate.MappingException;
  12. import org.hibernate.dialect.Dialect;
  13. import org.hibernate.engine.SessionImplementor;
  14. import org.hibernate.id.Configurable;
  15. import org.hibernate.id.IdentifierGenerator;
  16. import org.hibernate.id.PersistentIdentifierGenerator;
  17. import org.hibernate.type.Type;
  18. public class IncrementGenerator implements IdentifierGenerator, Configurable {
  19. private static final Log log = LogFactory.getLog(IncrementGenerator.class);
  20. private Long next;
  21. private String sql;
  22. public Serializable generate(SessionImplementor session, Object object)
  23. throws HibernateException {
  24. if (sql!=null) {
  25. getNext( session.connection() );
  26. }
  27. return next;
  28. }
  29. public void configure(Type type, Properties params, Dialect d) 
    throws MappingException {
  30. String table = params.getProperty("table");
  31. if (table==null) table = params.
    getProperty(PersistentIdentifierGenerator.TABLE);
  32. String column = params.getProperty("column");
  33. if (column==null) column = params.
    getProperty(PersistentIdentifierGenerator.PK);
  34. String schema = params.getProperty
    (PersistentIdentifierGenerator.SCHEMA);
  35. sql = "select max("+column +") from " + 
    ( schema==null ? table : schema + '.' + table );
  36. log.info(sql);
  37. }
  38. private void getNext(Connection conn) throws HibernateException {
  39. try {
  40. PreparedStatement st = conn.prepareStatement(sql);
  41. ResultSet rs = st.executeQuery();
  42. if ( rs.next() ) {
  43. next = rs.getLong(1) + 1;
  44. }
  45. else {
  46. next = 1l;
  47. }
  48. }catch(SQLException e)
  49. {
  50. throw new HibernateException(e);
  51. }
  52. finally {
  53. try{
  54. conn.close();
  55. }catch(SQLException e)
  56. {
  57. throw new HibernateException(e);
  58. }
  59. }
  60. }
  61. }

配置:
在對(duì)應(yīng)的hbm文件里面將id的配置如下:

 
 
 
  1.  name="id" type="long" column="id" >
  2.  class="hibernate.IncrementGenerator" />

分享標(biāo)題:簡(jiǎn)單講述Hibernate實(shí)例
URL標(biāo)題:http://www.5511xx.com/article/cceiici.html