HIbernate 继承关系实践一

环境: hibernate-2.1   hibernate-extentions-2.1.3   Middlegen-hibernate-r5 

http://internap.dl.sourceforge.net/sourceforge/hibernate/Middlegen-Hibernate-r5.zip
可以生成*.hbm.xml文件,是hibernate所需的映射文件,下载Middlegen Hibernate后解压即可。

 

1  一对一关系   "表与子类之间独立的一对一关系"

 

Gitem  的两个子类 Gdvd 和 Gbook  ,公共属性为id ,name

 

在MySql里创建两张表g_dvd和g_book 

 

使用middlegen和hbm2java命令生成pojo和hbm配置文件

 

package nl.inherit; public class GItem { private int id; private String name; /** * @param id the id to set */ public void setId(int id) { this.id = id; } /** * @return the id */ public int getId() { return id; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the name */ public String getName() { return name; } }

 

package nl.inherit; import java.io.Serializable; import org.apache.commons.lang.builder.ToStringBuilder; /** * @hibernate.class * table="g_dvd" * */ public class Gdvd extends GItem implements Serializable { /** nullable persistent field */ private String regionCode; /** default constructor */ public Gdvd() { } /** * @hibernate.property * column="regionCode" * length="100" * */ public String getRegionCode() { return this.regionCode; } public void setRegionCode(String regionCode) { this.regionCode = regionCode; } public String toString() { return new ToStringBuilder(this) .append("id", getId()) .toString(); } }

 

package nl.inherit; import java.io.Serializable; import org.apache.commons.lang.builder.ToStringBuilder; /** * @hibernate.class * table="g_book" * */ public class Gbook extends GItem implements Serializable { /** nullable persistent field */ private Integer pageCount; /** default constructor */ public Gbook() { } /** * @hibernate.property * column="pageCount" * length="10" * */ public Integer getPageCount() { return this.pageCount; } public void setPageCount(Integer pageCount) { this.pageCount = pageCount; } public String toString() { return new ToStringBuilder(this) .append("id", getId()) .toString(); } }

 

hibernate.hbm.xml

 

 <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-2.0.dtd"> <hibernate-configuration> <session-factory name="foo"> <mapping resource="nl/inherit/Gdvd.hbm.xml"/> <mapping resource="nl/inherit/Gbook.hbm.xml"/> </session-factory> </hibernate-configuration>

 

 package test; import java.util.Iterator; import java.util.List; import junit.framework.TestCase; import net.sf.hibernate.HibernateException; import net.sf.hibernate.Session; import net.sf.hibernate.SessionFactory; import net.sf.hibernate.cfg.Configuration; import nl.inherit.Gbook; import nl.inherit.Gdvd; public class MoreTableTest extends TestCase { protected void tearDown() throws Exception { super.tearDown(); seesion.close(); } Session seesion = null; public void init() { System.out.println("test......."); } protected void setUp() throws Exception { super.setUp(); Configuration configuration = new Configuration().configure(); SessionFactory factory = configuration.buildSessionFactory(); seesion = factory.openSession(); } public void testItem(){ try { List list=seesion.createQuery("from nl.inherit.GItem").list(); Iterator item=list.iterator(); System.out.println(item.hasNext()); while(item.hasNext()) { Object it=item.next(); if(it instanceof Gbook) { System.out.println("Gbook:"+((Gbook)it).getPageCount()); }else if (it instanceof Gdvd) { System.out.println("Gdvd:"+((Gdvd)it).getRegionCode()); } } } catch (HibernateException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

 

通过继承GItem,实现对两张表的同时查询

 

 

 

你可能感兴趣的:(Hibernate,exception,String,Integer,Class,Constructor)