1

在 play 2.0.X 中,一对多的关系是这样创建的。从父级删除子成员的正确方法是什么。我应该使用 iter.remove() 还是 child.delete()?

@Entity
@Table(name="channel")
public class ChannelInfo extends Model {
    @Id
    private Long id;

    @Constraints.Required
    private String channelName;

    @OneToMany(cascade=CascadeType.ALL, mappedBy="channelInfo")
    private Set<ChannelDetailMember> members;

    private int membersCount = 0;

    public void removeMemberId(String memberId) {
        Iterator<ChannelDetailMember> iter = this.getMembers().iterator();
        while (iter.hasNext()) {
            ChannelDetailMember dMember = iter.next();
            if (dMember.getMemberId().equals(memberId)) {
           dMember.delete();
           membersCount--;
           break;
             }
        }
    }
}

@Entity
@Table(name="channel_detail")
public class ChannelDetailMember extends Model {

    @Id
    private Long id;

    @Constraints.Required
    private String memberId;

    @Constraints.Required
    @ManyToOne(cascade=CascadeType.PERSIST)
    ChannelInfo channelInfo;
}
4

0 回答 0