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语句:
Classes.java
Student.java:
Classes.hbm.xml:
Student.hbm.xml
测试类:
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 (?, ?)
*/
}
}