我遇到了Hibernate的问题。问题是某些查询执行时间太长。 我发现其中一个实体"客户"有一个关系@OneToOne到另一个实体"地址" - 对于不同类型的地址:" managementAddress"," correspondenceAddress"和#34; buildingAddress"。在"地址"的实体中,与"客户"没有任何关系。实体。我发现这些关系的缓慢加载。 急切地初始化@OneToOne关系。我不允许更改模型关系。有谁知道如何提高执行速度?感谢。
这就是在"客户"中声明关系的方式。实体:
@OneToOne(optional=true)
@Cascade(org.hibernate.annotations.CascadeType.ALL)
private Address managementAddress;
这是来自Hibernate统计数据的示例,用于加载50"客户端"实体:
7248515 nanoseconds spent acquiring 51 JDBC connections;
2583681 nanoseconds spent releasing 51 JDBC connections;
43894536 nanoseconds spent preparing 203 JDBC statements;
4106621728 nanoseconds spent executing 203 JDBC statements;
0 nanoseconds spent executing 0 JDBC batches;
0 nanoseconds spent performing 0 L2C puts;
0 nanoseconds spent performing 0 L2C hits;
0 nanoseconds spent performing 0 L2C misses;
答案 0 :(得分:0)
仅在需要时将FetchType.LAZY
标记为延迟加载地址。
@OneToOne(optional=true, fetch=FetchType.LAZY)
@Cascade(org.hibernate.annotations.CascadeType.ALL)
private Address managementAddress;