JPA ManyToMany bi-directional insert - java

I have problem and i could not find solution. I have bi-directional many to many anotation. I have this tables in DB (mariaDB):
item
section
item_section
Item part:
#ManyToMany(fetch = FetchType.LAZY)
#JoinTable(name = "item_section",
joinColumns = {
#JoinColumn(name = "item", nullable = false, updatable = false)},
inverseJoinColumns = {
#JoinColumn(name = "section", nullable = false, updatable = false)})
private Set<Section> sections;
Section part:
#ManyToMany(fetch = FetchType.EAGER, mappedBy = "sections")
private Set<Item> items;
PROBLEM:
I create some Sections first. Than i want to create Item "within" this section. So i create instance of Item and add some existing Sections into set (Sections i get from DAO). If i persist the Item, records in item and item_section tables are created. But if i get some of affected section from DAO and iterate thru items set database changes are not here, Sections instances are in same state as before insert.
What did i wrong?
PS: i use eclipselink but i do not think it is interesting
SOLUTION
As #Chris said i called persist utimately but one side of direction already exist. No cascades was defined so during persist sections was not persisted nor merge. So best solution for my staless and JTA solution was use merge instead of persist on item and add MERGE cascade to sections collection...
Thanks everybody who came to help but especially to #Chris

It seems you are missing the attribute cascade=CascadeType.ALL. You should try to change
#ManyToMany(fetch = FetchType.LAZY)
#JoinTable(name = "item_section",
joinColumns = {
#JoinColumn(name = "item", nullable = false, updatable = false)},
inverseJoinColumns = {
#JoinColumn(name = "section", nullable = false, updatable = false)})
private Set<Section> sections;
to
#ManyToMany(cascade=CascadeType.ALL,fetch = FetchType.LAZY)
#JoinTable(name = "item_section",
joinColumns = {
#JoinColumn(name = "item", nullable = false, updatable = false)},
inverseJoinColumns = {
#JoinColumn(name = "section", nullable = false, updatable = false)})
private Set<Section> sections;
then it works normally.
I found this mistake via the tutorial Hibernate Many-To-Many Bidirectional Mapping Example
Hope this help

Problem may be in incorrect using of eclipselink's shared cache which is enabled by default. Try to disable it by adding in persistence.xml following:
<shared-cache-mode>NONE</shared-cache-mode>
When you persist or merge Section provider flushes it to database and add Section instance to shared cache so it can be retrieved from memory on next requests. When you persist Item you update only one side of the relationship, but the other side does not get updated, and becomes out of sync. In JPA, as in Java in general, it is the responsibility of the application or the object model to maintain relationships. If your application adds to one side of a relationship, then it must add to the other side. Read more about caching in bi-direction mapping in this article.
I can offer following solutions of your problem:
Disable cache for whole application in persistence.xml
Manually refresh Section in DAO before retrieving it to user
Use cache correctly by updating both sides of relationship:

Related

Multiple Spring JPA relationships between 2 entities

I have 2 entities, one for a taskList and the other for users. The users can create tasks for themselves or their friends, so in the Tasks entity I have different columns referencing the User entity, one for createdBy another for createdFor and a last one for modifiedBy.
In the tasks entity I made the following #ManyToOne JPA relationships:
#ManyToOne(optional = false)
#JoinColumn(name = "created_by", nullable = false,
referencedColumnName = "id")
private User createdBy;
#ManyToOne
#JoinColumn(name = "created_for", nullable = false,
referencedColumnName = "id")
private User createdFor;
#ManyToOne
#JoinColumn(name = "modified_by", nullable = false,
referencedColumnName = "id")
private User modifiedBy;
But I am not sure how to do the linking in the Users side, I had the following #OneToMany relationship:
#OneToMany(mappedBy = "createdFor", cascade = CascadeType.ALL)
private List<Task> taskList;
But I am not sure how to proceed with the other relationships as I don't really need a taskList for the tasks that a user created, or what tasks did he modify.
You do not need to map all things in an Entity , and also do not need to configure every relationships as bi-directional.
So if you really do not need to access the Tasks that are created and modified from a given User , just ignore them and do not map them in User.
It is perfectly valid to configure a relationship as unidirectional. Your existing codes should already work perfectly.

CascadeType.MERGE - Avoiding IllegalStateException: Multiple representations of the same entity are being merged [duplicate]

I have 3 entities with ManyToMany relationships:
Role Entity:
#Entity
public class Role {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer roleID;
private String roleName;
private String description;
#ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.EAGER)
#JoinTable(name = "role_permission", joinColumns = {#JoinColumn(name = "role_id")}, inverseJoinColumns = {#JoinColumn(name = "permission_id")})
private Set<Permission> permissions = new LinkedHashSet<Permission>();
}
Permission Entity:
#Entity
public class Permission {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int permissionID;
private String permissionName;
private String description;
#ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.EAGER)
#JoinTable(name = "permission_functionality", joinColumns = {#JoinColumn(name = "permission_id")}, inverseJoinColumns = {#JoinColumn(name = "functionality_id")})
private Set<Functionality> functionalities = new LinkedHashSet<>();
}
Functionality Entity:
#Entity
public class Functionality {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
}
I did the following:
I have created 3 functionalities:
Functionality1, Functionality2, Functionality3
Then created 2 permissions:
Permission1 with Functionality1, Functionality2
Permission2 with Functionality2, Functionality3
Then created a role:
Role1 with Permission1 and Permission2
I am getting the following exception:
java.lang.IllegalStateException: Multiple representations of the same entity [com.persistence.entity.admin.Functionality#1] are being merged. Detached: [com.persistence.entity.admin.Functionality#4729256a]; Detached: [com.persistence.entity.admin.Functionality#56ed25db]
Fixed it by removing CascadeType.MERGE on Permission entity
The correct solution would have been to upgrade to hibernate 4.2.15 / 4.3.6 or above and add the following lines to your persistence.xml:
<property name="hibernate.event.merge.entity_copy_observer" value="allow"/>
Check your equals and hashCode method, ensure that it is consistent and correctly defined. For example I had copied and mistakenly pasted another-class when computing hashCode, this caused the object never to be equal with itself :(.
Like others who based their answers on HHH-9106 but, because I'm using Spring Boot with Java-based annotation, I had to use the following in application.properties:
spring.jpa.properties.hibernate.event.merge.entity_copy_observer=allow
I ran into the same problem too and solved it by add a few configs in application.yaml files.
jpa:
properties:
hibernate:
enable_lazy_load_no_trans: true
event:
merge:
entity_copy_observer: allow
See it here How to persist a new entity containing multiple identical instances of another unpersisted entity with spring-boot and JPA?
I could fix it by replacing
cascade = CascadeType.All
with
casecade={CascadeType.PERSIST,CascadeType.REMOVE}
For Hibernate see the workaround here HHH-9106.
In my case, moving the fetch operation and save operation in same #Transactional block solved the problem.
error occurs when we have multiple object of same time in HashSet.Possible due to incorrect hash function.Hashset check equality of object on the basis of hash function between two objects.
Way to debug
Just try to print hashset you will see multiple object of same type.
Solution::#
Use HashSet while defining one to many relationships.
Avoid using Lists.
Make sure your hash function should be correct.
I fixed this issue by removing cascade = CascadeType.ALL in your case (cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
My code source:
Before
#ManyToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
#JoinTable(
name = "link_module_parcour",
joinColumns = {#JoinColumn(name = "module_id", referencedColumnName = "id")},
inverseJoinColumns = {#JoinColumn(name = "parcour_id", referencedColumnName = "id")})
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
#BatchSize(size = 20)
private Set<Parcour> parcours = new HashSet<>();
After
#ManyToMany(fetch=FetchType.EAGER)
#JoinTable(
name = "link_module_parcour",
joinColumns = {#JoinColumn(name = "module_id", referencedColumnName = "id")},
inverseJoinColumns = {#JoinColumn(name = "parcour_id", referencedColumnName = "id")})
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
#BatchSize(size = 20)
private Set<Parcour> parcours = new HashSet<>();
**#LazyCollection(LazyCollectionOption.FALSE**
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
#JoinColumn(name = "translate_id")
If you remove the cascade merge you won't be able to update the entity when updating the parent.
The case will be right if you don't want to update the child, but if you want to update the child when updating the parent removing merge will only not show the error but the issue will still exist.
What happened with me: I got the same exception but I need Cascade.merge. After searching I found that some of the values I was using when creating the entity for the first time are updated and removed from the code but in the database, they still exist and when removing them from the database also work as expected.
To clarify the case:
Let's say I have an enum('ACTIVE', 'NOT_ACTIVE', 'SUSPENDED') and the field using this enum is key, when updating the code and removing the NOT_ACTIVE we should alter the database table with the new values enum('ACTIVE', 'SUSPENDED')
I had similar issue and it is resolved when I replace CascadeType.All with {CascadeType.persist, CascadeType.remove}.
Let me know if it works.
We can also resolve that using CascadeType.DETACH . In this case, if we need to update something in Permission, we have to update Permession separately and not by updating the related entity!
Just a note to say I am using Hibernate Core 4.3.8 in a Spring MVC application, based on Spring Core 4.1.6. The workaround:
<property name="hibernate.event.merge.entity_copy_observer" value="allow"/>
Did not work for me. I needed to remove the CascadeType.MERGE in order to correctly populate an #ManyToMany. Not sure if newer versions of Hibernate have fixed this.

JPA merge on auto increment

I have an Entity that has the following multiple fields along with a field that is autogenerated on an insert
Now everytime I want to do an update using merge() in my DAO, it will add a new entry instead of updating the current entry. I am assuming this is due to UPDATE statement on id and the id is new everytime. Is there a way within JPA (Hibernate) to avoid writing a new record in the database and just to an update instead. Please find below my entity object:
#Id
#GeneratedValue(strategy = GenerationType.AUTO)
#Column(name = "CONFIG_ID")
private long configID;
#ManyToOne
#JoinColumns({
#JoinColumn(name = "CID", referencedColumnName = "CID", insertable = true, updatable = false),
#JoinColumn(name = "DID", referencedColumnName = "DID", insertable = true, updatable = false)})
#Version private <Object> dE;
#Column(name = "HISTORY", nullable = false, unique = false)
private String history;
merge() is supposed for detached entity use . It is an update or insert action depending if the identity of the merged object is null or not. If it is null , a new record will be inserted.
To correctly update an entity in JPA , you simply load it and change its properties. When the transaction commit , JPA will figure out if there are any properties changes and issue the UPDATE SQL if there are changes. No need to call any methods on EntityManager for any update.
Reference:
JPA wiki - Merge

Many to Many Not Respecting Update and Insert

I am trying to setup and Many to Many relationship between users and roles. I am mostly using JPA Repositories but I also tried using and EntityManger.
I have the following in my main User object.
#ManyToMany(fetch = FetchType.LAZY)
#JoinTable(name = "svcAuthUserRolev2", schema="dbo", joinColumns = {
#JoinColumn(name = "user_id", updatable=false,insertable=false, nullable = false) },
inverseJoinColumns = { #JoinColumn(name = "role_id",
updatable=false,insertable=false, nullable = false) })
private Set<AuthRoleEntity> roles;
And the following in my Roles object
#ManyToMany(fetch = FetchType.LAZY, mappedBy = "roles")
private Set<AuthUserEntity> users;
No matter what I do if I make changes to the roles on a user when saving they are persisted and this is not what I would like. I want roles on the user object to be read only.
I'm not sure why updateable and insertable are not working, I haven't used those attributes much. One possible solution is to make AuthRoleEntity the owning entity of the many-to-many relationship. Just move the #JoinTable annotation to the AuthRoleEntity and put the mappedBy on the AuthUserEntity.

java.lang.IllegalStateException: Multiple representations of the same entity with #ManyToMany 3 entities

I have 3 entities with ManyToMany relationships:
Role Entity:
#Entity
public class Role {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer roleID;
private String roleName;
private String description;
#ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.EAGER)
#JoinTable(name = "role_permission", joinColumns = {#JoinColumn(name = "role_id")}, inverseJoinColumns = {#JoinColumn(name = "permission_id")})
private Set<Permission> permissions = new LinkedHashSet<Permission>();
}
Permission Entity:
#Entity
public class Permission {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private int permissionID;
private String permissionName;
private String description;
#ManyToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}, fetch = FetchType.EAGER)
#JoinTable(name = "permission_functionality", joinColumns = {#JoinColumn(name = "permission_id")}, inverseJoinColumns = {#JoinColumn(name = "functionality_id")})
private Set<Functionality> functionalities = new LinkedHashSet<>();
}
Functionality Entity:
#Entity
public class Functionality {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
}
I did the following:
I have created 3 functionalities:
Functionality1, Functionality2, Functionality3
Then created 2 permissions:
Permission1 with Functionality1, Functionality2
Permission2 with Functionality2, Functionality3
Then created a role:
Role1 with Permission1 and Permission2
I am getting the following exception:
java.lang.IllegalStateException: Multiple representations of the same entity [com.persistence.entity.admin.Functionality#1] are being merged. Detached: [com.persistence.entity.admin.Functionality#4729256a]; Detached: [com.persistence.entity.admin.Functionality#56ed25db]
Fixed it by removing CascadeType.MERGE on Permission entity
The correct solution would have been to upgrade to hibernate 4.2.15 / 4.3.6 or above and add the following lines to your persistence.xml:
<property name="hibernate.event.merge.entity_copy_observer" value="allow"/>
Check your equals and hashCode method, ensure that it is consistent and correctly defined. For example I had copied and mistakenly pasted another-class when computing hashCode, this caused the object never to be equal with itself :(.
Like others who based their answers on HHH-9106 but, because I'm using Spring Boot with Java-based annotation, I had to use the following in application.properties:
spring.jpa.properties.hibernate.event.merge.entity_copy_observer=allow
I ran into the same problem too and solved it by add a few configs in application.yaml files.
jpa:
properties:
hibernate:
enable_lazy_load_no_trans: true
event:
merge:
entity_copy_observer: allow
See it here How to persist a new entity containing multiple identical instances of another unpersisted entity with spring-boot and JPA?
I could fix it by replacing
cascade = CascadeType.All
with
casecade={CascadeType.PERSIST,CascadeType.REMOVE}
For Hibernate see the workaround here HHH-9106.
In my case, moving the fetch operation and save operation in same #Transactional block solved the problem.
error occurs when we have multiple object of same time in HashSet.Possible due to incorrect hash function.Hashset check equality of object on the basis of hash function between two objects.
Way to debug
Just try to print hashset you will see multiple object of same type.
Solution::#
Use HashSet while defining one to many relationships.
Avoid using Lists.
Make sure your hash function should be correct.
I fixed this issue by removing cascade = CascadeType.ALL in your case (cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
My code source:
Before
#ManyToMany(fetch=FetchType.EAGER, cascade = CascadeType.ALL)
#JoinTable(
name = "link_module_parcour",
joinColumns = {#JoinColumn(name = "module_id", referencedColumnName = "id")},
inverseJoinColumns = {#JoinColumn(name = "parcour_id", referencedColumnName = "id")})
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
#BatchSize(size = 20)
private Set<Parcour> parcours = new HashSet<>();
After
#ManyToMany(fetch=FetchType.EAGER)
#JoinTable(
name = "link_module_parcour",
joinColumns = {#JoinColumn(name = "module_id", referencedColumnName = "id")},
inverseJoinColumns = {#JoinColumn(name = "parcour_id", referencedColumnName = "id")})
#Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
#BatchSize(size = 20)
private Set<Parcour> parcours = new HashSet<>();
**#LazyCollection(LazyCollectionOption.FALSE**
#OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
#JoinColumn(name = "translate_id")
If you remove the cascade merge you won't be able to update the entity when updating the parent.
The case will be right if you don't want to update the child, but if you want to update the child when updating the parent removing merge will only not show the error but the issue will still exist.
What happened with me: I got the same exception but I need Cascade.merge. After searching I found that some of the values I was using when creating the entity for the first time are updated and removed from the code but in the database, they still exist and when removing them from the database also work as expected.
To clarify the case:
Let's say I have an enum('ACTIVE', 'NOT_ACTIVE', 'SUSPENDED') and the field using this enum is key, when updating the code and removing the NOT_ACTIVE we should alter the database table with the new values enum('ACTIVE', 'SUSPENDED')
I had similar issue and it is resolved when I replace CascadeType.All with {CascadeType.persist, CascadeType.remove}.
Let me know if it works.
We can also resolve that using CascadeType.DETACH . In this case, if we need to update something in Permission, we have to update Permession separately and not by updating the related entity!
Just a note to say I am using Hibernate Core 4.3.8 in a Spring MVC application, based on Spring Core 4.1.6. The workaround:
<property name="hibernate.event.merge.entity_copy_observer" value="allow"/>
Did not work for me. I needed to remove the CascadeType.MERGE in order to correctly populate an #ManyToMany. Not sure if newer versions of Hibernate have fixed this.

Categories

Resources