提交 f8e49f1d 作者: 方治民

feat: 更新雪花 ID 生成器的使用,提供全新的 @SnowflakeIdGenerator 注解支持自动解析字段类型生成

上级 64847f23
/* (C) 2022 YiRing, Inc. */ /* (C) 2022 YiRing, Inc. */
package com.yiring.common.domain; package com.yiring.common.domain;
import com.yiring.common.snowflake.GenerateStringId; import com.yiring.common.snowflake.SnowflakeIdGenerator;
import com.yiring.common.snowflake.SnowflakeId; import jakarta.persistence.Column;
import jakarta.persistence.*; import jakarta.persistence.EntityListeners;
import jakarta.persistence.Id;
import jakarta.persistence.MappedSuperclass;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import lombok.*; import lombok.*;
import lombok.experimental.FieldDefaults; import lombok.experimental.FieldDefaults;
import lombok.experimental.FieldNameConstants; import lombok.experimental.FieldNameConstants;
import lombok.experimental.SuperBuilder; import lombok.experimental.SuperBuilder;
import org.hibernate.annotations.*; import org.hibernate.annotations.Comment;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.ParamDef;
import org.springframework.data.annotation.CreatedBy; import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate; import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy; import org.springframework.data.annotation.LastModifiedBy;
...@@ -40,8 +45,7 @@ public class BasicEntity { ...@@ -40,8 +45,7 @@ public class BasicEntity {
@Comment("主键") @Comment("主键")
@Id @Id
@GeneratedValue(generator = SnowflakeId.GENERATOR) @SnowflakeIdGenerator
@GenericGenerator(name = SnowflakeId.GENERATOR, type = GenerateStringId.class)
String id; String id;
@Comment("创建人") @Comment("创建人")
......
/* (C) 2023 YiRing, Inc. */
package com.yiring.common.snowflake;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import java.io.Serial;
import java.io.Serializable;
import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;
import org.springframework.stereotype.Component;
/**
* 基于雪花算法的 ID 生成器
* 生成 Long 类型
*
* @author ifzm
* @version 0.1
* 2020/1/14 16:18
*/
@Component
public class GenerateLongId implements IdentifierGenerator {
@Serial
private static final long serialVersionUID = 5262354019320675421L;
private Snowflake snowflake;
@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
return snowflake.nextId();
}
@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
snowflake = IdUtil.getSnowflake();
}
}
/* (C) 2023 YiRing, Inc. */
package com.yiring.common.snowflake;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import java.io.Serial;
import java.io.Serializable;
import java.util.Properties;
import org.hibernate.HibernateException;
import org.hibernate.MappingException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.type.Type;
import org.springframework.stereotype.Component;
/**
* 基于雪花算法的 ID 生成器
* 生成 String 类型
*
* @author ifzm
* @version 0.1
* 2020/1/14 16:18
*/
@Component
public class GenerateStringId implements IdentifierGenerator {
@Serial
private static final long serialVersionUID = 2874005990846580098L;
private Snowflake snowflake;
@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
return snowflake.nextIdStr();
}
@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
snowflake = IdUtil.getSnowflake();
}
}
/* (C) 2023 YiRing, Inc. */
package com.yiring.common.snowflake;
import lombok.experimental.UtilityClass;
/**
* 雪花 ID 生成器常量
*
* @author ifzm
* @version 0.1
* 2020/8/10 15:35
*/
@SuppressWarnings("unused")
@UtilityClass
public class SnowflakeId {
public final String GENERATOR = "snowflake";
public class Strategy {
public static final String STRING = "com.yiring.common.snowflake.GenerateStringId";
public static final String LONG = "com.yiring.common.snowflake.GenerateLongId";
}
}
/* (C) 2024 YiRing, Inc. */
package com.yiring.common.snowflake;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import org.hibernate.annotations.IdGeneratorType;
import org.hibernate.annotations.ValueGenerationType;
/**
* 雪花 ID 生成器
* <p>
* 支持自动识别 Long 和 String 类型的 ID 生成
*
* @author Jim
*/
@IdGeneratorType(SnowflakeIdGeneratorAccomplish.class)
@ValueGenerationType(generatedBy = SnowflakeIdGeneratorAccomplish.class)
@Retention(RUNTIME)
@Target({ FIELD, METHOD })
public @interface SnowflakeIdGenerator {
}
/* (C) 2024 YiRing, Inc. */
package com.yiring.common.snowflake;
import static org.hibernate.generator.EventTypeSets.INSERT_ONLY;
import static org.hibernate.internal.util.ReflectHelper.getPropertyType;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import java.io.Serial;
import java.lang.reflect.Member;
import java.util.Arrays;
import java.util.EnumSet;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.generator.BeforeExecutionGenerator;
import org.hibernate.generator.EventType;
import org.hibernate.generator.EventTypeSets;
import org.hibernate.generator.GeneratorCreationContext;
import org.hibernate.id.factory.spi.CustomIdGeneratorCreationContext;
/**
* @author Jim
*/
@SuppressWarnings({ "unused" })
public class SnowflakeIdGeneratorAccomplish implements BeforeExecutionGenerator {
@Serial
private static final long serialVersionUID = 245241369672012904L;
private final Snowflake snowflake;
private final Class<?> type;
private SnowflakeIdGeneratorAccomplish(SnowflakeIdGenerator config, Member idMember) {
final Class<?> propertyType = getPropertyType(idMember);
if (!Arrays.asList(String.class, Long.class).contains(propertyType)) {
throw new HibernateException(
"Unanticipated return type [" + propertyType.getName() + "] for SnowflakeId conversion"
);
}
snowflake = IdUtil.getSnowflake();
type = propertyType;
}
public SnowflakeIdGeneratorAccomplish(
SnowflakeIdGenerator config,
Member idMember,
CustomIdGeneratorCreationContext creationContext
) {
this(config, idMember);
}
public SnowflakeIdGeneratorAccomplish(
SnowflakeIdGenerator config,
Member member,
GeneratorCreationContext creationContext
) {
this(config, member);
}
/**
* @return {@link EventTypeSets#INSERT_ONLY}
*/
@Override
public EnumSet<EventType> getEventTypes() {
return INSERT_ONLY;
}
@Override
public Object generate(
SharedSessionContractImplementor session,
Object owner,
Object currentValue,
EventType eventType
) {
if (type == String.class) {
return snowflake.nextIdStr();
} else {
return snowflake.nextId();
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论