Hibernate逍遥游记-第10章 映射继承关系-001继承关系树中的每个具体类对应一个表
2016-03-20 17:04
701 查看
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
2.
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping > <class name="mypack.CMonkey" table="CMONKEYS"> <id name="id" type="long" column="ID"> <generator class="increment"/> </id> <property name="name" type="string" column="NAME" /> <property name="length" column="LENGTH" type="double" /> <many-to-one name="team" column="TEAM_ID" class="mypack.Team" /> </class> </hibernate-mapping>
3.
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping > <class name="mypack.JMonkey" table="JMONKEYS"> <id name="id" type="long" column="ID"> <generator class="increment"/> </id> <property name="name" type="string" column="NAME" /> <property name="color" column="COLOR" type="string" /> <many-to-one name="team" column="TEAM_ID" class="mypack.Team" /> </class> </hibernate-mapping>
4.
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping > <class name="mypack.Team" table="TEAMS" > <id name="id" type="long" column="ID"> <generator class="increment"/> </id> <property name="name" type="string" column="NAME" /> </class> </hibernate-mapping>
5.
package mypack; abstract public class Monkey{ private Long id; private String name; private Team team; /** full constructor */ public Monkey(String name,Team team) { this.name = name; this.team = team; } /** default constructor */ public Monkey() { } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Team getTeam() { return this.team; } public void setTeam(Team team) { this.team = team; } }
6.
package mypack; public class CMonkey extends Monkey { private double length; /** full constructor */ public CMonkey(String name, double length,Team team) { super(name,team); this.length=length; } /** default constructor */ public CMonkey() { } public double getLength() { return this.length; } public void setLength(double length) { this.length = length; } }
7.
package mypack; public class JMonkey extends Monkey{ private String color; /** full constructor */ public JMonkey(String name, String color,Team team) { super(name,team); this.color=color; } /** default constructor */ public JMonkey() { } public String getColor() { return this.color; } public void setColor(String color) { this.color = color; } }
8.
package mypack; import java.util.Set; import java.util.HashSet; public class Team{ private Long id; private String name; private Set monkeys=new HashSet(); /** full constructor */ public Team(String name, Set monkeys) { this.name = name; this.monkeys = monkeys; } /** default constructor */ public Team() { } /** minimal constructor */ public Team(Set monkeys) { this.monkeys = monkeys; } public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Set getMonkeys() { return this.monkeys; } public void setMonkeys(Set monkeys) { this.monkeys = monkeys; } }
9.
package mypack; import org.hibernate.*; import org.hibernate.cfg.Configuration; import java.util.*; import java.sql.*; public class BusinessService{ public static SessionFactory sessionFactory; static{ try{ Configuration config = new Configuration().configure(); sessionFactory = config.buildSessionFactory(); }catch(RuntimeException e){e.printStackTrace();throw e;} } public void saveMonkey(Monkey monkey){ Session session = sessionFactory.openSession(); Transaction tx = null; List results=new ArrayList(); try { tx = session.beginTransaction(); session.save(monkey); tx.commit(); }catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } } public List findAllMonkeys(){ Session session = sessionFactory.openSession(); Transaction tx = null; try { List results=new ArrayList(); tx = session.beginTransaction(); List jMonkeys=session.createQuery("from JMonkey").list(); results.addAll(jMonkeys); List cMonkeys=session.createQuery("from CMonkey").list(); results.addAll(cMonkeys); tx.commit(); return results; }catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } } public Team loadTeam(long id) { Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); Team team=(Team)session.get(Team.class,new Long(id)); List jMonkeys=session .createQuery("from JMonkey j where j.team.id="+id) .list(); team.getMonkeys().addAll(jMonkeys); List cMonkeys=session .createQuery("from CMonkey c where c.team.id="+id) .list(); team.getMonkeys().addAll(cMonkeys); tx.commit(); return team; }catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } } public void test() { List monkeys=findAllMonkeys(); printAllMonkeys(monkeys.iterator()); Team team=loadTeam(1); printAllMonkeys(team.getMonkeys().iterator()); Monkey monkey=new JMonkey("Mary","yellow",team); saveMonkey(monkey); } private void printAllMonkeys(Iterator it){ while(it.hasNext()){ Monkey m=(Monkey)it.next(); if(m instanceof JMonkey) System.out.println(((JMonkey)m).getColor()); else System.out.println(((CMonkey)m).getLength()); } } public static void main(String args[]) { new BusinessService().test(); sessionFactory.close(); } }
10.
drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; create table TEAMS ( ID bigint not null, NAME varchar(15), primary key (ID) ); create table JMONKEYS ( ID bigint not null, NAME varchar(15), COLOR varchar(15), TEAM_ID bigint, primary key (ID) ); create table CMONKEYS ( ID bigint not null, NAME varchar(15), LENGTH double precision, TEAM_ID bigint, primary key (ID) ); alter table JMONKEYS add index IDX1_TEAM(TEAM_ID), add constraint FK1_TEAM foreign key (TEAM_ID) references TEAMS (ID); alter table CMONKEYS add index IDX2_TEAM(TEAM_ID), add constraint FK2_TEAM foreign key (TEAM_ID) references TEAMS (ID); insert into TEAMS(ID,NAME) values(1,'NBA'); insert into JMONKEYS(ID,NAME,COLOR,TEAM_ID) values(1,'Tom','yellow',1); insert into JMONKEYS(ID,NAME,COLOR,TEAM_ID) values(2,'Mike','orange',1); insert into CMONKEYS(ID,NAME,LENGTH,TEAM_ID) values(1,'Jack',1.2,1); insert into CMONKEYS(ID,NAME,LENGTH,TEAM_ID) values(2,'Linda',2.0,1);
11.
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="connection.url"> jdbc:mysql://localhost:3306/sampledb </property> <property name="connection.username"> root </property> <property name="connection.password"> 1234 </property> <property name="show_sql">true</property> <mapping resource="mypack/Team.hbm.xml" /> <mapping resource="mypack/JMonkey.hbm.xml" /> <mapping resource="mypack/CMonkey.hbm.xml" /> </session-factory> </hibernate-configuration>
相关文章推荐
- 阿里云Linux系统Nginx配置多个域名的方法
- leetcod--Climbing Stairs
- C#中protected用法详解
- Linux环境下使用GDB调试C程序
- Python Singleton Summary
- STL 函数对象
- 项目总结之关于系统通讯录的操作
- 20142335郝昊第三周学习总结
- 猴子吃桃问题
- 电路常识性概念(3)-TTL与CMOS集成电路
- PHP zend 常用快捷键汇总
- GO语言入门到精通(一)Golang环境搭建
- 136. Single Number
- AsyncTask不会执行doInBackground 的原因
- 集中式总线
- Android利用HttpClient发送Json到后台
- Web负载均衡的几种实现方式
- 98.公共汽车
- 《深入.NET平台和C#编程》内部测试题
- 数据存储和界面展示(一)