Hibernate多对多注解中间表插入数据失败的问题

阅读更多
1.表A  @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "companies")
       public Set getBs() {
           return bs;
        }

2.表B  @ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
@JoinTable(name = "tcompany_project", joinColumns = { @JoinColumn(name = "COMPANYID", nullable = false) }, inverseJoinColumns = { @JoinColumn(name = "PROJECTID", nullable = false) })
        public Set getAs() {
         return as;
       }

上面这两个注解还是有讲究的,顺序不能颠倒,否则会出错,中间表不会插入数据,因为这个被坑了好多天.
正确.  b.getAs.add(a)
错误   a.getBs.add(b).这个不会插入数据

你可能感兴趣的:(中间表,插入数据,hibernate)