HIbernate Y映射 Map、list、set
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(); }
<hibernate-mapping> <class name="com.liule.hibernate.Team" table="team"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="name" type="string"> <column name="name" length="50"></column> </property> <map name="students" table="team_student"> <!-- table:存放map内的key-value对的值 --> <key column="team_id"></key> <!-- 在team_student表内靠team_id联系到team表 --> <index column="name" type="string"></index> <!-- 在map内key的值 --> <element column="description" type="string" ></element> <!-- map内value的值 --> </map> </class> </hibernate-mapping>
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; }
<hibernate-mapping> <class name="com.liule.hibernate.Team" table="team"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="name" type="string"> <column name="name" length="50"></column> </property> <map name="students" table="team_student" cascade="all"> <!-- table:存放map内的key-value对的值 --> <key column="team_id"></key> <!-- 在team_student表内靠team_id联系到team表 --> <index column="card_id" type="string"></index> <!-- 在map内key的值 --> <one-to-many class="com.liule.hibernate.Student"/><!-- map内value的值 --> </map> </class> </hibernate-mapping>
<hibernate-mapping> <class name="com.liule.hibernate.Student" table="Student"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="name" type="string"> <column name="name" length="50"></column> </property> <property name="cardId" column="card_id" type="string"></property><!-- key --> <property name="age" column="age" type="int"></property> <many-to-one name="team" column="team_id" class="com.liule.hibernate.Team"></many-to-one> </class> </hibernate-mapping>
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(); }
<hibernate-mapping> <class name="com.liule.hibernate.Team" table="team"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="name" type="string"> <column name="name" length="50"></column> </property> <set name="students" table="student" cascade="all"> <!-- 将set内容,设置在team_table表 --> <key column="team_id"></key> <element column="name" type="string"></element><!-- column映射到name这一列 --> </set> </class> </hibernate-mapping>
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;
<hibernate-mapping> <class name="com.liule.hibernate.Team" table="team"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="name" type="string"> <column name="name" length="50"></column> </property> <list name="students" table="student" cascade="all"> <key column="team_id"></key> <index column="index_"></index><!-- 所存放的是索引的位置 --> <one-to-many class="com.liule.hibernate.Student"/> </list> </class> </hibernate-mapping>
<hibernate-mapping> <class name="com.liule.hibernate.Student" table="Student"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="name" type="string"> <column name="name" length="50"></column> </property> <property name="cardId" column="card_id" type="string"></property><!-- key --> <property name="age" column="age" type="int"></property> <many-to-one name="team" column="team_id" class="com.liule.hibernate.Team"></many-to-one> </class> </hibernate-mapping>
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(); } }