• JPA忽略更新空值配置


    import org.hibernate.bytecode.enhance.spi.LazyPropertyInitializer;
    import org.hibernate.engine.spi.SessionImplementor;
    import org.hibernate.event.internal.DefaultMergeEventListener;
    import org.hibernate.persister.entity.EntityPersister;
    import org.hibernate.property.access.internal.PropertyAccessStrategyBackRefImpl;
    import org.hibernate.type.Type;
    
    import java.util.Map;
    
    public class IgnoreNullEventListener extends DefaultMergeEventListener {
     
        public static final IgnoreNullEventListener INSTANCE = new IgnoreNullEventListener();
     
        @Override
        protected void copyValues(EntityPersister persister, Object entity, Object target, SessionImplementor source, Map copyCache) {
            //源目标
            Object[] original = persister.getPropertyValues( entity );
            //存储目标
            Object[] targets = persister.getPropertyValues(target);
     
            Type[] types = persister.getPropertyTypes();
    
            Object[] copied = new Object[original.length];
            for ( int i = 0; i < types.length; i++ ) {
                if ( original[i] == null ||
                        original[i] == LazyPropertyInitializer.UNFETCHED_PROPERTY ||
                        original[i] == PropertyAccessStrategyBackRefImpl.UNKNOWN
                ){
                    copied[i] = targets[i];
                } else {
                    copied[i] = types[i].replace( original[i], targets[i], source, target, copyCache );
                }
            }
            persister.setPropertyValues( target, copied );
        }
    }
    import org.hibernate.event.service.spi.EventListenerRegistry;
    import org.hibernate.event.spi.EventType;
    import org.hibernate.internal.SessionFactoryImpl;
    import org.springframework.context.annotation.Configuration;
    
    import javax.annotation.PostConstruct;
    import javax.persistence.EntityManagerFactory;
    import javax.persistence.PersistenceUnit;
    
    @Configuration
    public class HibernateListenerConfigurer {
        @PersistenceUnit
        private EntityManagerFactory emf;
     
        @PostConstruct
        protected void init() {
            SessionFactoryImpl sessionFactory = emf.unwrap(SessionFactoryImpl.class);
            EventListenerRegistry registry = sessionFactory.getServiceRegistry().getService(EventListenerRegistry.class);
            registry.getEventListenerGroup(EventType.MERGE).clear();
            registry.getEventListenerGroup(EventType.MERGE).prependListener(IgnoreNullEventListener.INSTANCE);
        }
    }
  • 相关阅读:
    一款前端文件上传工具
    聊一聊最近找工作的感受
    神秘的计算机网络----(1)
    月下无限连?拒绝无休止switch!
    计算机网络---序
    验证码识别
    两数之和
    Sanic框架基础之解决CORS跨域
    Sanic框架进阶之实现异步缓存组件
    asyncio异步模块的21个协程编写实例
  • 原文地址:https://www.cnblogs.com/i-tao/p/15590051.html
Copyright © 2020-2023  润新知