I have an entity "user" which is OneToMany mapped to another entity "item". List of Items are successfully added until the items are not duplicate. How can add duplicate items to the user.
In User entity
@OneToMany(mappedBy="user")
private List<Item> item;
//getters and setters
In Item Entity
@ManyToOne(cascade = CascadeType.ALL,fetch = FetchType.EAGER)
@JoinTable(
name="UserItem",
joinColumns= @JoinColumn(name="ITEM_ID"),
inverseJoinColumns = @JoinColumn(name="USER_ID")
)
private User user;
//getters and setters
My function in service class to add Item to User
public void addItemToUser(Integer userId,Integer itemId){
Session session = sessionFactory.getCurrentSession();
Item item2 = (Item)session.get(Item.class,itemId);
// session.save(item2);
User user2 = (User)session.get(User.class,userId);
user2.getItem().add(item2);
session.save(user2);
}
How can i add duplicate items to user. I am newbie in spring and hibernate
Check if you really need to use CascadeType.ALL instead of Cascade.SaveUpdate.