当前页面: 开发资料首页 → Eclipse 专题 → Eclipse快速上手Hibernate--4. 继承映射(2)
摘要: Eclipse快速上手Hibernate--4. 继承映射(2)
Vehicle.java
/* * Hibernate - 继承映射(每个子类一个表) * 创建日期 2005-4-9 * @author javamxj(分享java快乐) * @link Blog: htpp://javamxj.mblogger.cn * htpp://blog.csdn.net/javamxj/ */ package javamxj.inheritance.two; /** * @hibernate.class */ public class Vehicle { private Long id; private String name; /** * @hibernate.id * column="ID" * generator-class="hilo" * unsaved-value="null" */ public Long getId() { return id; } public void setId(Long id) { this.id = id; } /** * @hibernate.property * length = "24" */ public String getName() { return name; } public void setName(String name) { this.name = name; } }
</td></tr>package javamxj.inheritance.two; /** * @hibernate.joined-subclass * @hibernate.joined-subclass-key * column="id" */ public class Car extends Vehicle { private String seat; /** * @hibernate.property * column = "载客" * length = "24" */ public String getSeat() { return seat; } public void setSeat(String seat) { this.seat = seat; } }
</td></tr>package javamxj.inheritance.two; /** * @hibernate.joined-subclass * @hibernate.joined-subclass-key * column="id" */ public class Truck extends Vehicle { private String load; /** * @hibernate.property * column = "载重" * length = "24" */ public String getLoad() { return load; } public void setLoad(String load) { this.load = load; } }
Vehicle.hbm.xml
<?xml version="1.0" encoding="GBK"?> hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">To add non XDoclet generator parameters, create a file named hibernate-generator-params-Vehicle.xml containing the additional parameters and place it in your merge dir. To add non XDoclet property mappings, create a file named hibernate-properties-Vehicle.xml containing the additional properties and place it in your merge dir.
Demo.java
/* * Hibernate - 继承映射(每个子类一个表) * 创建日期 2005-4-9 * @author javamxj(分享java快乐) * @link Blog: htpp://javamxj.mblogger.cn * htpp://blog.csdn.net/javamxj/ */ package javamxj.inheritance.two; import java.util.Iterator; import java.util.List; import net.sf.hibernate.HibernateException; import net.sf.hibernate.Session; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.Transaction; import net.sf.hibernate.cfg.Configuration; public class Demo { public static void main(String[] args) { try { new Demo(); } catch (HibernateException he) { he.printStackTrace(); } } public Demo() throws HibernateException { SessionFactory sf = new Configuration().configure() .buildSessionFactory(); Session sess = sf.openSession(); Transaction tx = null; try { tx = sess.beginTransaction(); Car car = new Car(); car.setName("奇瑞QQ"); car.setSeat("4人"); sess.save(car); Truck truck = new Truck(); truck.setName("一汽解放"); truck.setLoad("10吨"); sess.save(truck); tx.commit(); } catch (HibernateException e) { if (tx != null) tx.rollback(); throw e; } finally { sess.close(); } sess = sf.openSession(); tx = null; try { tx = sess.beginTransaction(); List pets = sess.find("from " + Vehicle.class.getName()); for (Iterator it = pets.iterator(); it.hasNext();) { Vehicle vehicle = (Vehicle) it.next(); System.out.println("车型 " + vehicle.getName() + " its class is: " + vehicle.getClass().getName()); } tx.commit(); } catch (HibernateException e) { if (tx != null) tx.rollback(); throw e; } finally { sess.close(); } } }