天天看点

JPA中使用 @GenericGenerator 自定义方式 生成 主键 ID

一、官网资料

1、文档地址:https://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html#identifiers-generators-GenericGenerator

2、文档说明:需要实现 org.hibernate.id.IdentifierGenerator 这个接口

@GenericGenerator allows integration of any Hibernate org.hibernate.id.IdentifierGenerator implementation, 
including any of the specific ones discussed here and any custom ones.
           

3、官网中的案例(写的很详细,直接对这个类进行改造):

JPA中使用 @GenericGenerator 自定义方式 生成 主键 ID

二、改造一下官网的案例 

1、实体类:

@Data
@Entity
@Table(name = OrderTable.TABLE_NAME,
    indexes = {
        @Index(name = "id", columnList = "id", unique = true)
    })
@AllArgsConstructor
@NoArgsConstructor
@Builder
@EntityListeners(AuditingEntityListener.class)
public class OrderTable {

  public static final String TABLE_NAME = "order_table";

  /**
   * 使用 snowflake : 雪花算法。
   */
  @Id
  @GeneratedValue(generator = "orderIdGenerator",
      strategy = GenerationType.SEQUENCE)
  @GenericGenerator(
      name = "orderIdGenerator",
      strategy = "com.wangyk.sharding.config.OrderIdGeneratorConfig"
  )
  private Long id;

  @Column(columnDefinition = "BIGINT(20) COMMENT '订单Id'")
  private Long orderId;

  @Column(columnDefinition = "BIGINT(20) COMMENT '用户Id'")
  private Long userId;

  @Column(columnDefinition = "VARCHAR(255) COMMENT '订单备注'")
  private String remark;

  @Column
  @CreatedDate
  private Date creation;

  @Column
  @LastModifiedDate
  private Date modification;
}
           

2.实现类

@Slf4j
public class OrderIdGeneratorConfig implements IdentifierGenerator {

  @Override
  public Serializable generate(SharedSessionContractImplementor session, Object object)
      throws HibernateException {
    /**
     * 获取容器中的 Bean
     */
    SnowflakeIdWorker snowflakeIdWorker = SpringUtil.getBean(SnowflakeIdWorker.class);
    long id = snowflakeIdWorker.nextId();
    log.info("id -> [{}]", id);
    return id;
  }
}
           

3.自定义生成主键ID成功

JPA中使用 @GenericGenerator 自定义方式 生成 主键 ID