Hibernate Map映射文件

Map 的第一种结构 Map<Integer,String>


Team.java


package com.wansha.pojo; /** * 一个班级team 由多个学生Map组成,map分为键(学号),值(学生姓名) */ import java.util.HashMap; import java.util.Map; public class Team { private int id; private String teamName; private Map<Integer,String> student = new HashMap<Integer,String>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTeamName() { return teamName; } public void setTeamName(String teamName) { this.teamName = teamName; } public Map<Integer, String> getStudent() { return student; } public void setStudent(Map<Integer, String> student) { this.student = student; } }

Team.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.wansha.pojo.Team" table="team"> <id name="id" column="id" type="int"> <generator class="identity"></generator> </id> <property name="teamName" column="teamName" type="string" /> <map name="student" table="student"> <key column="team_id" /> <index column="student_num" type="int"/> <element column="student_name" type="string"></element> </map> </class> </hibernate-mapping>
Test.java


package com.wansha.util; import org.hibernate.Session; import org.hibernate.Transaction; import com.wansha.pojo.Team; public class Test { public static void main(String[] args) { /* Team team = new Team(); team.setTeamName("team 1"); team.getStudent().put(12, "sharp"); team.getStudent().put(13, "hailie"); team.getStudent().put(15, "dudu"); Session session = HibernateUtil.getSession(); Transaction ts = null; try{ ts = session.beginTransaction(); session.save(team); ts.commit(); }catch(Exception ex){ if(ts != null){ ts.rollback(); } }finally{ HibernateUtil.close(session); }*/ Session session = HibernateUtil.getSession(); Transaction ts = null; try{ ts = session.beginTransaction(); Team team = (Team)session.get(Team.class,1); team.getStudent().remove(12); ts.commit(); }catch(Exception ex){ if(ts != null){ ts.rollback(); } }finally{ HibernateUtil.close(session); } } }



Map 的第二种结构 Map<Integer,Object>



Team.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.wansha.pojo.Team" table="team"> <id name="id" column="id" type="int"> <generator class="identity"></generator> </id> <property name="teamName" column="teamName" type="string" /> <map name="student" table="student" cascade="all" inverse="true" > // lazy="false" 不加也可以通过entryset = map.entrySet();读取student <key column="team_id" /> <index column="cardid" type="int"/> <one-to-many class="com.wansha.pojo.Student"/> </map> </class> </hibernate-mapping>

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.wansha.pojo.Student" table="student"> <id name="id" column="id" type="int"> <generator class="identity"></generator> </id> <property name="name" column="name" type="string" /> <property name="cardid" column="cardid" type="int" /> <many-to-one name="team" class="com.wansha.pojo.Team" column="team_id"/> </class> </hibernate-mapping>
Test.java


package com.wansha.util; import java.util.Map; import java.util.Set; import java.util.Map.Entry; import org.hibernate.Session; import org.hibernate.Transaction; import com.wansha.pojo.Student; import com.wansha.pojo.Team; public class Test { public static void main(String[] args) { /* Team team = new Team(); team.setTeamName("team 1"); Student student1 = new Student(); student1.setCardid(111); student1.setName("sharp"); student1.setTeam(team); Student student2 = new Student(); student2.setCardid(222); student2.setName("hailie"); student2.setTeam(team); Student student3 = new Student(); student3.setCardid(333); student3.setName("dudu"); student3.setTeam(team); team.getStudent().put(student1.getCardid(), student1); team.getStudent().put(student2.getCardid(), student2); team.getStudent().put(student3.getCardid(), student3); Session session = HibernateUtil.getSession(); Transaction ts = null; try{ ts = session.beginTransaction(); session.save(team); ts.commit(); }catch(Exception ex){ if(ts != null){ ts.rollback(); } }finally{ HibernateUtil.close(session); }*/ //-------------------------------------------------------------------------------- Session session = HibernateUtil.getSession(); Transaction ts = null; Set<Entry<Integer, Student>> entryset = null; try{ ts = session.beginTransaction(); Team team = (Team)session.get(Team.class,2); Map<Integer, Student> map = team.getStudent(); entryset = map.entrySet(); ts.commit(); }catch(Exception ex){ if(ts != null){ ts.rollback(); } }finally{ HibernateUtil.close(session); } for(Entry<Integer, Student> entry: entryset){ System.out.println(((Student)entry.getValue()).getName()); } } }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值