简单讲述Hibernate实例

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

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

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

Hibernate实例代码:

 
 
 
  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. }

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

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

当前标题:简单讲述Hibernate实例
本文链接:http://www.hantingmc.com/qtweb/news32/330232.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联