jpa:当合并多个以前的记录被删除 [英] jpa: when merging many to many previous record gets deleted

查看:154
本文介绍了jpa:当合并多个以前的记录被删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用户和标签表,还有一个user_tag_xref表,其中包含许多关系。许多netbeans为我生成实体类(使用eclipselink),下面是实体映射关系

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

on User class

@ManyToMany(mappedBy = "usersList")
    private List<Tags> tagsList;

标签类

@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;

现在我的业务逻辑RESTfull服务,一个json客户端消耗这种方法

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(); 
    }

问题是每次我合并一个新用户来创建多个关系,如果现有用户ID = 6201具有2,3,4的标签,并且新用户尝试使用标记号2,3,4,
删除现有用户,并且新用户合并到标签。我已经阅读了几篇关于重写哈希的文章,并等同于我的实体类中的方法,这些方法在eclipselink中被默认覆盖,我也不能将我的集合类型更改为Set或Collection,因为List<>类型对于json数组工作得很好。我现在一直很难,已经24小时了,默认的映射策略是不对的?我需要cascasde吗?

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?

推荐答案

使用合并作为其语义时,您必须格外小心(如 here )与更新有点不同。
由于您的关系是双向的,用户是反面的,所以所有的关系持久性将由
标记一边处理。假设你的标签列表包含分离的标签,这意味着所有标签都有自己的id设置,那么你需要迭代tagList

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

Tag managedTag = em.merge(t);

这一点,如果t是新的实例(非管理),那么它的持久表示将被返回
必须在那里被使用,或者如果这些实例设置了id,那么ORM将使用数据库中的数据(或者存在于第一/第二级缓存中)创建一个托管实例。返回的实例是管理的

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

for(Tags t : tagList){
        Tag managedTag = em.merge(t);
        managedTag.getUsersList().add(u);
        u.getTagsList().add(t);         
       User managedUser =  em.merge(u); 

    } 

您还可以在标签侧设置合并级联选项保存第二个合并调用并让ORM自动管理关系。

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.

这篇关于jpa:当合并多个以前的记录被删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆