简体   繁体   English

Spring 数据 JpaRepository saveAndFlush 在@Transactional 测试方法中不起作用

[英]Spring Data JpaRepository saveAndFlush not working in @Transactional test method

Using JPA Spring Data I have created a couple of entities Ebook and Review, which are linked by a ManyToOne relationship from Review towards Ebook.使用 JPA Spring 数据我创建了几个实体 Ebook 和 Review,它们通过从 Review 到 Ebook 的 ManyToOne 关系链接。 I have tried to insert a review for an existent ebook, but the result is not being reflected in the database (Postgres DB), even though I am using a saveAndFlush inside a @Transactional.我试图为现有的电子书插入评论,但结果没有反映在数据库(Postgres DB)中,即使我在@Transactional 中使用了 saveAndFlush。 I retrieve inserted review correctly, it is just that is not saving the record in the table.我正确检索插入的评论,只是没有将记录保存在表中。

Entities:实体:

Ebook:电子书:

@Entity
public class Ebook {

    @Id
    @SequenceGenerator(
            name="ebook_sequence",
            sequenceName = "ebook_sequence",
            allocationSize = 1
    )
    @GeneratedValue (
            strategy = GenerationType.SEQUENCE,
            generator ="ebook_sequence"
    )
    private Long idEbook;
    private String title;
}

Review:审查:

@Entity
public class Review {
    @Id
    @SequenceGenerator(
            name="sequence_review",
            sequenceName = "sequence_review",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.SEQUENCE,
            generator = "sequence_review"
    )
    private Long idReview;
    private String reviewText;

    //ManytoOne Ebook
    @ManyToOne(
            cascade = CascadeType.ALL,
            fetch = FetchType.LAZY
    )
    @JoinColumn(
            name = "id_ebook",
            referencedColumnName = "idEbook"
    )
    private Ebook ebook;
}

And the test in which I am trying to insert just a review corresponding to a preexisting ebook record:而我试图插入与预先存在的电子书记录相对应的评论的测试:

    @Test
    @Transactional
    public void saveReviewWithEbook() {

        Ebook ebook = ebookRepository.getById((long)1);

        Review review = Review.builder()
                .reviewText("review text ebook 1")
                .ebook(ebook)
                .build();

        Review reviewInserted = reviewRepository.saveAndFlush(review); //saveAndFlush(review);
        Review reviewLoaded = reviewRepository.getById(reviewInserted.getIdReview());
        System.out.println("reviewLoaded = " + reviewLoaded);
    }

After executing it, there is no record inserted in table 'review', although data appear correctly in variable 'reviewLoaded'.执行后,表“review”中没有插入记录,尽管数据在变量“reviewLoaded”中正确显示。 Why is that?这是为什么? How can I save just a review corresponding to an existing ebook?如何仅保存与现有电子书相对应的评论?

The transaction which is opened for the test is rolled back at the end of the test method.为测试打开的事务在测试方法结束时回滚。

This can be disabled:这可以禁用:

@Rollback(false) // <-- !
@Test   
public void myTest()

Spring boot test @Transactional not saving Spring 开机测试@Transactional 不保存

Also note that the ORM (eg Hibernate) will probably have some caching in place, which can prevent actually hitting the DB when querying within a transaction for a known entity.另请注意,ORM(例如 Hibernate)可能会有一些缓存,这可以防止在事务中查询已知实体时实际命中数据库。

So to test the successful persistence of entities it is good practice to test the retrieval in a separated transaction.因此,要测试实体的成功持久性,最好在单独的事务中测试检索。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM