Hibernate实体层次设计(二)
Table per concrete class方式:
TItem有两个子类:TBook、TDVD。按照Table per concrete class方式,每个字类对应一张数据库表。对应于TBook和TDVD,我们有以下库表:
1.数据库库表
T_Book.sql
DROP
TABLE
T_Book;
CREATE TABLE T_Book (
id INT NOT NULL AUTO_INCREMENT
, name VARCHAR ( 50 )
, manufacturer VARCHAR ( 50 )
, pagecount INT
, PRIMARY KEY (id)
);
CREATE TABLE T_Book (
id INT NOT NULL AUTO_INCREMENT
, name VARCHAR ( 50 )
, manufacturer VARCHAR ( 50 )
, pagecount INT
, PRIMARY KEY (id)
);
T_DVD.sql
DROP
TABLE
T_DVD;
CREATE TABLE T_DVD (
id INT NOT NULL AUTO_INCREMENT
, name VARCHAR ( 50 )
, manufacturer VARCHAR ( 50 )
, regioncode VARCHAR ( 30 )
, PRIMARY KEY (id)
);
CREATE TABLE T_DVD (
id INT NOT NULL AUTO_INCREMENT
, name VARCHAR ( 50 )
, manufacturer VARCHAR ( 50 )
, regioncode VARCHAR ( 30 )
, PRIMARY KEY (id)
);
2.配置文件
配置文件和普通配置文件没有区别
TBook.hbm.xml:
<?
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 package ="cn.blogjava.start" >
< class name ="TBook" table ="T_Book" catalog ="sample" >
< id name ="id" column ="id" type ="java.lang.Integer" >
< generator class ="native" />
</ id >
< property name ="name" type ="java.lang.String" column ="name" />
< property name ="manufacturer" type ="java.lang.String" column ="manufacturer" />
< property name ="pageCount" type ="java.lang.Integer" column ="pagecount" />
</ class >
</ hibernate-mapping >
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping package ="cn.blogjava.start" >
< class name ="TBook" table ="T_Book" catalog ="sample" >
< id name ="id" column ="id" type ="java.lang.Integer" >
< generator class ="native" />
</ id >
< property name ="name" type ="java.lang.String" column ="name" />
< property name ="manufacturer" type ="java.lang.String" column ="manufacturer" />
< property name ="pageCount" type ="java.lang.Integer" column ="pagecount" />
</ class >
</ hibernate-mapping >
TDVD.hbm.xml:
<?
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 package ="cn.blogjava.start" >
< class name ="TDVD" table ="T_DVD" catalog ="sample" >
< id name ="id" column ="id" type ="java.lang.Integer" >
< generator class ="native" />
</ id >
< property name ="name" type ="java.lang.String" column ="name" />
< property name ="manufacturer" type ="java.lang.String" column ="manufacturer" />
< property name ="regionCode" type ="java.lang.String" column ="regioncode" />
</ class >
</ hibernate-mapping >
<! DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
< hibernate-mapping package ="cn.blogjava.start" >
< class name ="TDVD" table ="T_DVD" catalog ="sample" >
< id name ="id" column ="id" type ="java.lang.Integer" >
< generator class ="native" />
</ id >
< property name ="name" type ="java.lang.String" column ="name" />
< property name ="manufacturer" type ="java.lang.String" column ="manufacturer" />
< property name ="regionCode" type ="java.lang.String" column ="regioncode" />
</ class >
</ hibernate-mapping >
3.测试代码
HibernateTest.java
package
cn.blogjava.start;
import java.util.Iterator;
import java.util.List;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateTest extends TestCase {
Session session = null ;
/**
* JUnit中的setUp方法在TestCase初始化的时候会自动调用
* 一般用于初始化公用资源
*/
protected void setUp() {
try {
/**
* 可以采用hibernate.properties或者hibernate.cfg.xml
* 配置文件的初始化代码
*
* 采用hibernate.properties
* Configuration config = new Configuration();
* config.addClass(TUser.class);
*/
// 采用hibernate.cfg.xml配置文件,与上面的方法对比,两个差异
// 1.Configuration的初始化方式
// 2.xml
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* JUnit中的tearDown方法在TestCase执行完毕的时候会自动调用
* 一般用于释放资源
*/
protected void tearDown() {
try {
session.close();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 对象持久化测试(Insert方法)
*/
public void testInsert() {
Transaction tran = null ;
try {
tran = session.beginTransaction();
TBook book1 = new TBook();
book1.setManufacturer( " 电子工业 " );
book1.setName( " Java beginner " );
book1.setPageCount( 501 );
TBook book2 = new TBook();
book2.setManufacturer( " 机械工业 " );
book2.setName( " Thinking in java " );
book2.setPageCount( 1201 );
TDVD dvd1 = new TDVD();
dvd1.setManufacturer( " columnibia " );
dvd1.setName( " Lord king " );
dvd1.setRegionCode( " 5 area " );
TDVD dvd2 = new TDVD();
dvd2.setManufacturer( " sony " );
dvd2.setName( " Forrest gump " );
dvd2.setRegionCode( " 3 area " );
session.save(book1);
session.save(book2);
session.save(dvd1);
session.save(dvd2);
session.flush();
tran.commit();
Assert.assertEquals(book1.getId().intValue() > 0 , true );
Assert.assertEquals(book2.getId().intValue() > 0 , true );
Assert.assertEquals(dvd1.getId().intValue() > 0 , true );
Assert.assertEquals(dvd2.getId().intValue() > 0 , true );
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
Assert.fail(e.getMessage());
if (tran != null ) {
try {
tran.rollback();
} catch (Exception e1) {
// TODO: handle exception
e1.printStackTrace();
}
}
}
}
/**
* 对象读取测试(Select方法)
*/
public void testSelect(){
//指定了具体父类,如果是from TItem,是会失败的。
List list = session.createQuery( " from cn.blogjava.start.TItem " ).list();
Iterator it = list.iterator();
while (it.hasNext()) {
TItem item = (TItem)it.next();
System.out.println(item.getName());
}
}
}
import java.util.Iterator;
import java.util.List;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateTest extends TestCase {
Session session = null ;
/**
* JUnit中的setUp方法在TestCase初始化的时候会自动调用
* 一般用于初始化公用资源
*/
protected void setUp() {
try {
/**
* 可以采用hibernate.properties或者hibernate.cfg.xml
* 配置文件的初始化代码
*
* 采用hibernate.properties
* Configuration config = new Configuration();
* config.addClass(TUser.class);
*/
// 采用hibernate.cfg.xml配置文件,与上面的方法对比,两个差异
// 1.Configuration的初始化方式
// 2.xml
Configuration config = new Configuration().configure();
SessionFactory sessionFactory = config.buildSessionFactory();
session = sessionFactory.openSession();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* JUnit中的tearDown方法在TestCase执行完毕的时候会自动调用
* 一般用于释放资源
*/
protected void tearDown() {
try {
session.close();
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
}
}
/**
* 对象持久化测试(Insert方法)
*/
public void testInsert() {
Transaction tran = null ;
try {
tran = session.beginTransaction();
TBook book1 = new TBook();
book1.setManufacturer( " 电子工业 " );
book1.setName( " Java beginner " );
book1.setPageCount( 501 );
TBook book2 = new TBook();
book2.setManufacturer( " 机械工业 " );
book2.setName( " Thinking in java " );
book2.setPageCount( 1201 );
TDVD dvd1 = new TDVD();
dvd1.setManufacturer( " columnibia " );
dvd1.setName( " Lord king " );
dvd1.setRegionCode( " 5 area " );
TDVD dvd2 = new TDVD();
dvd2.setManufacturer( " sony " );
dvd2.setName( " Forrest gump " );
dvd2.setRegionCode( " 3 area " );
session.save(book1);
session.save(book2);
session.save(dvd1);
session.save(dvd2);
session.flush();
tran.commit();
Assert.assertEquals(book1.getId().intValue() > 0 , true );
Assert.assertEquals(book2.getId().intValue() > 0 , true );
Assert.assertEquals(dvd1.getId().intValue() > 0 , true );
Assert.assertEquals(dvd2.getId().intValue() > 0 , true );
} catch (HibernateException e) {
// TODO: handle exception
e.printStackTrace();
Assert.fail(e.getMessage());
if (tran != null ) {
try {
tran.rollback();
} catch (Exception e1) {
// TODO: handle exception
e1.printStackTrace();
}
}
}
}
/**
* 对象读取测试(Select方法)
*/
public void testSelect(){
//指定了具体父类,如果是from TItem,是会失败的。
List list = session.createQuery( " from cn.blogjava.start.TItem " ).list();
Iterator it = list.iterator();
while (it.hasNext()) {
TItem item = (TItem)it.next();
System.out.println(item.getName());
}
}
}