Hibernate / JPA中注释字段或getter方法之间的性能差异 [英] Performance difference between annotating fields or getter methods in Hibernate / JPA

查看:96
本文介绍了Hibernate / JPA中注释字段或getter方法之间的性能差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,如果有人使用私有字段而不是公共getter方法来注释实体之间的性能差异,那么是否有任何硬编码。我听说有人说字段比较慢,因为它们被称为通过反射,但是再一次,getter方法也是如此,不是吗?在尝试读取它之前,Hibernate需要将字段的可访问性设置为true,我可以看到有一些轻微的开销。然而,这不是在会话范围内的Class级别完成的,或者只是在Configuration被读取并构建SessionFactory时才会完成?



只是好奇而已这是一个神话,或者如果真的有真相,我个人发现注释字段的可读性更高一些。

解决方案

加载5000记录到一个简单的3列表中。将两个类映射到该表,一个使用带注释的私有字段,另一个使用带注释的公共获得者。运行Spring的HibernateTemplate.loadAll(),然后运行HibernateTemplate.clear()以清除会话缓存。结果在ms以下...



方法总数:6510,平均值:217

字段总数:6586,平均:219

在给每个班级增加更多属性之后,我应该再次尝试一下,但现在这种差异在统计上并不显着。


I was curious if anyone had any hard numbers around the performance difference between annotating Entities using private fields instead of public getter methods. I've heard people say that fields are slower because they're called "through reflection" but then again so are the getter methods, no? Hibernate needs to set the accessibility of the field to true before it tries to read it which I can see having some slight overhead. However wouldn't that be done at the Class-level in the scope of a Session or perhaps only once when the Configuration is read and SessionFactory is built?

Just curious if this is a myth or if there's really truth to it; I personally find annotating the fields to be a bit more readable.

解决方案

Loaded 5000 records into a simple 3 column table. Mapped two classes to that table, one using annotated private fields and another using annotated public getters. Ran 30 runs of Spring's HibernateTemplate.loadAll() followed by a HibernateTemplate.clear() to purge the Session cache. Results in ms below...

methods total: 6510, average: 217

fields total: 6586, average: 219

I should probably take another stab at it after adding more properties to each class but right now the difference doesn't appear to be statistically significant.

这篇关于Hibernate / JPA中注释字段或getter方法之间的性能差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆