现有两个对象Role(int id,Set<Authority> authoritys)和Authority(int id,String name),多对多单向关联
怎样写对authoritys的增、删、改、查语句(hibernate)???
请举例说明,
谢谢。

解决方案 »

  1.   

    配置好关联关系,设置好级联把数据都set()好
    然后掉用save()方法就行了
      

  2.   

    我正在做一个学校的管理系统,里面用到了学校和学院的多对多关系,你可以参考借鉴一下。
    学校的xml,中间表是school_college,通过ID来关联。
    <class name="School">
    <id name="schoolId" type="integer">
    <generator class="identity"></generator>
    </id>
    <property name="schoolName" type="string"></property>
    <set name="student" cascade="none" lazy="true">
    <key>
    <column name="schoolId"></column>
    </key>
    <one-to-many class="Student" />
    </set>
    <set name="college" cascade="save-update" table="school_college" lazy="false" inverse="false">
    <key>
    <column name="schoolId"></column>
    </key>
    <many-to-many class="College" column="collegeId" />
    </set>
    </class>
    学院的xml
    <class name="College">
    <id name="collegeId">
    <generator class="identity"></generator>
    </id>
    <property name="collegeName"></property>
    <set name="school" table="school_college" lazy="false" inverse="false"
    cascade="save-update">
    <key column="collegeId"></key>
    <many-to-many class="School" column="schoolId"></many-to-many>
    </set>
    <set name="student" cascade="none" lazy="false">
    <key>
    <column name="collegeId"></column>
    </key>
    <one-to-many class="Student" />
    </set> </class>
    学校bean
    public class School {
    private int schoolId;
    private String schoolName;
    private Set<College> college;
    学院bean
    public class College {
    private int collegeId;
    private String collegeName;
    private Set<School> school;
    插入的时候先setSchool所有的属性,将学院的信息也要set进去,然后你操作学校就会更改中间表和对应的学院信息
      

  3.   

    我也是用的Set。
    对中间的连接表的操作是不是只能用createSQLQuery()呢?
    有没有对对象的操作,来改变中间关联表的数据内容?
      

  4.   

    配置好关联关系,设置好级联把数据都set()好
    然后掉用save()方法就行了 
      

  5.   

    我试过了,Set的数据总是save不了,
    只好写一个createSQLQuery()对关联表直接操作了。