0

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

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]

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

1 Answer 1

1

well, transitioning to long unique IDs that are proper primary key (unique, indexed) in database can speed up updates significantly.

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? Retrieve just that ParamValue and store it after modification (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.

Sign up to request clarification or add additional context in comments.

4 Comments

Actually I have 2 flows: 1. Inserting new entity, with new ParameterValues (which points to new or old Parameters) 2. Inserting new ParameterValues (which points to new or old Parameters) to existing entities
both of them are slow? or only second one?
Both are slow and in the first one I receive sometimes org.hibernate.NonUniqueObjectException:
inserting new records into database should be fast (no need to look up anything), if primary keys are generated on database side. review your entity objects to have native unique keys <id name="id" type="long"><generator class="native"/></id>. inspect hibernate debug log of actual queries performed when your object is stored - there should only be simple "insert" queries ommitting ID values.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.