关联映射之单向多对一
1、实体类
Role.java
public class Role {
private Integer id;
private String name;
public Role() {
// TODO Auto-generated constructor stub
}
public Role(String name) {
super();
this.name = name;
}
//省略get/set方法
}
User.java
public class User {
private int id;
private String name;
private int age;
private Role role;
public User() {
}
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
//省略get/set方法
}
2、在数据库表的关系
3、映射文件
User.hbm.xml
<hibernate-mapping>
<!-- class配置类 name指类名 table指定表名 如果不写,默认类名为表名-->
<class name="cn.sxt.pojo.User" table="t_user">
<id name="id" column="id">
<generator class="identity">
</generator>
</id>
<!-- property是配置类的属性 name指属性名 -->
<property name="name" length="40"/>
<property name="age" />
<many-to-one name="role" column="roleId" not-null="true"></many-to-one>
</class>
</hibernate-mapping>
Role.hbm.xml
<hibernate-mapping>
<!-- class配置类 name指类名 table指定表名 如果不写,默认类名为表名-->
<class name="cn.sxt.pojo.Role" table="t_role">
<id name="id" column="id">
<generator class="identity">
</generator>
</id>
<!-- property是配置类的属性 name指属性名 -->
<property name="name" length="40"/>
</class>
</hibernate-mapping>
4、测试
public class HibernateTest {
@Test
public void testCreateDB(){
Configuration cfg = new Configuration().configure();
SchemaExport se = new SchemaExport(cfg);
//第一个参数 是否打印脚本,第二个参数是否导入数据库
se.create(true, true);
}
@Test
public void testInit(){
Session session = null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
Role r1 = new Role("岛主");
Role r2 = new Role("峰主");
session.save(r1);
session.save(r2);
User u1 = new User();
u1.setName("黄药师");
u1.setAge(66);
u1.setRole(r1);
User u2 = new User();
u2.setName("西毒");
u2.setAge(56);
u2.setRole(r2);
User u3 = new User();
u3.setName("郭襄");
u3.setAge(63);
u3.setRole(r1);
session.save(u1);
session.save(u2);
session.save(u3);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
@Test
public void testGet(){
Session session = null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
User u = (User)session.get(User.class, 1);
System.out.println("姓名:"+u.getName()+" 年龄:"+u.getAge()+" 角色:"+u.getRole().getName());
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
}
关联映射之单向一对多
1、实体类
Role.java
public class Role {
private Integer id;
private String name;
private Set<User> users;
public Role() {
// TODO Auto-generated constructor stub
}
public Role(String name) {
super();
this.name = name;
}
//省略get/set方法
}
User.java
public class User {
private int id;
private String name;
private int age;
public User() {
}
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
//省略get/set方法
}
2、映射文件
User.hbm.xml
<hibernate-mapping>
<!-- class配置类 name指类名 table指定表名 如果不写,默认类名为表名-->
<class name="cn.sxt.pojo.User" table="t_user">
<id name="id" column="id">
<generator class="identity">
</generator>
</id>
<!-- property是配置类的属性 name指属性名 -->
<property name="name" length="40"/>
<property name="age" />
</class>
</hibernate-mapping>
Role.hbm.xml
<hibernate-mapping>
<!-- class配置类 name指类名 table指定表名 如果不写,默认类名为表名-->
<class name="cn.sxt.pojo.Role" table="t_role">
<id name="id" column="id">
<generator class="identity">
</generator>
</id>
<!-- property是配置类的属性 name指属性名 -->
<property name="name" length="40"/>
<set name="users">
<!-- 配置外键 -->
<key column="roleId" not-null="true"></key>
<one-to-many class="cn.sxt.pojo.User"/>
</set>
</class>
</hibernate-mapping>
/*测试*/
public class HibernateTest {
@Test
public void testCreateDB(){
Configuration cfg = new Configuration().configure();
SchemaExport se = new SchemaExport(cfg);
//第一个参数 是否打印脚本,第二个参数是否导入数据库
se.create(true, true);
}
@Test
public void testInit(){
Session session = null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
User u1 = new User();
u1.setName("黄药师");
u1.setAge(66);
User u2 = new User();
u2.setName("西毒");
u2.setAge(56);
User u3 = new User();
u3.setName("郭襄");
u3.setAge(63);
Role r1 = new Role("岛主");
r1.getUsers().add(u1);
r1.getUsers().add(u3);
Role r2 = new Role("峰主");
r2.getUsers().add(u2);
session.save(r1);
session.save(r2);
session.save(u1);
session.save(u2);
session.save(u3);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
@Test
public void testGet(){
Session session = null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
Role role = (Role)session.get(Role.class, 1);
System.out.println("角色名:"+role.getName());
System.out.println("--------------");
for(User u:role.getUsers()){
System.out.println(u);
}
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
}
关联映射之双向一对多
1、实体类
Role.java
public class Role {
private Integer id;
private String name;
private Set<User> users;
public Role() {
// TODO Auto-generated constructor stub
}
public Role(String name) {
super();
this.name = name;
}
//省略get/set方法
}
User.java
public class User {
private int id;
private String name;
private int age;
private Role role;
public User() {
}
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
//省略get/set方法
}
2、映射文件
User.hbm.xml
<hibernate-mapping>
<!-- class配置类 name指类名 table指定表名 如果不写,默认类名为表名-->
<class name="cn.sxt.pojo.User" table="t_user">
<id name="id" column="id">
<generator class="identity">
</generator>
</id>
<!-- property是配置类的属性 name指属性名 -->
<property name="name" length="40"/>
<property name="age" />
<many-to-one name="role" column="roleId"/>
</class>
</hibernate-mapping>
Role.hbm.xml
<hibernate-mapping>
<!-- class配置类 name指类名 table指定表名 如果不写,默认类名为表名-->
<class name="cn.sxt.pojo.Role" table="t_role">
<id name="id" column="id">
<generator class="identity">
</generator>
</id>
<!-- property是配置类的属性 name指属性名 -->
<property name="name" length="40"/>
<set name="users">
<!-- 配置外键 -->
<key column="roleId"></key>
<one-to-many class="cn.sxt.pojo.User"/>
</set>
</class>
</hibernate-mapping>
3、测试
public class HibernateTest {
@Test
public void testCreateDB(){
Configuration cfg = new Configuration().configure();
SchemaExport se = new SchemaExport(cfg);
//第一个参数 是否打印脚本,第二个参数是否导入数据库
se.create(true, true);
}
@Test
public void testInit(){
Session session = null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
Role r1 = new Role("岛主");
Role r2 = new Role("峰主");
session.save(r1);
session.save(r2);
User u1 = new User();
u1.setName("黄药师");
u1.setAge(66);
u1.setRole(r1);
User u2 = new User();
u2.setName("西毒");
u2.setAge(56);
u2.setRole(r2);
User u3 = new User();
u3.setName("郭襄");
u3.setAge(63);
u3.setRole(r1);
session.save(u1);
session.save(u2);
session.save(u3);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
@Test
public void testGet(){
Session session = null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
User user = (User)session.get(User.class, 1);
System.out.println("姓名:"+user.getName()+" 角色:"+user.getRole().getName());
System.out.println("=====================");
Role role = (Role)session.get(Role.class, 1);
System.out.println("角色名:"+role.getName());
System.out.println("--------------");
for(User u:role.getUsers()){
System.out.println(u);
}
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
}
关联映射之基于外键单向一对一
1、人与身份证的关系(一对一)
User.java
public class User {
private int id;
private String name;
private int age;
private IdCard idCard;
public User() {
}
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
//省略get/set
}
IdCard.java
public class IdCard {
private String id;
private String address;
public IdCard() {
}
public IdCard(String address) {
super();
this.address = address;
}
//省略get/set
}
2、在数据库表的关系
3、映射文件
User.hbm.xml
<hibernate-mapping>
<!-- class配置类 name指类名 table指定表名 如果不写,默认类名为表名-->
<class name="cn.sxt.pojo.User" table="t_user">
<id name="id" column="id">
<generator class="identity">
</generator>
</id>
<!-- property是配置类的属性 name指属性名 -->
<property name="name" length="40"/>
<property name="age" />
<many-to-one name="idCard" column="cardId" unique="true"/>
</class>
</hibernate-mapping>
IdCard.hbm.xml
<hibernate-mapping>
<!-- class配置类 name指类名 table指定表名 如果不写,默认类名为表名-->
<class name="cn.sxt.pojo.IdCard" table="t_idCard">
<id name="id" column="id">
<generator class="assigned">
</generator>
</id>
<property name="address"/>
</class>
</hibernate-mapping>
4、测试
public class HibernateTest {
@Test
public void testCreateDB(){
Configuration cfg = new Configuration().configure();
SchemaExport se = new SchemaExport(cfg);
//第一个参数 是否打印脚本,第二个参数是否导入数据库
se.create(true, true);
}
@Test
public void testInit(){
Session session = null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
IdCard idCard = new IdCard();
idCard.setId("321023123123123");
idCard.setAddress("北京");
IdCard idCard1 = new IdCard();
idCard1.setId("654352345243523");
idCard1.setAddress("天津");
User u1 = new User("张三",23);
u1.setIdCard(idCard);
User u2 = new User("李四",22);
u2.setIdCard(idCard1);
session.save(idCard);
session.save(idCard1);
session.save(u1);
session.save(u2);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
@Test
public void testGet(){
Session session = null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
User user = (User)session.get(User.class, 1);
System.out.println("姓名:"+user.getName()+" 身份证:"+user.getIdCard().getId());
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
}
关联映射之基于外键的双向一对一
1、人与身份证的关系(一对一)
User.java
public class User {
private int id;
private String name;
private int age;
private IdCard idCard;
public User() {
}
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
//省略get/set
}
IdCard.java
public class IdCard {
private String id;
private String address;
private User user;
public IdCard() {
}
public IdCard(String address) {
super();
this.address = address;
}
//省略get/set
}
2、在数据库表的关系
3、映射文件
User.hbm.xml
<hibernate-mapping>
<!-- class配置类 name指类名 table指定表名 如果不写,默认类名为表名-->
<class name="cn.sxt.pojo.User" table="t_user">
<id name="id" column="id">
<generator class="identity">
</generator>
</id>
<!-- property是配置类的属性 name指属性名 -->
<property name="name" length="40"/>
<property name="age" />
<many-to-one name="idCard" column="cardId" unique="true"/>
</class>
</hibernate-mapping>
IdCard.hbm.xml
<hibernate-mapping>
<!-- class配置类 name指类名 table指定表名 如果不写,默认类名为表名-->
<class name="cn.sxt.pojo.IdCard" table="t_idCard">
<id name="id" column="id">
<generator class="assigned">
</generator>
</id>
<property name="address"/>
<one-to-one name="user" property-ref="idCard"/>
</class>
</hibernate-mapping>
4、测试
public class HibernateTest {
@Test
public void testCreateDB(){
Configuration cfg = new Configuration().configure();
SchemaExport se = new SchemaExport(cfg);
//第一个参数 是否打印脚本,第二个参数是否导入数据库
se.create(true, true);
}
@Test
public void testInit(){
Session session = null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
IdCard idCard = new IdCard();
idCard.setId("321023123123123");
idCard.setAddress("北京");
IdCard idCard1 = new IdCard();
idCard1.setId("654352345243523");
idCard1.setAddress("天津");
User u1 = new User("张三",23);
u1.setIdCard(idCard);
User u2 = new User("李四",22);
u2.setIdCard(idCard1);
session.save(idCard);
session.save(idCard1);
session.save(u1);
session.save(u2);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
@Test
public void testGet(){
Session session = null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
User user = (User)session.get(User.class, 1);
System.out.println("姓名:"+user.getName()+" 身份证:"+user.getIdCard().getId());
System.out.println("===================");
IdCard card = (IdCard)session.get(IdCard.class, "321023123123123");
System.out.println("身份证:"+card.getId()+" 地址:"+card.getAddress()+" 姓名:"+card.getUser().getName());
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
}
关联映射之基于主键的单向一对一
1、人与身份证的关系(一对一)
User.java
public class User {
private String id;
private String name;
private int age;
private IdCard idCard;
public User() {
}
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
//省略get/set
}
IdCard.java
public class IdCard {
private String id;
private String address;
public IdCard() {
}
public IdCard(String address) {
super();
this.address = address;
}
//省略get/set
}
2、在数据库表的关系
3、映射文件
User.hbm.xml
<hibernate-mapping>
<!-- class配置类 name指类名 table指定表名 如果不写,默认类名为表名-->
<class name="cn.sxt.pojo.User" table="t_user">
<id name="id" column="id">
<generator class="foreign">
<param name="property">idCard</param>
</generator>
</id>
<!-- property是配置类的属性 name指属性名 -->
<property name="name" length="40"/>
<property name="age" />
<one-to-one name="idCard" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
IdCard.hbm.xml
<hibernate-mapping>
<!-- class配置类 name指类名 table指定表名 如果不写,默认类名为表名-->
<class name="cn.sxt.pojo.IdCard" table="t_idCard">
<id name="id" column="id">
<generator class="assigned">
</generator>
</id>
<property name="address"/>
</class>
</hibernate-mapping>
4、测试
public class HibernateTest {
@Test
public void testCreateDB(){
Configuration cfg = new Configuration().configure();
SchemaExport se = new SchemaExport(cfg);
//第一个参数 是否打印脚本,第二个参数是否导入数据库
se.create(true, true);
}
@Test
public void testInit(){
Session session = null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
IdCard idCard = new IdCard();
idCard.setId("321023123123123");
idCard.setAddress("北京");
IdCard idCard1 = new IdCard();
idCard1.setId("654352345243523");
idCard1.setAddress("天津");
session.save(idCard);
session.save(idCard1);
User u1 = new User("张三",23,idCard);
User u2 = new User("李四",22,idCard1);
session.save(u1);
session.save(u2);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
@Test
public void testGet(){
Session session = null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
User user = (User)session.get(User.class, "321023123123123");
System.out.println("姓名:"+user.getName()+" 身份证:"+user.getIdCard().getId()+" 地址:"+user.getIdCard().getAddress());
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
}
关联映射之主键一对一双向关联
1、人与身份证的关系(一对一)
User.java
public class User {
private String id;
private String name;
private int age;
private IdCard idCard;
public User() {
}
public User(String name, int age) {
super();
this.name = name;
this.age = age;
}
//省略get/set
}
IdCard.java
public class IdCard {
private String id;
private String address;
private User user;
public IdCard() {
}
public IdCard(String address) {
super();
this.address = address;
}
//省略get/set
}
2、在数据库表的关系
3、映射文件
User.hbm.xml
<hibernate-mapping>
<!-- class配置类 name指类名 table指定表名 如果不写,默认类名为表名-->
<class name="cn.sxt.pojo.User" table="t_user">
<id name="id" column="id">
<generator class="foreign">
<param name="property">idCard</param>
</generator>
</id>
<!-- property是配置类的属性 name指属性名 -->
<property name="name" length="40"/>
<property name="age" />
<one-to-one name="idCard" constrained="true"></one-to-one>
</class>
</hibernate-mapping>
IdCard.hbm.xml
```java
<hibernate-mapping>
<!-- class配置类 name指类名 table指定表名 如果不写,默认类名为表名-->
<class name="cn.sxt.pojo.IdCard" table="t_idCard">
<id name="id" column="id">
<generator class="assigned">
</generator>
</id>
<property name="address"/>
<one-to-one name="user"/>
</class>
</hibernate-mapping>
4、测试
public class HibernateTest {
@Test
public void testCreateDB(){
Configuration cfg = new Configuration().configure();
SchemaExport se = new SchemaExport(cfg);
//第一个参数 是否打印脚本,第二个参数是否导入数据库
se.create(true, true);
}
@Test
public void testInit(){
Session session = null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
IdCard idCard = new IdCard();
idCard.setId("321023123123123");
idCard.setAddress("北京");
IdCard idCard1 = new IdCard();
idCard1.setId("654352345243523");
idCard1.setAddress("天津");
session.save(idCard);
session.save(idCard1);
User u1 = new User("张三",23,idCard);
User u2 = new User("李四",22,idCard1);
session.save(u1);
session.save(u2);
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
@Test
public void testGet(){
Session session = null;
try{
session = HibernateUtil.getSession();
session.beginTransaction();
User user = (User)session.get(User.class, "321023123123123");
System.out.println("姓名:"+user.getName()+" 身份证:"+user.getIdCard().getId()+" 地址:"+user.getIdCard().getAddress());
System.out.println("==================");
IdCard card = (IdCard)session.get(IdCard.class, "321023123123123");
System.out.println("身份证:"+card.getId()+" 地址:"+card.getAddress()+" 姓名:"+card.getUser().getName());
session.getTransaction().commit();
}catch(Exception e){
e.printStackTrace();
session.getTransaction().rollback();
}finally{
if(session!=null){
session.close();
}
}
}
}