【问题标题】:Spring integration Jpa | To insert row with Jpa in java dslSpring 集成 Jpa |在 java dsl 中使用 Jpa 插入行
【发布时间】:2019-02-20 14:47:09
【问题描述】:

我在互联网上搜索了很多,但无法找到以下句柄:

使用JpaUpdatingOutboundEndpointSpec 插入一行的具体示例。

@Bean
public JpaUpdatingOutboundEndpointSpec insertToTable() {
    return Jpa.updatingGateway(entityManger)
              .entityClass(EntitySample.class); 
}

以上就够了吗?

请帮助我。

【问题讨论】:

    标签: spring-integration spring-integration-dsl


    【解决方案1】:

    没错,在某些情况下,该代码可能就足够了。从这里很高兴知道您将如何使用此代码。尽管同时我将与您分享我的测试配置和测试本身:

        @Bean
        public IntegrationFlow outboundAdapterFlow(EntityManagerFactory entityManagerFactory) {
            return f -> f
                    .handle(Jpa.outboundAdapter(entityManagerFactory)
                                    .entityClass(StudentDomain.class)
                                    .persistMode(PersistMode.PERSIST),
                            e -> e.transactional(true));
        }
    

    ...

    @Autowired
    @Qualifier("outboundAdapterFlow.input")
    private MessageChannel outboundAdapterFlowInput;
    
    @Test
    public void testOutboundAdapterFlow() {
        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
    
        List<?> results1 = jdbcTemplate.queryForList("Select * from Student");
        assertNotNull(results1);
        assertTrue(results1.size() == 3);
    
        Calendar dateOfBirth = Calendar.getInstance();
        dateOfBirth.set(1981, 9, 27);
    
        StudentDomain student = new StudentDomain()
                .withFirstName("Artem")
                .withLastName("Bilan")
                .withGender(Gender.MALE)
                .withDateOfBirth(dateOfBirth.getTime())
                .withLastUpdated(new Date());
    
        assertNull(student.getRollNumber());
    
        this.outboundAdapterFlowInput.send(MessageBuilder.withPayload(student).build());
    
        List<?> results2 = jdbcTemplate.queryForList("Select * from Student");
        assertNotNull(results2);
        assertTrue(results2.size() == 4);
    
        assertNotNull(student.getRollNumber());
    }
    

    您可以在框架的测试类中找到更多关于 Spring Integration Java DSL for JPA 的测试:https://github.com/spring-projects/spring-integration/blob/master/spring-integration-jpa/src/test/java/org/springframework/integration/jpa/dsl/JpaTests.java

    【讨论】:

    • 感谢 Artem 的快速转身!所以这需要像 FTPOutboundAdapter 一样的成功和异常的建议,对吗?
    • 是的,没错。您也可以在此 .handle() 中为 JPA 使用 ExpressionEvaluatingRequestHandlerAdvice
    猜你喜欢
    • 1970-01-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-06
    • 2014-11-14
    • 2015-06-09
    • 1970-01-01
    相关资源
    最近更新 更多