i have a Users and Tags table,and also a user_tag_xref table that holds the many to many relationship.now netbeans generates the entity classes for me (using eclipselink) below is the entity mapping relationship
on User class
@ManyToMany(mappedBy = "usersList")
private List<Tags> tagsList;
on Tags class
@JoinTable(name = "USERS_TAG_XREF", joinColumns = {
@JoinColumn(name = "TAG_ID", referencedColumnName = "TAG_ID")}, inverseJoinColumns = {
@JoinColumn(name = "USER_ID", referencedColumnName = "USER_ID")})
@ManyToMany
private List<Users> usersList;
Now im my business logic RESTfull service,a json client consumes this method
@POST
@Path("/registration2/tag")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response registration2_3(List<Tags>tagList,@Context HttpServletRequest req){
Profile p =(Profile) registerMap.get(req.getSession().getId());
Users u = em.find(Users.class,p.getUserId());
for(Tags t : tagList){
t.getUsersList().add(u);
u.getTagsList().add(t);
em.merge(t);
em.merge(u);
}
logger.log(Level.INFO, "the taglist created for user");
return Response.ok(u,MediaType.APPLICATION_JSON).build();
}
The problem is each time i merge a new user to create a many to many relationship, if an existing userid=6201 has a tag with 2,3,4 ,and the new user is try to use to tag id 2,3,4, the existing user is deleted and the new user merges to the tags. i have read several articles on overriding hash and equals to method in my entity classes,those methods are overridden by default in eclipselink ,also i cannot change my collection type to Set or Collection since List<> type works perfectly well for a json array. i v been having a hard time right now,its been 24hours,could it be the default mapping strategy is wrong? do i need to cascasde?
You have to be extra cautious while using merge as its semantics ( as explained here) is bit different from just update. As your relationship is bidirectional and users is the inverse side, so all of relationship persistence will be handled by tags side. Assuming that you tag lists contains detached tags, meaning all Tags have their id set, then you need to iterate over tagList
This takes care that if t is new instance (unmanaged) then a persistent representation of it will be returned which has to be used there after or if the instances were having their id set, then the ORM will create a managed instance with data from database ( or from first/second level cache if it exists there). The returned instance is the one managed
Also you can set the Merge cascade option on the Tag side to save you the second merge call and let the ORM manage relationship automatically.
Here is how merge behaves with detached entities and relations.
TestProgram
Here is the SQL generated and corresponding explanation