hibernate中自定义主键(集群)

  在HIBERNATE中,identity ,sequence ,native 是数据局提供的主键生成方式,往往也不是我们需要,而且在程序跨数据库方面也体现出不足.还有基于算法的生成方式生成出来的主键基本都是字符串的.

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

 主键生成器代码 类:

     package com.ta.jiang;
  
  import java.io.Serializable;
  
  import java.sql.Connection;
  
  import java.sql.PreparedStatement;
  
  import java.sql.ResultSet;
  
  import java.sql.SQLException;
  
  import java.util.Properties;
  
  import org.apache.commons.logging.Log;
  
  import org.apache.commons.logging.LogFactory;
  
  import org.hibernate.HibernateException;
  
  import org.hibernate.MappingException;
  
  import org.hibernate.dialect.Dialect;
  
  import org.hibernate.engine.SessionImplementor;
  
  import org.hibernate.id.Configurable;
  
  import org.hibernate.id.IdentifierGenerator;
  
  import org.hibernate.id.PersistentIdentifierGenerator;
  
  import org.hibernate.type.Type;
  
  public class IncrementGenerator implements IdentifierGenerator, Configurable {
  
  private static final Log log = LogFactory.getLog(IncrementGenerator.class);
  
  private Long next;
  
  private String sql;
  
  public Serializable generate(SessionImplementor session, Object object)
  
  throws HibernateException {
  
  if (sql!=null) {
  
  getNext( session.connection() );
  
  }
  
  return next;
  
  }
  
  public void configure(Type type, Properties params, Dialect d) throws MappingException {
  
  String table = params.getProperty("table");
  
  if (table==null) table = params.getProperty(PersistentIdentifierGenerator.TABLE);
  
  String column = params.getProperty("column");
  
  if (column==null) column = params.getProperty(PersistentIdentifierGenerator.PK);
  
  String schema = params.getProperty(PersistentIdentifierGenerator.SCHEMA);
  
  sql = "select max("+column +") from " + ( schema==null ? table : schema + '.' + table );
  
  log.info(sql);
  
  }
  
  private void getNext(Connection conn) throws HibernateException {
  
  try {
  
  PreparedStatement st = conn.prepareStatement(sql);
  
  ResultSet rs = st.executeQuery();
  
  if ( rs.next() ) {
  
  next = rs.getLong(1) + 1;
  
  }
  
  else {
  
  next = 1l;
  
  }
  
  }catch(SQLException e)
  
  {
  
  throw new HibernateException(e);
  
  }
  
  finally {
  
  try{
  
  conn.close();
  
  }catch(SQLException e)
  
  {
  
  throw new HibernateException(e);
  
  }
  
  }
  
  }
  
  }

 

  代码二:

  Hibernate 配置文件:

  <id name="id" type="long" column="id" >
  
  <generator class="com.ta.jiang.IncrementGenerator" />
  
  </id>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hibernate 配置联合主键,可以通过使用 @IdClass 或 @EmbeddedId 注解来实现。 1. 使用 @IdClass 注解: 首先,需要创建一个用于表示联合主键的类,该类需要实现 Serializable 接口,并且需要包含所有联合主键字段。例如: ``` public class MyCompositeKey implements Serializable { private Long id1; private Long id2; // constructors, getters, setters, equals, hashCode, etc. } ``` 然后,在实体类使用 @IdClass 注解,并指定联合主键类的名称。例如: ``` @Entity @IdClass(MyCompositeKey.class) public class MyEntity { @Id private Long id1; @Id private Long id2; // other entity fields, getters, setters, etc. } ``` 2. 使用 @EmbeddedId 注解: 首先,需要创建一个用于表示联合主键的类,该类需要实现 Serializable 接口,并且需要使用 @Embeddable 注解标记。例如: ``` @Embeddable public class MyCompositeKey implements Serializable { private Long id1; private Long id2; // constructors, getters, setters, equals, hashCode, etc. } ``` 然后,在实体类使用 @EmbeddedId 注解,并指定联合主键类的名称。例如: ``` @Entity public class MyEntity { @EmbeddedId private MyCompositeKey id; // other entity fields, getters, setters, etc. } ``` 以上就是在 Hibernate 配置联合主键的两种方式。需要注意的是,使用 @IdClass 注解时需要在实体类指定所有联合主键字段,而使用 @EmbeddedId 注解时只需要在联合主键指定即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值