Hibernate Annotation级联操作不成功,求解?
1,对象关系说明:
School对UserMember是一对多关系:
====================================================
2,相关类说明:
private List<UserMember> users = new ArrayList<UserMember>(); @OneToMany(mappedBy = "school",cascade = {CascadeType.PERSIST,CascadeType.REFRESH}, fetch = FetchType.LAZY, targetEntity = UserMember.class) public List getUsers() { return users; }
private School school; @ManyToOne(cascade = { CascadeType.REFRESH},fetch=FetchType.LAZY) @JoinColumn(name = "schoolId") public SchoolInfo getSchool() { return school; } //注意这里,郁闷的是双向关联时,关联的主键的insert,update操作必须设定为false @Column(name = "schoolId", insertable = false, updatable = false, nullable = true, length = 36) public String getSchoolId() { return this.schoolId; }
====================================================
3,持久化操作:
备注:member1和member2都是新实例化的,并且注入有数据member1.setSchool(school); member2.setSchool(school); List<UserMember> users=new ArrayList<UserMember>(); users.add(member1); users.add(member2); school.setUsers(users); this.schoolService.create(this.school);====================================================
4,问题:
1,本来预期上面操作会同时insert2条用户信息到数据库,但结果是只插入了school信息.我把关联改为单向也不行.级联好像没起作用.求解????
2,我配置的为双向一对多关系,保存用户信息时,必须member.setSchool(school)才能保存进去数据,直接member.setSchoolId("");好像不行,求解????