【问题标题】:Perfomance in Hibernate 4.2 with @OneToMany - Unidirectional vs Bidirectional使用 @OneToMany 在 Hibernate 4.2 中的性能 - 单向与双向
【发布时间】:2015-12-20 19:11:37
【问题描述】:

我只需要一个单向关系 OneToMany,但在性能方面我想问什么是最佳实践?

我用junit做了一个测试,双向的结果是:1.9s,单向码2.4s,看来单向需要更多时间

我使用的是 Hibernate 4.2.21

@单向2.4s

Parent.class

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "parentuni_id")  
private Set<ChildUni> childs;

Hibernate: select nextval ('parentuni_seq')
Hibernate: select nextval ('childuni_seq')
Hibernate: select nextval ('childuni_seq')
Hibernate: insert into parentuni (parentuni_name, parentuni_id) values (?, ?)
Hibernate: insert into childuni (childuni_name, childuni_id) values (?, ?)
Hibernate: insert into childuni (childuni_name, childuni_id) values (?, ?)
Hibernate: update childuni set parentuni_id=? where childuni_id=?
Hibernate: update childuni set parentuni_id=? where childuni_id=?

@双向 1.9s

Parent.class

@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "parent")
private Set<Child> childs;

Child.class

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "parent_id", nullable = false)
private Parent parent;

Hibernate: select nextval ('parent_seq')
Hibernate: select nextval ('child_seq')
Hibernate: select nextval ('child_seq')
Hibernate: insert into parent (parent_name, parent_id) values (?, ?)
Hibernate: insert into Child (child_name, parent_id, child_id) values (?, ?, ?)
Hibernate: insert into Child (child_name, parent_id, child_id) values (?, ?, ?)

【问题讨论】:

    标签: java hibernate


    【解决方案1】:

    Hibernate best practices 中,它说

    首选双向关联:

    • 单向关联更难查询。在大型应用程序中,几乎所有关联都必须在查询中双向导航。

    有用的来源:http://www.javacodegeeks.com/2011/02/hibernate-mapped-collections.html

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-27
      • 1970-01-01
      • 2013-08-19
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 2021-10-20
      • 2016-05-16
      相关资源
      最近更新 更多