Spring整合Jpa |在Java dsl中使用Jpa插入行

时间:2019-02-20 14:47:10

标签: spring-integration spring-integration-dsl

我在互联网上进行了很多搜索,但无法找到以下内容:

使用$top5CatWithVideos = Category::withCount('video')->with(['video' => function($q){ return $q->take(5); }, 'video.user'])->->orderBy('video_count', 'desc')->limit(5)->get(); 插入行的具体示例。

JpaUpdatingOutboundEndpointSpec

以上足够了吗?

请帮助我。

1 个答案:

答案 0 :(得分: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());
}

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