Hibernate 部分更新(如何?)
我的休眠实现存在性能问题,其性能成本很高。 我将尝试解释我当前的实现,必须使用伪类对其进行改进。 假设我有以下 POJO 类(实体类是 hibernate 注释的“副本”)。
Country.java and CountryEntity.java
City.javaand CityEntity.java
Inhabitant.javaand InhabitantEntity.java
我想将一个城市添加到一个国家并将其保存/保留在数据库中,新城市作为 POJO 完全填充。 当前代码
CountryEntity countryEntity = CountryDao.fetch(someId);
Country country = CountryConverter(countryEnity);
country.getCities.add(newCity);
countryEnity = CountryEntityConverter(country);
CountryDao.save(countryEnity);
这会导致主要的性能问题。假设我有 200 个城市,有 10,000 名居民。 对于我添加一个新城市,转换器将转换 200 x 10,000 = 2,000,000 inhabitantEntity -->居民-->居民实体 由于经常添加新城市,这会给服务器带来巨大的负载。 人们也觉得没有必要为了坚持和连接另一个城市而改变该国的所有城市。
我正在考虑创建一个轻型转换器,它不会转换所有字段,而只会转换在添加城市期间一些业务逻辑所需的字段,但这些字段将保持不变,我不知道 Hibernate 是否足够好来处理这种情况。 例如,如果我保存一个包含大量空字段的实体和只有一个城市的列表城市,我可以告诉 hibernate 将其与数据库合并在一起吗? 或者我可以采取不同的方法来解决性能问题,但将 POJO 和实体分开?
下面的一些代码显示了我当前的“慢速”实现代码。
Country.Java (pseudo code)
private fields
private List<City> cities;
City.Java (pseudo code)
private fields
private List<Inhabitant> inhabitants;
Inhabitant.Java (pseudo code)
private fields
目前我通过 Dao java 类获取 CountryEnity。 然后我有转换器类(实体 --> POJO),用于设置所有字段并启动所有列表。 我也有类似的转换器类转换(POJO --> 实体)。
CountryConverter(countryEntity)
Country country = new Country();
Country.setField(countryEntity.getField())
Loop thru cityEnitites
Country.getCities.add(CityConverter(cityEntity))
return country
CityConverter(cityEntity)
City city = new City()
city.setField(cityEntity.getField())
Loop thru inhabitantEnitites
city.getInhabitants.add(InhabitantConverter(inhabitantEntity))
return country
InhabitantConverter(inhabitantEntity)
Inhabitant inhabitant = new Inhabitant()
inhabitant.setField(inhabitantEntity.getField())
return inhabitant
预先感谢/Farmor
I have a performance problem with a hibernate implementation that is far to performance costly.
I will try to explain my current implementation which must be improved upon with pseudo classes.
Let’s say I have the following POJO classes (the Entity classes are hibernate annotated "copies").
Country.java and CountryEntity.java
City.javaand CityEntity.java
Inhabitant.javaand InhabitantEntity.java
And I want to add a city to a country and save/persist it in the database, the new city arrives fully populated as a POJO.
Current code
CountryEntity countryEntity = CountryDao.fetch(someId);
Country country = CountryConverter(countryEnity);
country.getCities.add(newCity);
countryEnity = CountryEntityConverter(country);
CountryDao.save(countryEnity);
This results in a major performance problem. Let's say I have 200 cities with 10,000 inhabitants.
For me to add a new city the converter will convert 200 x 10,000 = 2,000,000 inhabitantEntity --> inhabitant --> inhabitantEntity
This puts a tremendous load on the server, as new cities are added often.
It also feels unnecessary to convert all cities in the country just to persist and connect another one.
I am thinking of creating a light converter which doesn't convert all the fields and only the ones I need for some business logic during the addition of the city, but those will be kept unchanged, I don't know if Hibernate is good enough to handle this scenario.
For example if I save an entity with alot of null fields and the list cities with only one city, can I tell hibernate to merge this together with the db.
Or is there a different approace I can take to solve the performance problem but keeping the POJO and Entitys separate?
Some code below showing my current "slow" implementation code.
Country.Java (pseudo code)
private fields
private List<City> cities;
City.Java (pseudo code)
private fields
private List<Inhabitant> inhabitants;
Inhabitant.Java (pseudo code)
private fields
Currently I fetch a CountryEnity thru a Dao java class.
Then I have converter classes (Entities --> POJO) that sets all fields and initiate all lists.
I also have similar converter classes converting (POJO --> Entities).
CountryConverter(countryEntity)
Country country = new Country();
Country.setField(countryEntity.getField())
Loop thru cityEnitites
Country.getCities.add(CityConverter(cityEntity))
return country
CityConverter(cityEntity)
City city = new City()
city.setField(cityEntity.getField())
Loop thru inhabitantEnitites
city.getInhabitants.add(InhabitantConverter(inhabitantEntity))
return country
InhabitantConverter(inhabitantEntity)
Inhabitant inhabitant = new Inhabitant()
inhabitant.setField(inhabitantEntity.getField())
return inhabitant
Thanks in advance /Farmor
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我怀疑可能发生的情况是关联上没有索引列,因此 Hibernate 正在删除然后插入子集合,而不是仅向子关联添加或删除离散对象。
如果确实发生了这种情况,您可以尝试向子关联的 get 方法添加
@IndexColumn
注释。这将允许 Hibernate 对关联记录执行离散插入、更新和删除,而不是必须删除然后重新插入。然后,您将能够插入新城市及其新居民,而无需重建一切。I suspect what might be happening is that you don't have an index column on the association, so Hibernate is deleting and then inserting the child collection, as opposed to just adding to or deleting discrete objects to and from the child association.
If that is what's going on, you could try adding an
@IndexColumn
annotation to the get method for the child association. That will then allow Hibernate to perform discrete inserts, updates, and deletes on association records, as opposed to having to delete and then re-insert. You would then be able to insert the new city and its new inhabitants without having to rebuild everything.