简单讲述Hibernate实例

开发 后端
这里介绍Hibernate实例,increment可以在一个Hibernate实例的应用上很方便的时候,但是在集群的时候就不行了。

在向大家详细介绍Hibernate实例之前,首先让大家了解下Hibernate提供了多种生成主键的方式,然后全面介绍Hibernate实例。

Hibernate(目前使用的版本是3.2)中提供了多种生成主键的方式。然而当前的这么多种生成方式未必能满足我们的要求。比如increment,可以在一个Hibernate实例的应用上很方便的时候,但是在集群的时候就不行了。再如 identity ,sequence ,native 是数据局提供的主键生成方式,往往也不是我们需要,而且在程序跨数据库方面也体现出不足。还有基于算法的生成方式生成出来的主键基本都是字符串的。

我们现在需要一种生成方式:使用Long作为主键类型,自动增,支持集群。那么我们需要自定义一个我们的主键生成器才能实现了。

Hibernate实例代码:

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

配置:
在对应的hbm文件里面将id的配置如下:

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

【编辑推荐】

  1. Hibernate连接配置方法剖析
  2. Hibernate Session实例剖析
  3. 浅析Hibernate Callback接口
  4. Hibernate Session缓存概述
  5. Hibernate修改addMate方法
责任编辑:佚名 来源: IT168
相关推荐

2009-09-25 10:00:47

Hibernate映射

2009-09-28 10:14:08

Hibernate基础

2009-09-25 13:39:40

Hibernate属性

2009-09-27 16:51:40

Hibernate U

2009-09-24 10:40:19

Hibernate核心

2009-09-25 13:51:13

Hibernate S

2009-09-25 12:59:52

Hibernate映射

2009-09-25 15:03:21

Hibernate绑定

2009-09-27 13:25:22

2009-10-10 09:57:58

使用RHEL合法

2009-09-23 17:05:52

Hibernate S

2009-09-27 16:21:22

Hibernate C

2009-03-19 00:42:46

IFEO劫持程序

2009-10-16 13:26:53

VB.NET Exce

2009-12-18 15:28:19

Linux内核

2009-09-22 08:39:59

Hibernate F

2009-09-23 17:18:16

Hibernate S

2009-09-23 10:28:49

使用Hibernate

2009-09-22 10:09:21

Hibernate S

2009-09-23 15:12:41

Hibernate视图
点赞
收藏

51CTO技术栈公众号