hibernate基于连接表的多对多单向关联

来源:互联网 发布:windows安装caffe 编辑:程序博客网 时间:2024/06/06 02:11

sql脚本:

create table Person4 ( personId number(10) not null primary key ,personName NVARCHAR2(100) );create table PersonAddress4 ( personId4 number(10) not null, addressId4 number(10) not null,primary key(personId4, addressId4));create table Address4 ( addressId number(10) not null primary key,addressName NVARCHAR2(100) );

person.hbm.xml:

<class name="com.chenjun.eshop.staffManage.domain.Person"table="Person4"><id name="id" column="personId"><generator class="assigned"><!-- 由程序分配主键 --></generator></id><property name="personName"></property><!-- personId4, addressId4都是连接表PersonAddress4的字段,通过Address,Person对应的表为它赋值--><set name="addresses" table="PersonAddress4"><key column="personId4" /><many-to-many column="addressId4"class="com.chenjun.eshop.staffManage.domain.Address" /></set></class>

address.hbm.xml:

<class name="com.chenjun.eshop.staffManage.domain.Address"table="Address4"><id name="id" column="addressId"><generator class="assigned"><!-- 由程序分配主键 --></generator></id><property name="addressName"></property></class>

测试代码:

public void test()    {        Session session = this.getHibernateTemplate().getSessionFactory().openSession();        session.beginTransaction();                        Address address1 = new Address();        address1.setId(3);        address1.setAddressName("上海");        session.save(address1);//要先持久化,不然报org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update                        Address address2 = new Address();        address2.setId(4);        address2.setAddressName("深圳");        session.save(address2);//要先持久化,不然报org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update                        Set<Address> addresses = new HashSet<Address>();        addresses.add(address1);        addresses.add(address2);                Person person1 = new Person();        person1.setId(21);        person1.setPersonName("王五1");        person1.setAddresses(addresses);                Person person2 = new Person();//再添加一个人,地址相同        person2.setId(22);        person2.setPersonName("王五2");        person2.setAddresses(addresses);                        session.save(person1);        session.save(person2);        // hibernate中的增删改都要提交事件,因为它默认是设为不提交的,而jdbc的connect是默认提交的。        session.beginTransaction().commit();        // 释放资源        session.close();    }

执行sql:

 insert into Address4 (addressName, addressId) values (?, ?) insert into Address4 (addressName, addressId) values (?, ?) insert into Person4 (personName, personId) values (?, ?) insert into Person4 (personName, personId) values (?, ?)insert into PersonAddress4 (personId4, addressId4) values (?, ?)insert into PersonAddress4 (personId4, addressId4) values (?, ?)

结果:

person表:

    21    王五1    AAASpfAAEAAAANUAAA
    22    王五2    AAASpfAAEAAAANUAAB

address表:

   3    上海
    4    深圳


连接表:

   21    3

    21    4
    22    3
    22    4


从结果可以看出,人与地址是多对多关系



0 0
原创粉丝点击