特征总结:
集合映射(值类型集合)
Set
集合内的对象不排序,没有重复对象
List
集合内的对象按照索引位置排序,可以有重复对象
Map
集合内的每一个元素包含一对键对象和值对象,集合中没有重复的键对象,可以有重复的值对象。
Map中的key是唯一的,当出现重复的就会出现覆盖操作了的;
-------------------------------------------------------------------------------------------------------------------------------------------
代码分析:
映射Set:
1、实体类CS_Student
import
java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* CS_Student entity. @author MyEclipse Persistence Tools
*/
public class
CS_Student
implements
java.io.Serializable {
// Fields
private Long id;
private String name;
private String email;
private Set ccolTelephones;
// Constructors
/** default constructor */
public CS_Student() {
}
/** full constructor */
public CS_Student(String name, String email, Set ccolTelephones) {
this.name = name;
this.email = email;
this.ccolTelephones = ccolTelephones;
}
// Property accessors
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public Set getCcolTelephones() {
return this.ccolTelephones;
}
public void setCcolTelephones(Set ccolTelephones) {
this.ccolTelephones = ccolTelephones;
}
public String toString(){
StringBuffer buf = new StringBuffer("it is: ");
buf.append(" id=").append(id);
buf.append(" name=").append(name);
buf.append(" email=").append(email);
buf.append(" telephones=[");
boolean flag = false;
Iterator iter = ccolTelephones.iterator();
while(iter.hasNext()){
buf.append(iter.next()).append(", ");
flag = true;
}
if(flag){//处理电话号码的字符串
buf.delete(buf.length()-2, buf.length());
buf.append("]");
}
return buf.toString();
}
}
2、映射表
CCOL_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>
<class name="com.neusoft.entity.CS_Student" table="cs_student">
<id name="id" type="java.lang.Long">
<column name="id"/>
<generator class="native"/>
</id>
<property name="name" column="student_name" type="java.lang.String" length="20"/>
<property name="email" column="email" type="java.lang.String" length="50"/>
<!-- set -->
<!-- lazy="false|true" 默认是true -->
<set name="ccolTelephones" table="ccol_telephone" lazy="true" fetch="select" order-by="lower(telephone) desc">
<key column="student_id" not-null="true"/>
<element column="telephone" type="java.lang.String"></element>
</set>
</class>
</hibernate-mapping>
3、正向工程的初始化表操作:
【(
是否自动创建表,取代配置文件中
)】
-----------------------------------------------------------
方式一:
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class InitEnv {
/**
* @param args
*/
public static void main(String[] args) {
//初始化: 1、创建table
Configuration cf = new Configuration().configure();
SchemaExport se = new SchemaExport(cf);
se.create(true, true);
System.out.println("------init ok.");
}
}
-----------------------------------------------------------
方式二:在hibernate.cfg.xml文件下配置创建表操作:
4、
使用插入语句持久化操作
public static void doInsert(){
//1、获取Session
Session session = HibernateUtil.getSession();
//2、构造对象
CS_Student stu = new CS_Student();
stu.setName("zhangsan");
stu.setEmail("1@2.3");
Set ccolTelephones = new HashSet();
ccolTelephones.add("13988888888");
ccolTelephones.add("0411-88888888");
ccolTelephones.add("13988888887");
stu.setCcolTelephones(ccolTelephones);
//3、保存对象
session.save(stu);
//4、提交操作
Transaction ts = session.beginTransaction();
ts.commit();
System.out.println("---save ok.");
//5、关闭资源
session.close();
}
----------------------------------------------------------------
5、Set映射的插入结果显示:
并不与第4步java中的插入代码一致顺序,其为无序,
6
、温馨提示,对于Hibernate中的其他集合映射的特征,可按以上所说方式去验证,这里就不多说了,多动手多实践才行。