Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Spring数据Jpa存储库中填充@Transient字段_Spring_Spring Data Jpa - Fatal编程技术网

在Spring数据Jpa存储库中填充@Transient字段

在Spring数据Jpa存储库中填充@Transient字段,spring,spring-data-jpa,Spring,Spring Data Jpa,是否可以以某种方式(通过投影或其他方式)用Spring数据REST api填充实体类中的瞬态字段,以便在JSON响应中获得该值?例如,我需要用从第二个数据源获得的值填充info字段(我有这个数据源的SpringRepoBean,需要将它注入类似“interceptor”的内容并填充该字段) @实体 公共类用户{ @身份证 私人长id; @短暂的 私有字符串信息; //接球手和接球手 } 公共接口用户存储库扩展了JpaRepository{ } 我找到了使用的解决方案,但它是针对Hibernate

是否可以以某种方式(通过投影或其他方式)用Spring数据REST api填充实体类中的瞬态字段,以便在JSON响应中获得该值?例如,我需要用从第二个数据源获得的值填充
info
字段(我有这个数据源的SpringRepoBean,需要将它注入类似“interceptor”的内容并填充该字段)

@实体
公共类用户{
@身份证
私人长id;
@短暂的
私有字符串信息;
//接球手和接球手
}
公共接口用户存储库扩展了JpaRepository{
}
我找到了使用的解决方案,但它是针对Hibernate的,不完全是我想要的,而是有效的。我认为这应该是更普遍的解决办法

@Component
public class UserInterceptor implements PostLoadEventListener {

    @Autowired
    private SecondRepository repo;

    @Autowired
    @Qualifier("prmiaryEntityManagerFactory")
    private EntityManagerFactory entityManagerFactory;

    @PostConstruct
    private void init() {
        HibernateEntityManagerFactory hibernateEntityManagerFactory = (HibernateEntityManagerFactory) this.entityManagerFactory;
        SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) hibernateEntityManagerFactory.getSessionFactory();
        EventListenerRegistry registry = sessionFactoryImpl.getServiceRegistry().getService(EventListenerRegistry.class);
        registry.appendListeners(EventType.POST_LOAD, this);
    }   

    @Override
    public void onPostLoad(PostLoadEvent event) {
        final Object entity = event.getEntity();
        if(entity != null && entity instanceof User) {
            User user = (User) entity;

            // populate using another repo bean
            Info s = repo.findOne(user.getInfoId());
            user.setInfo(s.getName());
        }
    }

}

您希望如何填充它?暂时性意味着它不仅不会在数据库中持久化,而且字段仍然可用
@Component
public class UserInterceptor implements PostLoadEventListener {

    @Autowired
    private SecondRepository repo;

    @Autowired
    @Qualifier("prmiaryEntityManagerFactory")
    private EntityManagerFactory entityManagerFactory;

    @PostConstruct
    private void init() {
        HibernateEntityManagerFactory hibernateEntityManagerFactory = (HibernateEntityManagerFactory) this.entityManagerFactory;
        SessionFactoryImpl sessionFactoryImpl = (SessionFactoryImpl) hibernateEntityManagerFactory.getSessionFactory();
        EventListenerRegistry registry = sessionFactoryImpl.getServiceRegistry().getService(EventListenerRegistry.class);
        registry.appendListeners(EventType.POST_LOAD, this);
    }   

    @Override
    public void onPostLoad(PostLoadEvent event) {
        final Object entity = event.getEntity();
        if(entity != null && entity instanceof User) {
            User user = (User) entity;

            // populate using another repo bean
            Info s = repo.findOne(user.getInfoId());
            user.setInfo(s.getName());
        }
    }

}