-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: hibernate many-to-many updating table
PostPosted: Tue Dec 12, 2017 3:23 am 
Newbie

Joined: Fri Dec 08, 2017 6:00 am
Posts: 4
Hi friend,

I have a user entity and usermenu entity.Mapped usermenu in user class with many to many as shown below.But what i found is new rows are being inserted into user_menu mapping table when user objects are created and updated.solution reqd .

Code:
import java.io.Serializable;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Proxy;
import org.hibernate.annotations.Type;
import org.springframework.context.annotation.Scope;
@Entity
@Table(name ="USERS_MASTERS")
@Proxy(lazy = false)
public class User    {
   /**
    *
    */
   private static final long serialVersionUID = 1L;
   @Id 
   @Column(name="id", updatable = false, nullable = false)
   private String id;
    
   
    @ManyToMany(fetch = FetchType.EAGER)
   @JoinTable(name = " MENU_USE_MAPPING", joinColumns = { @JoinColumn(name = "id") }, inverseJoinColumns = { @JoinColumn(name = "MENU_ID") })
   private Set<UserMenu> usermenus;
}



and user tables is as folllow

Code:
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.hibernate.annotations.Proxy;

import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "USERS_MENU" ,   uniqueConstraints = @UniqueConstraint(columnNames = "MENU_ID"))
public class UserMenu {
   @Id @GeneratedValue
   @Column(name="MENU_ID" , insertable = false, updatable = false)
   private int menuId;
   
   public UserMenu() {
      super();
      // TODO Auto-generated constructor stub
   }
   
   @Column(name="MENU_LEVEL")
   private int menuLevel;
   @Column(name="MENU_NAME")
   private String MenuName;
   @ManyToOne(cascade={CascadeType.ALL})
   @JoinColumn(name="PARENT_MENU")
   private UserMenu parent;
   @OneToMany(fetch = FetchType.EAGER,mappedBy="parent", cascade = CascadeType.ALL)
   private Set<UserMenu> submenus=new HashSet<UserMenu>();
   
   public Set<UserMenu> getSubmenus() {
      return submenus;
   }
   public void setSubmenus(Set<UserMenu> submenus) {
      this.submenus = submenus;
   }
   
   
   @Column(name="MENU_URL")
   private String url;
   public int getMenuId() {
      return menuId;
   }
   public void setMenuId(int menuId) {
      this.menuId = menuId;
   }
   public int getMenuLevel() {
      return menuLevel;
   }
   public void setMenuLevel(int menuLevel) {
      this.menuLevel = menuLevel;
   }
   public String getMenuName() {
      return MenuName;
   }
   public void setMenuName(String menuName) {
      MenuName = menuName;
   }
   public UserMenu getParent() {
      return parent;
   }
   public void setParent(UserMenu parent) {
      this.parent = parent;
   }
   public String getUrl() {
      return url;
   }
   public void setUrl(String url) {
      this.url = url;
   }

}




thanks


Top
 Profile  
 
 Post subject: Re: hibernate many to may updating table
PostPosted: Tue Dec 12, 2017 6:25 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1630
Location: Romania
There are many issues related to your mapping:

1. Unidirectional associations are very problematic, switch to bidirectional ones as explained in this article.
2. Using EAGER is bad for performance. You are going to load the entire database with one query.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.