Hibernate统计语句计数不准确

3

我有一个如下的双向一对多关系。在我的测试中,我插入了一个父实体和3个子实体。然后我统计了准备好的语句数量。Hibernate告诉我是6个,但在日志中我只看到了4个插入语句被记录。为什么会发生这种情况?

public class Order {
    @Id
    private Long id;

    @OneToMany(mappedBy = "order", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Line> lines = new ArrayList<>();

    public void addLine(Line line) {
        lines.add(line);
        line.setOrder(this);
    }
}

public class Line {
    @Id
    private Long id;

    @ManyToOne
    private Order order;
}

测试

@Test
    public void test() {
        Statistics stat = entityManager.unwrap(Session.class).getSessionFactory().getStatistics();
        Line line1 = Line.builder().lineNbr("1").build();
        Line line2 = Line.builder().lineNbr("2").build();
        Line line3 = Line.builder().lineNbr("3").build();
        Order order = Order.builder().orderNbr("1").lines(new ArrayList<>()).build();
        order.addLine(line1);
        order.addLine(line2);
        order.addLine(line3);

        repo.saveAndFlush(order);
        assertEquals(4, stat.getEntityInsertCount());
        assertEquals(4, stat.getPrepareStatementCount()); // this fails. Hibernate says it's 6
    }

这是我在日志中看到的内容:
Hibernate: insert into orders (order_nbr, id) values (?, ?)
Hibernate: insert into line (line_nbr, order_id, id) values (?, ?, ?)
Hibernate: insert into line (line_nbr, order_id, id) values (?, ?, ?)
Hibernate: insert into line (line_nbr, order_id, id) values (?, ?, ?)

927500 nanoseconds spent preparing 6 JDBC statements;
2732200 nanoseconds spent executing 4 JDBC statements;
2118000 nanoseconds spent executing 2 JDBC batches; -------- what is this?
1个回答

0
首先,Line插入被视为普通语句,接下来的两个被视为批量插入,即它们重复使用相同的语句或使用JDBC批处理。不确定它们是否仍然被视为预编译语句,但我认为是这样的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接