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