合并父级和懒惰子集合列表

时间:2016-09-20 08:50:54

标签: java spring hibernate spring-mvc merge

1个父实体可能有0个或多个懒惰子实体

例如,有一个函数更改父实体和子实体中的status列,而merge(父),父实体更新但子实体插入new而不是update。

子实体id,数据与调试时的数据库完全相同。

父对象放在spring控制器的@SessionAttributes中,这是原因吗?

即使我只合并子列表merge(childList),它也会创建新记录而不是更新。

    @Entity
    @Table(name = "member")
    public class Member implements Serializable{



        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name="id")
        private int id;

        @OneToMany(fetch = FetchType.LAZY, mappedBy = "memberParent", cascade = CascadeType.ALL)
        public List<Child> ChildList


        getter setter......

    }

    @Entity
    @Table(name = "child")
    public class Child implements Serializable{



        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Column(name="id")
        private int id;

    @Column(name="member_id")
        private int mem_id;

    @ManyToOne(fetch = FetchType.EAGER)
        @JoinColumns({
            @JoinColumn(name = "member_id", referencedColumnName = "id", insertable = false, updatable = false)
        })
        public Member memberParent;


        getter setter......

    }




//Controller
    @SessionAttributes({"member"})
    public class Appcontroller {

    @Transactional
        @RequestMapping(value = {"/update-member/{id}"}, method = RequestMethod.GET)
        public String viewEditRepresetative(ModelMap model, @PathVariable ind id) {
        Member member = memberService.find(id);
        model.addAttributes("member", member);

    }

    @Transactional
        @RequestMapping(value = {"/update-member"}, method = RequestMethod.POST)
        public String viewEditRepresetative(ModelMap model, HttpServletRequest reques, @Valid @ModelAttribute("member") Member member, BindingResult result,
                RedirectAttributes redirectAttributes, SessionStatus status) {
        if (!result.hasErrors()) {
            memberService.merge(member);
        }

    }

1 个答案:

答案 0 :(得分:0)

我在快照代码中看不到任何父子关系

请使用以下代码修改子类的代码以创建继承关系。

public class Child extends Member implements Serializable{

将Child类扩展为Parent(Member)将反映与延迟加载相关的所需更改。