fix id生成: 存在id则不覆盖

V0.5.x
jay 2023-06-07 11:55:34 +08:00
parent 4a216306a2
commit 77f657df32
1 changed files with 14 additions and 0 deletions

View File

@ -2,6 +2,9 @@ package cc.iotkit.data.config.id;
import cc.iotkit.common.utils.SnowflakeIdGeneratorUtil;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Objects;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
@ -18,6 +21,17 @@ public class SnowflakeIdGenerator implements IdentifierGenerator {
@Override
public Serializable generate(SharedSessionContractImplementor sharedSessionContractImplementor, Object o) throws HibernateException {
Field id = null;
try {
id = o.getClass().getDeclaredField("id");
id.setAccessible(true);
Object val = id.get(o);
if (Objects.nonNull(val)){
return (Serializable) val;
}
} catch (NoSuchFieldException | IllegalAccessException e) {
}
return SnowflakeIdGeneratorUtil.getInstanceSnowflake().nextId();
}
}