新手发帖,很多方面都是刚入门,有错误的地方请大家见谅,欢迎批评指正
在hibernate中,有几种不同的映射文件,这几种映射文件各有特点,set在后面已经说过,这种映射关系多的一方中没有次序关系,并且其中也不允许有重复的值,但是在list中,这两个刚好相反,list允许有重复的值,并且多的一方数据在持久化到数据中时也强调了数据的次序,而bag中和了list和set的特点,bag允许有不必的值,但是这些值之间没有次序,而map是一种键值对的结构,根据值得不同情况,又可以分为两种,一种情况是map的value可以直接对应到数据库中的字段类型,例如string,int,char,boolean等等,还有一种map的value本身是一个实体类,那么value 将被持久化到另外一个数据库表中。上面将对上面所说的情况来逐一说明。
public class House { private int id; private String location; private List rooms; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } public List getRooms() { return rooms; } public void setRooms(List rooms) { this.rooms = rooms; } }
public class Room { private String id; private String number; private House house; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public House getHouse() { return house; } public void setHouse(House house) { this.house = house; } }
上面是两个类的映射文件。
House.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.example.domain.House" table="house"> <id name="id" column="id" type="int"> <generator class="increment"></generator> </id> <property name="location" column="location" type="java.lang.String"></property> <list name="rooms" table="rooms" cascade="all" > <key column="house_id"></key> <index column="index_"></index> <one-to-many class="com.example.domain.Room"/> </list> </class> </hibernate-mapping>
其中须要懂得的就是list标签, name 属性表示对应House类中表示list对象的变量名,table表示了用数据库中那张表来存放list中所对应的对象,key表示 rooms表中的外键名称是什么,index标签是一个必须的标签,其中column指定了在rooms中的哪一列来存放list中元素进入数据库的次序,那么在示例中指定了index_列来存放书次序,这个列的值是从0开始的连续整数。因为list表示的本质上也是一种一对多的关系,那么还须要加上一个one-to-many标签,class来指定list中元素的类。
room.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.example.domain.Room" table="rooms"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="number" column="number" type="string"></property> <many-to-one name="house" class="com.example.domain.House" column="house_id"></many-to-one> </class> </hibernate-mapping>
上面是测试的代码
public class ListTest { /** * @param args */ public static void main(String[] args) { Session session = HibernateSessionFactory.openSession(); Transaction tx = session.beginTransaction(); House house = new House(); house.setLocation("ShiShou city,Hb Province,China"); house.setRooms(new ArrayList()); Room roomA = new Room(); roomA.setNumber("510"); Room roomB = new Room(); roomB.setNumber("502"); house.getRooms().add(roomA); house.getRooms().add(roomB); try { session.save(house); tx.commit(); } catch(Exception ex) { ex.printStackTrace(); if(tx != null) { tx.rollback(); } } finally { session.close(); } } }
<?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.example.domain.House" table="house"> <id name="id" column="id" type="int"> <generator class="increment"></generator> </id> <property name="location" column="location" type="java.lang.String"></property> <bag name="rooms" table="rooms" cascade="all"> <key column="house_id"></key> <one-to-many class="com.example.domain.Room"/> </bag> </class> </hibernate-mapping>
与list比拟,只是少了一个index标签,这也就表明白bag是没有次序,那么雷同的程序运行结果截图如下。
第一种情况,这种情况下map的key-value中的value为可以直接映射到数据库中的类型,例如string(数据库中为char,varchar等)。上面以班级为例,班级中有students, 类型为map<String,String>,上面是banji类的代码
package com.example.domain; import java.util.HashMap; import java.util.Map; public class Banji { private String id; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } private Map <String,String> students = new HashMap<String,String>(); public String getId() { return id; } public void setId(String id) { this.id = id; } public Map<String, String> getStudents() { return students; } public void setStudents(Map<String, String> students) { this.students = students; } }
在map的value为string等类型的时候只须要配置一个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.example.domain.Banji" table="banji"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="name" column="name" type="string"></property> <map name="students" > <key column="banji_id" ></key> <index column="stu_id" type="string"></index> <element column="stu_name" type="string"></element> </map> </class> </hibernate-mapping>
name为banji 类中map类型的变量名,map中可以指定一个table表示map键值对存放的表,如果不显式说明,那么将创立一个名为name属性的值得数据表。key 依然是表示students表(默认创立的表名为students)的外键。index表示map中的key ,column表示表中用表中的哪一列来存放key值,上面的element 为map的value,column同理。同时须要说明的是应用hibernate供给的SchemaExport来生成数据库表的时候,students表的主键是map的key和banji_id,因为这个时候,任何一个信息都没法确认一行数据了,因为可能有多个班级,每个班级里面都有编号一样的学生。
上面是测试的代码:
public class Map1Test { public static void main(String[] args) { Session session = HibernateSessionFactory.openSession(); Transaction tx = session.beginTransaction(); Banji banji = new Banji(); banji.getStudents().put("101001", "zhangsan"); banji.getStudents().put("101002", "lisi"); banji.getStudents().put("101003", "wangwu"); try { session.save(banji); tx.commit(); } catch(Exception ex) { ex.printStackTrace(); if(tx != null) { tx.rollback(); } } finally { session.close(); } } }
另外一种map映射的情况是map的value是一个实体类,即在数据库字段中没有与之相对应的。上面以Club和Player为例说明。上面是pojo类代码
public class Club { private String id; private String name; private Map<String,String> field = new HashMap<String,String>(); private Map<String ,Player> team = new HashMap<String,Player>(); public Map<String, String> getField() { return field; } public void setField(Map<String, String> field) { this.field = field; } public Map<String, Player> getTeam() { return team; } public void setTeam(Map<String, Player> team) { this.team = team; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
package com.example.domain; public class Player { private String id; private String number; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } private Club club; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public Club getClub() { return club; } public void setClub(Club club) { this.club = club; } }
上面是两个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.example.domain.Club" table="club"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="name" column="name" type="string"></property> <map name="team" table="player" cascade="all"> <key column="team_id"></key> <index column="number" type="string"></index> <one-to-many class="com.example.domain.Player" /> </map> </class> </hibernate-mapping>
那么在map当中,value为一个实体类,那么这种映射本质上还是一种一对多的关系,所以应用了one-to-many标签。
<?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.example.domain.Player" table="player"> <id name="id" column="id" type="string"> <generator class="uuid"></generator> </id> <property name="name" column="name" type="string"></property> <many-to-one name="club" class="com.example.domain.Club" column="team_id" cascade="none"></many-to-one> </class> </hibernate-mapping>
那么在另外一端相对应的就是many-to-one.
import java.util.HashSet; import org.hibernate.Session; import org.hibernate.Transaction; import com.example.domain.Club; import com.example.domain.IdCard; import com.example.domain.Player; import com.example.domain.Student; import com.example.domain.Team; import com.example.util.HibernateSessionFactory; public class Test { public static void main(String args[]) { Club club = new Club(); club.setName("Heat"); Player LBJ = new Player(); LBJ.setClub(club); LBJ.setName("LBJ"); club.getTeam().put("6", LBJ); Session session = HibernateSessionFactory.openSession(); Transaction tx = session.beginTransaction(); try { session.save(club); tx.commit(); } catch(Exception ex) { ex.printStackTrace(); if(tx != null) { tx.rollback(); } } finally { session.close(); } } }
文章结束给大家分享下程序员的一些笑话语录:
问:你觉得让你女朋友(或者任何一个女的)从你和李彦宏之间选一个,你觉得她会选谁?
答:因为李艳红这种败类,所以我没女友!