Hibernate实例分析

发布时间:2021-12-06 09:12:01 作者:iii
阅读:136
开发者专用服务器限时活动,0元免费领! 查看>>

这篇文章主要介绍“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. }  

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

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

到此,关于“Hibernate实例分析”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:
  1. Hibernate如何使用
  2. Hibernate如何配置

开发者交流群:

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

hibernate

上一篇:Hibernate中Configuration是什么

下一篇:tomcat+mysql怎么部署jsp环境

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》
开发者交流群×