您的位置:首页 > 其它

Hibernate之对象关系映射07一对一多单向关联

2016-11-30 01:10 621 查看
一个人有多个地址,公司地址、家庭地址等等。

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"
"http://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"
"http://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();
}
}测试结果暂时报错,明天再看。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: