自定义JPA主键生成策略实现保存时允许自定义ID

自定义JPA主键生成策略实现保存时允许自定义ID

参考: https://www.jianshu.com/p/db5f10b546df

 1 public class CustomUUIDGenerator extends UUIDGenerator {
 2     @Override
 3     public Serializable generate(SessionImplementor session, Object object) throws HibernateException {
 4         if (object == null){
 5             throw new HibernateException(new NullPointerException());
 6         }
 7         String id = ((CarLocationLevel) object).getId();
 8         if (StringUtils.isBlank(id)) {
 9             return super.generate(session, object).toString().replaceAll("-", "") ;
10         } else {
11             return id;
12         }
13     }
14 }

猜你喜欢

转载自www.cnblogs.com/dx125135-wuxj/p/12916297.html