千家信息网

Hibernate实例分析

发表于:2024-10-08 作者:千家信息网编辑
千家信息网最后更新 2024年10月08日,这篇文章主要介绍"Hibernate实例分析",在日常操作中,相信很多人在Hibernate实例分析问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"Hibernate实例
千家信息网最后更新 2024年10月08日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的配置如下:

  

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

0