1.总
- set:不允许重复、无序
- list:有序、重复
- map:key-value对形式
2.map与set标签中的element子标签映射的是原子类型(string、data、int、long...),即就是能够直接映射到数据库表字段上的类型,而one-to-many映射的则是实体类型,指的是无法映射到表的某个字段,而是要映射到整张表的类型。
3.map
(1)原子类型
public class Team{ private String id; private String name; private Map students = new HashMap();}
public static void main(String[] args) { //生成表 SchemaExport export = new SchemaExport(new Configuration().configure()); export.create(true,true); }
Team team = new Team(); team.setName("team"); Map map = team.getStudents(); map.put("zhangsan","sdas"); map.put("lisi","sadasd"); Session session = sf.openSession(); Transaction ts = null; try { ts = session.beginTransaction(); session.save(team); ts.commit(); } catch(Exception ex) { if(null != ts) { ts.rollback(); } } finally { session.close(); }
(2)实体类型
public class Team{ private String id; private String name; private Map students = new HashMap();}
public class Student{ private String id; private String name; private String cardId; private int age; private Team team;}
public static void main(String[] args) { Team team = new Team(); team.setName("team1"); Student student = new Student(); student.setAge(12); student.setName("liule"); student.setTeam(team); team.getStudents().put("123456",student); Session session = sf.openSession(); Transaction ts = null; try { session.save(team); ts = session.beginTransaction(); ts.commit(); } catch(Exception ex) { ex.printStackTrace(); if(null != ts) { ts.rollback(); } } finally { session.close(); } }
4.set
public class Team{ private String id; private String name; private Set students = new HashSet();}
public static void main(String[] args) { Team team = new Team(); team.setName("team1"); team.getStudents().add("zhangsan"); Session session = sf.openSession(); Transaction ts = null; try { session.save(team); ts = session.beginTransaction(); ts.commit(); } catch(Exception ex) { ex.printStackTrace(); if(null != ts) { ts.rollback(); } } finally { session.close(); } }
5.list
public class Team{ private String id; private String name; private List students = new ArrayList();}
public class Student{ private String id; private String name; private String cardId; private int age; private Team team;
public static void main(String[] args) { Team team = new Team(); team.setName("team1"); Student student1 = new Student(); Student student2 = new Student(); Student student3 = new Student(); student1.setAge(10); student1.setCardId("asd"); student1.setName("zhangsan"); student1.setTeam(team); student2.setAge(11); student2.setCardId("asdsa"); student2.setName("lisi"); student2.setTeam(team); student3.setAge(12); student3.setCardId("asasad"); student3.setName("wangwu"); student3.setTeam(team); team.getStudents().add(student1); team.getStudents().add(student2); team.getStudents().add(student3); Session session = sf.openSession(); session.save(team); Transaction ts = null; try { session.save(team); ts = session.beginTransaction(); ts.commit(); } catch(Exception ex) { ex.printStackTrace(); if(null != ts) { ts.rollback(); } } finally { session.close(); } }