Set无序 元素不可重复
List有序 元素可重复
Bag无序 元素可重复
Map键值对
Student:
package model;
import java.util.Set;
public class Student {
private long id;
private String name;
private Set<String> images;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<String> getImages() {
return images;
}
public void setImages(Set<String> images) {
this.images = images;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="model">
<class name="Student" table="t_student">
<id name="id" column="stuId">
<generator class="native"></generator>
</id>
<property name="name" column="stuName"></property>
<set name="images" table="t_image">
<key column="studentId"></key>
<element column="imageName" type="string"></element>
</set>
</class>
</hibernate-mapping>
Student2:
package model;
import java.util.List;
public class Student2 {
private long id;
private String name;
private List<String> images;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getImages() {
return images;
}
public void setImages(List<String> images) {
this.images = images;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="model">
<class name="Student2" table="t_student">
<id name="id" column="stuId">
<generator class="native"></generator>
</id>
<property name="name" column="stuName"></property>
<list name="images" table="t_image2">
<key column="studentId"></key>
<list-index column="imageIndex"></list-index>
<element column="imageName" type="string"></element>
</list>
</class>
</hibernate-mapping>
Student3:
package model;
import java.util.List;
public class Student3 {
private long id;
private String name;
private List<String> images;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getImages() {
return images;
}
public void setImages(List<String> images) {
this.images = images;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="model">
<class name="Student3" table="t_student">
<id name="id" column="stuId">
<generator class="native"></generator>
</id>
<property name="name" column="stuName"></property>
<idbag name="images" table="t_image3">
<collection-id type="long" column="imageId">
<generator class="increment"></generator>
</collection-id>
<key column="studentId"></key>
<element column="imageName" type="string"></element>
</idbag>
</class>
</hibernate-mapping>
Student4:
package model;
import java.util.Map;
public class Student4 {
private long id;
private String name;
private Map<String,String>images;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Map<String, String> getImages() {
return images;
}
public void setImages(Map<String, String> images) {
this.images = images;
}
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="model">
<class name="Student4" table="t_student">
<id name="id" column="stuId">
<generator class="native"></generator>
</id>
<property name="name" column="stuName"></property>
<map name="images" table="t_image4">
<key column="studentId"></key>
<map-key type="string" column="imageKey"></map-key>
<element column="imageName" type="string"></element>
</map>
</class>
</hibernate-mapping>
StudentTest:
package service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import util.HibernateUtil;
import model.Student;
import model.Student2;
import model.Student3;
import model.Student4;
public class StudentTest {
SessionFactory sessionfactory=HibernateUtil.getSessionFactory();//获取Session工厂
private Session session;
@Before
public void setUp() throws Exception {
session=sessionfactory.openSession();//生成一个新的session
session.beginTransaction();//开启事务
}
@After
public void tearDown() throws Exception {
session.getTransaction().commit();//提交事务
session.close();//关闭session
}
@Test
public void testSetSave(){
Set<String> imageSet=new HashSet<String>();
imageSet.add("image1.png");
imageSet.add("image2.png");
imageSet.add("image3.png");
imageSet.add("image4.png");
Student s1=new Student();
s1.setImages(imageSet);
session.save(s1);
}
@Test
public void testSetFetch(){
Student student=(Student) session.get(Student.class,Long.valueOf(1));
Iterator it=student.getImages().iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
@Test
public void testListSave(){
List<String> imageList=new ArrayList<String>();
imageList.add("image1.png");
imageList.add("image2.png");
imageList.add("image3.png");
imageList.add("image4.png");
Student2 s2=new Student2();
s2.setImages(imageList);
session.save(s2);
}
@Test
public void testListFetch(){
Student2 student2=(Student2) session.get(Student2.class,Long.valueOf(2));
Iterator it=student2.getImages().iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
@Test
public void testBagSave(){
List<String> imageList=new ArrayList<String>();
imageList.add("image1.png");
imageList.add("image2.png");
imageList.add("image3.png");
imageList.add("image4.png");
Student3 s3=new Student3();
s3.setImages(imageList);
session.save(s3);
}
@Test
public void testBagFetch(){
Student3 student3=(Student3) session.get(Student3.class,Long.valueOf(3));
Iterator it=student3.getImages().iterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
@Test
public void testMapSave(){
Map<String,String>imageMap=new HashMap<String,String>();
imageMap.put("i1","image1.png");
imageMap.put("i2","image2.png");
imageMap.put("i3","image3.png");
imageMap.put("i4","image4.png");
Student4 s4=new Student4();
s4.setImages(imageMap);
session.save(s4);
}
@Test
public void testMapFetch(){
Student4 student4=(Student4) session.get(Student4.class,Long.valueOf(4));
Map<String,String>imageMap=student4.getImages();
Set keys=imageMap.keySet();
Iterator it=keys.iterator();
while(it.hasNext()){
String key=(String) it.next();
System.out.println(key+":"+imageMap.get(key));
}
}
}
转载于:https://www.cnblogs.com/daochong/p/4948708.html