频道栏目
首页 > 程序开发 > 软件开发 > 其他 > 正文
Hibernate之对象关系映射07一对一多单向关联
2016-12-01 09:51:00         来源:陈三的博客  
收藏   我要投稿

一个人有多个地址,公司地址、家庭地址等等。

Address1ntab.java

private int addressId;
private String detailAddress;

Person1ntab.java

private int personId;
private String personName;
private int age;
private Set address1n = new HashSet();

表结构:

-- 一对多单向 中间表
DROP TABLE IF EXISTS join_1ntab;
create table join_1ntab (
    person_id int(11) not null unique,
    address_id integer not null,
    primary key (person_id, address_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 一对多单向 地址表
DROP TABLE IF EXISTS address_1ntab;
create table address_1ntab (
    address_id int(11) primary key auto_increment,
    detail_address varchar(255)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- 一对多单向 用户表
DROP TABLE IF EXISTS person_1ntab;
create table person_1ntab (
    person_id int(11) primary key auto_increment,
    person_name varchar(255),
    age int(3)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

alter table join_1ntab add constraint fk_1ntab_address foreign key (address_id) references address_1ntab(address_id);
alter table join_1ntab add constraint fk_1ntab_person foreign key (person_id) references person_1ntab (person_id);

Address1ntab.hbm.xml

<?xml version='1.0' encoding='utf-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  "https://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  
<hibernate-mapping>  
  <class name="com.ack.hibernateMapping.entity.Address1ntab" table="address_1ntab">  
    <id name="addressId" column="address_id">  
        <generator class="identity"/>  
    </id>  
      
    <property name="detailAddress" column="detail_address"/>  
  </class>  
</hibernate-mapping>

Person1ntab.hbm.xml

<?xml version='1.0' encoding='utf-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
  "https://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  
<hibernate-mapping>  
  <class name="com.ack.hibernateMapping.entity.Person1ntab" table="person_1ntab">  
    <id name="personId" column="person_id">  
        <generator class="identity"/>  
    </id>  
      
    <property name="personName" column="person_name"/>  
    <property name="age"/>  
      
    <!--映射集合属性,join_1n是连接表表名-->  
    <set name="address1n" table="join_1ntab">  
      <key column="person_id"/>  
      <many-to-many column="address_id" unique="true"   
        class="com.ack.hibernateMapping.entity.Address1ntab"/>  
    </set>  
  </class>  
</hibernate-mapping>

测试类:

public class Test_pa_1ntab {  
    public static void main(String[] args) {  
          
        Address1ntab add1 = new Address1ntab();  
        Address1ntab add2 = new Address1ntab();  
        Address1ntab add3 = new Address1ntab();  
        Person1ntab p1 = new Person1ntab();  
        Person1ntab p2 = new Person1ntab();  
          
        add1.setDetailAddress("丽水市莲都区");  
        add2.setDetailAddress("丽水市缙云县");  
        add3.setDetailAddress("丽水市青田县");  
          
        p1.getAddress1n().add(add1);  
        p1.getAddress1n().add(add2);  
        p2.getAddress1n().add(add3);  
          
        p1.setPersonName("陈三");  
        p2.setPersonName("陈大大");  
          
        Session session = HibernateUtil.getCurrentSession();  
        session.beginTransaction();  
        session.save(add1);  
        session.save(add2);  
        session.save(add3);  
        session.save(p1);  
        session.save(p2);  
        session.getTransaction().commit();  
    }  
}
点击复制链接 与好友分享!回本站首页
上一篇:1094. The Largest Generation (25)
下一篇:LINK2005错误
相关文章
图文推荐
文章
推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站