fix id生成: 存在id则不覆盖
parent
4a216306a2
commit
77f657df32
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue