简体   繁体   English

Hibernate 4.2中具有@OneToMany的性能-单向与双向

[英]Perfomance in Hibernate 4.2 with @OneToMany - Unidirectional vs Bidirectional

I need only a unidirectional relationship OneToMany, but in Terms of performance I want to ask what is the best practice?. 我只需要一个单向关系OneToMany,但是就性能而言,我想问一下什么是最佳实践?

I did a test with Junit, and the result with bidirectional is: 1.9s and the unidirectional code 2.4s, it seems for unidirectional needs more time 我对Junit进行了测试,双向结果为:1.9s和单向代码为2.4s,看来单向需要更多时间

I'm using Hibernate 4.2.21 我正在使用Hibernate 4.2.21

@Unidirectional 2.4s @单向2.4秒

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=?

@Bidirectional 1.9s @双向1.9秒

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 (?, ?, ?)

In Hibernate best practices , it says Hibernate最佳做法中 ,它说

Prefer bidirectional associations: 首选双向关联:

  • Unidirectional associations are more difficult to query . 单向关联更难以查询 In a large application, almost all associations must be navigable in both directions in queries. 在大型应用程序中,查询中的几乎所有关联都必须在两个方向上都是可导航的。

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

Hope this helps. 希望这可以帮助。

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

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