Hibernate-------集合

Hibernate中的集合
Hibernate中的集合分为四类:
1.bag类:元素无序,可重复
2.set类:元素无序,不可重复
3.list类:元素有序,可重复
4.array类:数组
5.map类:映射关系
除bag类集合外,其它集合都可以在java api中找到对应的接口类,[color=red]bag可以认为对应java中的List[/color]
Hibernate中将集合分为2类:
一是值集合,集合中存储的都是基本数据类型,包括String类型,及其它可识别的数据库类型
二是实体集合,集合中存储已经映射了的实体对象,反应对象间的关系。

以班级和学生为例:
建表sql语句:


CREATE DATABASE `hibernatetest`
CHARACTER SET 'utf8';

USE `hibernatetest`;

DROP TABLE IF EXISTS `classes`;
CREATE TABLE `classes` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`classId` int(11) ,
foreign key (`classId`) references classes(`id`),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Classes.java

package com.domain;

import java.util.ArrayList;
import java.util.List;

public class Classes implements java.io.Serializable {
private Integer id;

private String name;

//Hibernate里的bag集合对应java里的List
private List studentNames = new ArrayList();

public Classes() {
}

public Classes(String name) {
this.name = name;
}


public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return this.name;
}

public void setName(String name) {
this.name = name;
}

public List getStudentNames() {
return studentNames;
}

public void setStudentNames(List studentNames) {
this.studentNames = studentNames;
}

}

Student.java:

package com.domain;

public class Student implements java.io.Serializable {

private Integer id;

private String name;

public Student() {
}

public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}

}

Classes.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.domain.Classes" table="classes" >
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" not-null="true" />
</property>
<bag name="studentNames" table="student">
<!-- key对应的都是外键,其意思也就是拿该外键和<id>的主键值匹配,取集合 -->
<key column="classId"></key>
<element column="name" type="string"></element>
</bag>

</class>
</hibernate-mapping>


Student.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 package="com.domain" auto-import="true">
<class name="Student" table="student" >
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>

<property name="name" type="java.lang.String">
<column name="name" not-null="true" />
</property>
</class>
</hibernate-mapping>

测试类:

package com.dao;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.domain.Classes;
import com.domain.Student;

public class TestInsert {

public static void main(String[] args) {
Session session = HibernateSessionFactory.getSession();
Transaction tx = session.beginTransaction();
//定义班级0201班
Classes classes = new Classes();
classes.setName("0201new");
classes.getStudentNames().add("studentnew1");
classes.getStudentNames().add("studentnew2");
session.save(classes);


tx.commit();
session.close();
/**
执行的sql:
Hibernate: insert into classes (name) values (?)
Hibernate: insert into student (classId, name) values (?, ?)
Hibernate: insert into student (classId, name) values (?, ?)
*/


}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值