package com.jiayue.ipfcst.common.data.entity.id;

import com.jiayue.ipfcst.common.data.id.IdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;

import java.io.Serializable;

/**
 * uuid类型主键工具类，实体主键有值的时候，保存实体会保存主键的值
 *
 * @author bizy
 * @version 3.0
 */
@Slf4j
public class CustomUUIDGenerator extends IdGenerator {
    @Override
    public Serializable generate(SharedSessionContractImplementor s, Object obj) throws HibernateException {
        Serializable id = s.getEntityPersister(null, obj).getClassMetadata().getIdentifier(obj, s);
        if (id != null && !id.toString().isEmpty()) {
        } else {
            id=super.generate(s, obj);
            log.debug("新生成的uuid="+id.toString());
        }
	    return id;
    }
}
