简体   繁体   English

休眠保存复杂实体

[英]Hibernate Saving complex entity

I have a relatively complex entity. 我有一个相对复杂的实体。 Something like this: 像这样:

@Entity
public class MyEntity {

/// some fields
///...

@OneToMany(/*cascade = CascadeType.ALL, */fetch = FetchType.EAGER)
@Fetch(FetchMode.SELECT)
@Cascade({CascadeType.ALL})
protected Set<ParameterValue> parameterValues 

//...
}

@Entity
public class ParameterValue {

// ...
 @ManyToOne(fetch = FetchType.EAGER)
 @Cascade({org.hibernate.annotations.CascadeType.MERGE,     org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.REFRESH})
 private Parameter  parameter

}

MyEntity has String id, ParameterValue has generated long Id and Parameter has string id MyEntity具有字符串ID,ParameterValue已生成长ID,并且参数具有字符串ID

My Entity has parameter values, each of whose has parameter, which is shared among other parameter values from different entities 我的实体具有参数值,每个参数值都有参数,这些值在不同实体的其他参数值之间共享

Parameter is abstract class with different implementations 参数是具有不同实现的抽象类

My problem is that when I call hibernate session saveOrUpdate for such objects 1) it is very slow 2) sometimes I receive org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.xxx.entities.content.EnumParameter#-1672482954] 我的问题是,当我为此类对象调用休眠会话saveOrUpdate时1)它非常慢2)有时我收到org.hibernate.NonUniqueObjectException:具有相同标识符值的另一个对象已与该会话相关联:[com.xxx。 Entitys.content.EnumParameter#-1672482954]

What is correct way to save/update such entities ? 保存/更新此类实体的正确方法是什么?

I inherited the schema from previous developer, so if it is required to simplify saving, I can change it 我继承了以前的开发人员的架构,因此如果需要简化保存,可以对其进行更改

well, transitioning to long unique IDs that are proper primary key (unique, indexed) in database can speed up updates significantly. 好的,过渡到数据库中适当的主键(唯一,索引)的long唯一ID可以大大加快更新速度。

depending on what you're updating, getting rid of eager fetch is another way of speeding things up - maybe you only need to update some ParamValue in some MyEntity ? 根据您要更新的内容,摆脱急切的获取是加快处理速度的另一种方法-也许您只需要更新某些MyEntity ParamValue Retrieve just that ParamValue and store it after modification ( select p from ParamValue p where p.entity=:entity ). 仅检索该ParamValue并在修改ParamValue其存储( select p from ParamValue p where p.entity=:entity )。

It really depends on what your code actually does, but those two things (lazy loading and proper unique keys) will speed things up - though lazy loading might need reviewing some code. 这实际上取决于您的代码实际执行的操作,但是这两件事(延迟加载和适当的唯一键)将加快速度-尽管延迟加载可能需要查看一些代码。

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

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