insert sql consumes long time in Hibernate
In my application I have two table, Group and Role, that are related between them with a @ManyToMany relationsheep. Since each Group can have many Roles and each Role can have many Groups.

The first one:

        @Table(name = "groups")
        @Cache(usage =  CacheConcurrencyStrategy.READ_WRITE)
        public class Groups {
     
     private Set<Role> roles = new HashSet<Role>();
     
        @ManyToMany( 
       fetch = FetchType.EAGER, 
       cascade = {CascadeType.DETACH, CascadeType.PERSIST, CascadeType.REFRESH})
       @JoinTable(name = "group_role", joinColumns = @JoinColumn(name = "group_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
     @IndexedEmbedded
     public Set<Role> getRoles() {
      return this.roles;  
     }
    }


And the Role class is:


        @Entity
        @Table(name = "role")
        @Cache(usage =  CacheConcurrencyStrategy.READ_WRITE)
        public class Role {
    
     /** Field mapping. */ 
     private Set<Groups> groups = new HashSet<Groups>();
    
      @ManyToMany(
      fetch = FetchType.EAGER
      //,cascade = { CascadeType.PERSIST} 
      )
     @JoinTable(name = "group_role", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name ="group_id" )) 
     public Set<Groups> getGroups() {
      return this.groups;  
     }
    }



I want to add a role to a group, that means adding a record to the group_role table.

        @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
     public void saveRoleToGroup(Group group,Role... roles) {
      groups.setRoles(roles);
      groupsDAO.merge(groups);
      LOG.debug("message2");
     }

It works and saves, just executing to the merge takes him 1 minutes! 

watching the sql statment that are printed to the log, shows that after executing the follwing statment, there is a minute before closing the session.

    15:55:56,875 DEBUG  - message2
    15:55:56,906 DEBUG (SQLStatementLogger.java:111) - update groups set version=?, company_id=?, name=? where id=? and version=?
    15:55:56,906 DEBUG (SQLStatementLogger.java:111) - insert into group_role (group_id, role_id) values (?, ?)
    15:57:10,171 DEBUG     - end

The end statement comes from the calling method.

Why the simple insert statement consumes so much time (1 min)?

 
insert sql consumes long time in Hibernate
===

In my application I have two table, Group and Role, that are related between them with a @ManyToMany relationsheep. Since each Group can have many Roles and each Role can have many Groups.

The first one:

        @Table(name = "groups")
        @Cache(usage =  CacheConcurrencyStrategy.READ_WRITE)
        public class Groups {
     
     private Set<Role> roles = new HashSet<Role>();
     
        @ManyToMany( 
       fetch = FetchType.EAGER, 
       cascade = {CascadeType.DETACH, CascadeType.PERSIST, CascadeType.REFRESH})
       @JoinTable(name = "group_role", joinColumns = @JoinColumn(name = "group_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
     @IndexedEmbedded
     public Set<Role> getRoles() {
      return this.roles;  
     }
    }


And the Role class is:


        @Entity
        @Table(name = "role")
        @Cache(usage =  CacheConcurrencyStrategy.READ_WRITE)
        public class Role {
    
     /** Field mapping. */ 
     private Set<Groups> groups = new HashSet<Groups>();
    
      @ManyToMany(
      fetch = FetchType.EAGER
      //,cascade = { CascadeType.PERSIST} 
      )
     @JoinTable(name = "group_role", joinColumns = @JoinColumn(name = "role_id"), inverseJoinColumns = @JoinColumn(name ="group_id" )) 
     public Set<Groups> getGroups() {
      return this.groups;  
     }
    }



I want to add a role to a group, that means adding a record to the group_role table.

        @Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
     public void saveRoleToGroup(Group group,Role... roles) {
      groups.setRoles(roles);
      groupsDAO.merge(groups);
      LOG.debug("message2");
     }

It works and saves, just executing to the merge takes him 1 minutes! 

watching the sql statment that are printed to the log, shows that after executing the follwing statment, there is a minute before closing the session.

    15:55:56,875 DEBUG  - message2
    15:55:56,906 DEBUG (SQLStatementLogger.java:111) - update groups set version=?, company_id=?, name=? where id=? and version=?
    15:55:56,906 DEBUG (SQLStatementLogger.java:111) - insert into group_role (group_id, role_id) values (?, ?)
    15:57:10,171 DEBUG     - end

The end statement comes from the calling method.

Why the simple insert statement consumes so much time (1 min)?

 