We are using old Hibernate on our project (version 3.6.10), upgrade is not an option at this moment unfortunately.
I'm trying to save entities with assigned ids that have the following structure:
I have a Base class that contains id field. Please note generator-class="assigned"
public abstract class Base {
private Long id;
// contructor
/**
* @hibernate.id generator-class="assigned" unsaved-value="null"
*/
public Long getId() {
return id;
}
// setter
}
Then I have a class Nested
public class Nested extends Base {
private Main main;
// constructor
/**
* @hibernate.many-to-one column="main_id" not-null="true" fetch="join"
* lazy="false" cascade="all"
* class="Main"
*/
public Main getMain() {
return main;
}
// setter
}
And class Main:
public class Main extends Base {
private List<Nested> nested;
/**
* @hibernate.bag cascade="all-delete-orphan" inverse="true" batch-size="10" lazy="false"
* @hibernate.key column="main_id" on-delete="cascade"
* @hibernate.one-to-many class="Nested"
*/
public List<Nested> getNested() {
return nested;
}
// setter
}
When I construct Main object with a list of Nested object (all of them have ids) and try to save it (getHibernateTemplate().save(main)), I see in generated SQL queries:
insert into main...
and then
update nested ... I assume that's because nested records have ids already.
How can I make Hibernate insert records for Nested instances?
I know that I can save each entity separately, but I'd prefer to avoid that.
If you don't want to save each Nested entity separately, you could use a custom interceptor to modify the Hibernate behaviour for saving the entities.
Below is a sample implementation that could be used for the same
This approach involves intercepting saving process for Main entities using custom interceptor. When a Main entity is saved, the interceptor will manually save each associated Nested entity before proceeding with the normal process for Saving Main entity.
P.S.: This solution can affect the behaviour of the Hibernate session globally. As always, make sure to thoroughly test the change to make sure it works correctly for your specific use case.