Hibernate之对象关系映射07一对一多单向关联
2016-11-30 01:10
621 查看
一个人有多个地址,公司地址、家庭地址等等。
Address1ntab.java
Person1ntab.java
表结构:
-- 一对多单向 中间表
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
Person1ntab.hbm.xml
测试类:
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();
}
}测试结果暂时报错,明天再看。
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();
}
}测试结果暂时报错,明天再看。
相关文章推荐
- 【HIbernate框架学习】:Hibernate对象关系映射之单向多对一关联映射
- 【HIbernate框架学习】:Hibernate对象关系映射之单向多对多关联映射
- Hibernate之对象关系映射02一对一单向主键关联
- Hibernate之对象关系映射01一对一单向关联
- 【HIbernate框架学习】:Hibernate对象关系映射之单向一对多关联映射
- Hibernate之对象关系映射03一对一单向外键关联
- Hibernate关系映射(一)一对一单向外键关联@OneToOne Annotation方式
- Hibernate关系映射(10)_多对多单向关联
- Hibernate学习笔记-----关系映射(2)单向1-1关联
- Hibernate关系映射(1)_一对一单向外键关联
- Hibernate 映射关系 ---One2Many单向关联
- Hibernate实体关系映射:单向主键一对一关联
- Hibernate 单向 多 对 多 对象关系映射 ,测试 crud
- hibernate映射关系之一对一单向主键关联
- 【hibernate框架】关系映射之一对一单向外键关联(XML实现)
- Hibernate关系映射(5)_一对一单向联合主键关联
- Hibernate关系映射(四)一对多单向关联@OneToMany Annotation方式
- Hibernate关系映射1:单向N-1关联
- Hibernate几种关系映射总结(单向关联)
- Hibernate 映射关系 ---Many2Many 单向关联