Hibernate 学习笔记(四)-- 实体层设计之Table per subclass
2010-05-06 13:49
513 查看
关于如何配置请参看:Hibernate
学习笔记(一)--用MyEclipse 6.5+MySQL 5.0的环境跑起来
准备:建表
用MySQL在名为STMS数据库中建表
Titem
Tbook
Tdvd
src/org.lxh.hibernate3.TItem.java
1
package
org.lxh.hibernate3;
2
/** */
/**
3
*
@author
∪∩BUG E-mail: tidelgl@163.com
4
*
@version
Aug 31, 2008 3:27:51 PM
5
* @父类
6
*/
7
public
class
TItem
{
8
9
private
String id;
10
private
String name;
11
private
String manufacturer;
12
13
public
String getId()
{
14
return
id;
15
}
16
17
public
void
setId(String id)
{
18
this
.id
=
id;
19
}
20
21
public
String getName()
{
22
return
name;
23
}
24
25
public
void
setName(String name)
{
26
this
.name
=
name;
27
}
28
29
public
String getManufacturer()
{
30
return
manufacturer;
31
}
32
33
public
void
setManufacturer(String manufacturer)
{
34
this
.manufacturer
=
manufacturer;
35
}
36
37
}
38
src/org.lxh.hibernate3.TBook.java
1
package
org.lxh.hibernate3;
2
3
/** */
/**
4
*
@author
∪∩BUG E-mail: tidelgl@163.com
5
*
@version
Aug 31, 2008 3:31:47 PM @ 继承父类TItem
6
*/
7
public
class
TBook
extends
TItem
{
8
9
private
int
pageCount;
10
11
public
int
getPageCount()
{
12
return
pageCount;
13
}
14
15
public
void
setPageCount(
int
pageCount)
{
16
this
.pageCount
=
pageCount;
17
}
18
}
19
src/org.lxh.hibernate3.TDvd.java
1
package
org.lxh.hibernate3;
2
/** */
/**
3
*
@author
∪∩BUG E-mail: tidelgl@163.com
4
*
@version
Aug 31, 2008 3:34:06 PM
5
* @ 继承父类TItem
6
*/
7
public
class
TDvd
extends
TItem
{
8
9
private
String regionCode;
10
11
public
String getRegionCode()
{
12
return
regionCode;
13
}
14
15
public
void
setRegionCode(String regionCode)
{
16
this
.regionCode
=
regionCode;
17
}
18
19
}
20
src/org.lxh.hibernate3.Titem.hbm.xml
1
xml version="1.0" encoding="utf-8"
?>
2
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
4
7
<
hibernate-mapping
>
8
9
<
class
name
="org.lxh.hibernate3.TItem"
table
="titem"
10
catalog
="stms"
>
11
<
id
name
="id"
type
="java.lang.String"
>
12
<
column
name
="id"
length
="32"
/>
13
<
generator
class
="assigned"
/>
14
id
>
15
<
property
name
="name"
type
="java.lang.String"
>
16
<
column
name
="name"
length
="20"
not-null
="true"
/>
17
property
>
18
<
property
name
="manufacturer"
type
="java.lang.String"
>
19
<
column
name
="manufacturer"
length
="20"
/>
20
property
>
21
22
28
<
joined-subclass
name
="org.lxh.hibernate3.TBook"
29
table
="TBook"
>
30
<
key
column
="id"
>key
>
31
<
property
name
="pageCount"
type
="int"
32
column
="pagecount"
>
33
property
>
34
joined-subclass
>
35
<
joined-subclass
name
="org.lxh.hibernate3.TDvd"
table
="TDvd"
>
36
<
key
column
="id"
>key
>
37
<
property
name
="regionCode"
type
="string"
38
column
="regioncode"
>
39
property
>
40
joined-subclass
>
41
class
>
42
hibernate-mapping
>
43
src/hibernate.cfg.xml
1
xml version='1.0' encoding='UTF-8'
?>
2
DOCTYPE hibernate-configuration PUBLIC
3
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>
5
6
7
<
hibernate-configuration
>
8
9
<
session-factory
>
10
<
property
name
="connection.username"
>
root
property
>
11
<
property
name
="connection.url"
>
12
jdbc:mysql://localhost:3306/STMS
13
property
>
14
<
property
name
="dialect"
>
15
org.hibernate.dialect.MySQLDialect
16
property
>
17
<
property
name
="myeclipse.connection.profile"
>
18
MySql_localhost
19
property
>
20
<
property
name
="connection.password"
>
root
property
>
21
<
property
name
="connection.driver_class"
>
22
com.mysql.jdbc.Driver
23
property
>
24
<
property
name
="show_sql"
>
true
property
>
25
26
27
<
mapping
resource
="org/lxh/hibernate3/Titem.hbm.xml"
/>
28
29
session-factory
>
30
31
hibernate-configuration
>
src/org.lxh.hibernate3.TItemOperate.java
1
package
org.lxh.hibernate3;
2
3
4
5
import
java.util.Iterator;
6
7
import
org.hibernate.Query;
8
import
org.hibernate.Session;
9
import
org.hibernate.SessionFactory;
10
import
org.hibernate.cfg.Configuration;
11
12
/** */
/**
13
*
@author
∪∩BUG E-mail: tidelgl@163.com
14
*
@version
Aug 31, 2008 4:08:14 PM
15
* @具体操作Hibernate类
16
*/
17
public
class
TItemOperate
{
18
private
Session session;
19
20
public
TItemOperate()
{
21
//
找
到Hibernate配置文件
22
Configuration config
=
new
Configuration().configure();
23
24
//
从
全局文件中取出SessionFactory
25
SessionFactory factory
=
config.buildSessionFactory();
26
27
//
从
SessionFactory取出一个session
28
this
.session
=
factory.openSession();
29
}
30
31
//
插入操
作
32
//
TBook
和TDvd类都是TItem的子类,所以只需往TItem里插入就可以了.
33
public
void
insert(TItem item)
{
34
//
执
行语句
35
this
.session.save(item);
36
37
//
开
始事务.提交事务
38
this
.session.beginTransaction().commit();
39
this
.session.close();
40
}
41
42
//
查询操
作
43
//
TBook
和TDvd类都是TItem的子类,所以只需查询TItem的ID就可以了.
44
public
TItem QueryById(String id)
{
45
TItem item
=
new
TItem();
46
String hql
=
"
FROM TItem as t WHERE t.id=?
"
;
47
Query q
=
this
.session.createQuery(hql);
48
q.setString(
0
, id);
49
Iterator iter
=
q.list().iterator();
50
if
(iter.hasNext())
{
51
item
=
(TItem)iter.next();
52
}
53
return
item;
54
}
55
}
56
src/org.lxh.hibernate3.Test.java
1
package
org.lxh.hibernate3;
2
3
/** */
/**
4
*
@author
∪∩BUG E-mail: tidelgl@163.com
5
*
@version
Aug 31, 2008 4:26:26 PM
6
* @测试类
7
*/
8
public
class
Test
{
9
10
/** */
/**
11
*
@param
args
12
*/
13
/** */
/**
14
*
@param
args
15
*/
16
public
static
void
main(String[] args)
{
17
18
TItemOperate to
=
new
TItemOperate();
19
20
/**/
/*
21
// 向数据库中插入数据
22
TBook book = new TBook();
23
book.setId("02");
24
book.setName("Hibernate");
25
book.setPageCount(31);
26
book.setManufacturer("Hibernate.org");
27
28
to.insert(book);
29
30
TDvd dvd = new TDvd();
31
dvd.setId("03");
32
dvd.setManufacturer("Apache.org");
33
dvd.setName("Struts2");
34
dvd.setRegionCode("87");
35
36
to.insert(dvd);
37
*/
38
39
//
查
询数据库
40
TBook book
=
(TBook)to.QueryById(
"
01
"
);
41
System.out.println(book.getName());
42
}
43
44
}
45
例子结构:
学习笔记(一)--用MyEclipse 6.5+MySQL 5.0的环境跑起来
准备:建表
用MySQL在名为STMS数据库中建表
Titem
Tbook
Tdvd
src/org.lxh.hibernate3.TItem.java
1
package
org.lxh.hibernate3;
2
/** */
/**
3
*
@author
∪∩BUG E-mail: tidelgl@163.com
4
*
@version
Aug 31, 2008 3:27:51 PM
5
* @父类
6
*/
7
public
class
TItem
{
8
9
private
String id;
10
private
String name;
11
private
String manufacturer;
12
13
public
String getId()
{
14
return
id;
15
}
16
17
public
void
setId(String id)
{
18
this
.id
=
id;
19
}
20
21
public
String getName()
{
22
return
name;
23
}
24
25
public
void
setName(String name)
{
26
this
.name
=
name;
27
}
28
29
public
String getManufacturer()
{
30
return
manufacturer;
31
}
32
33
public
void
setManufacturer(String manufacturer)
{
34
this
.manufacturer
=
manufacturer;
35
}
36
37
}
38
src/org.lxh.hibernate3.TBook.java
1
package
org.lxh.hibernate3;
2
3
/** */
/**
4
*
@author
∪∩BUG E-mail: tidelgl@163.com
5
*
@version
Aug 31, 2008 3:31:47 PM @ 继承父类TItem
6
*/
7
public
class
TBook
extends
TItem
{
8
9
private
int
pageCount;
10
11
public
int
getPageCount()
{
12
return
pageCount;
13
}
14
15
public
void
setPageCount(
int
pageCount)
{
16
this
.pageCount
=
pageCount;
17
}
18
}
19
src/org.lxh.hibernate3.TDvd.java
1
package
org.lxh.hibernate3;
2
/** */
/**
3
*
@author
∪∩BUG E-mail: tidelgl@163.com
4
*
@version
Aug 31, 2008 3:34:06 PM
5
* @ 继承父类TItem
6
*/
7
public
class
TDvd
extends
TItem
{
8
9
private
String regionCode;
10
11
public
String getRegionCode()
{
12
return
regionCode;
13
}
14
15
public
void
setRegionCode(String regionCode)
{
16
this
.regionCode
=
regionCode;
17
}
18
19
}
20
src/org.lxh.hibernate3.Titem.hbm.xml
1
xml version="1.0" encoding="utf-8"
?>
2
DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
3
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"
>
4
7
<
hibernate-mapping
>
8
9
<
class
name
="org.lxh.hibernate3.TItem"
table
="titem"
10
catalog
="stms"
>
11
<
id
name
="id"
type
="java.lang.String"
>
12
<
column
name
="id"
length
="32"
/>
13
<
generator
class
="assigned"
/>
14
id
>
15
<
property
name
="name"
type
="java.lang.String"
>
16
<
column
name
="name"
length
="20"
not-null
="true"
/>
17
property
>
18
<
property
name
="manufacturer"
type
="java.lang.String"
>
19
<
column
name
="manufacturer"
length
="20"
/>
20
property
>
21
22
28
<
joined-subclass
name
="org.lxh.hibernate3.TBook"
29
table
="TBook"
>
30
<
key
column
="id"
>key
>
31
<
property
name
="pageCount"
type
="int"
32
column
="pagecount"
>
33
property
>
34
joined-subclass
>
35
<
joined-subclass
name
="org.lxh.hibernate3.TDvd"
table
="TDvd"
>
36
<
key
column
="id"
>key
>
37
<
property
name
="regionCode"
type
="string"
38
column
="regioncode"
>
39
property
>
40
joined-subclass
>
41
class
>
42
hibernate-mapping
>
43
src/hibernate.cfg.xml
1
xml version='1.0' encoding='UTF-8'
?>
2
DOCTYPE hibernate-configuration PUBLIC
3
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
>
5
6
7
<
hibernate-configuration
>
8
9
<
session-factory
>
10
<
property
name
="connection.username"
>
root
property
>
11
<
property
name
="connection.url"
>
12
jdbc:mysql://localhost:3306/STMS
13
property
>
14
<
property
name
="dialect"
>
15
org.hibernate.dialect.MySQLDialect
16
property
>
17
<
property
name
="myeclipse.connection.profile"
>
18
MySql_localhost
19
property
>
20
<
property
name
="connection.password"
>
root
property
>
21
<
property
name
="connection.driver_class"
>
22
com.mysql.jdbc.Driver
23
property
>
24
<
property
name
="show_sql"
>
true
property
>
25
26
27
<
mapping
resource
="org/lxh/hibernate3/Titem.hbm.xml"
/>
28
29
session-factory
>
30
31
hibernate-configuration
>
src/org.lxh.hibernate3.TItemOperate.java
1
package
org.lxh.hibernate3;
2
3
4
5
import
java.util.Iterator;
6
7
import
org.hibernate.Query;
8
import
org.hibernate.Session;
9
import
org.hibernate.SessionFactory;
10
import
org.hibernate.cfg.Configuration;
11
12
/** */
/**
13
*
@author
∪∩BUG E-mail: tidelgl@163.com
14
*
@version
Aug 31, 2008 4:08:14 PM
15
* @具体操作Hibernate类
16
*/
17
public
class
TItemOperate
{
18
private
Session session;
19
20
public
TItemOperate()
{
21
//
找
到Hibernate配置文件
22
Configuration config
=
new
Configuration().configure();
23
24
//
从
全局文件中取出SessionFactory
25
SessionFactory factory
=
config.buildSessionFactory();
26
27
//
从
SessionFactory取出一个session
28
this
.session
=
factory.openSession();
29
}
30
31
//
插入操
作
32
//
TBook
和TDvd类都是TItem的子类,所以只需往TItem里插入就可以了.
33
public
void
insert(TItem item)
{
34
//
执
行语句
35
this
.session.save(item);
36
37
//
开
始事务.提交事务
38
this
.session.beginTransaction().commit();
39
this
.session.close();
40
}
41
42
//
查询操
作
43
//
TBook
和TDvd类都是TItem的子类,所以只需查询TItem的ID就可以了.
44
public
TItem QueryById(String id)
{
45
TItem item
=
new
TItem();
46
String hql
=
"
FROM TItem as t WHERE t.id=?
"
;
47
Query q
=
this
.session.createQuery(hql);
48
q.setString(
0
, id);
49
Iterator iter
=
q.list().iterator();
50
if
(iter.hasNext())
{
51
item
=
(TItem)iter.next();
52
}
53
return
item;
54
}
55
}
56
src/org.lxh.hibernate3.Test.java
1
package
org.lxh.hibernate3;
2
3
/** */
/**
4
*
@author
∪∩BUG E-mail: tidelgl@163.com
5
*
@version
Aug 31, 2008 4:26:26 PM
6
* @测试类
7
*/
8
public
class
Test
{
9
10
/** */
/**
11
*
@param
args
12
*/
13
/** */
/**
14
*
@param
args
15
*/
16
public
static
void
main(String[] args)
{
17
18
TItemOperate to
=
new
TItemOperate();
19
20
/**/
/*
21
// 向数据库中插入数据
22
TBook book = new TBook();
23
book.setId("02");
24
book.setName("Hibernate");
25
book.setPageCount(31);
26
book.setManufacturer("Hibernate.org");
27
28
to.insert(book);
29
30
TDvd dvd = new TDvd();
31
dvd.setId("03");
32
dvd.setManufacturer("Apache.org");
33
dvd.setName("Struts2");
34
dvd.setRegionCode("87");
35
36
to.insert(dvd);
37
*/
38
39
//
查
询数据库
40
TBook book
=
(TBook)to.QueryById(
"
01
"
);
41
System.out.println(book.getName());
42
}
43
44
}
45
例子结构:
相关文章推荐
- Hibernate 学习笔记(五)-- 实体层设计之Table per class hierarchy
- Hibernate 实体层设计--Table per subclass
- Hibernate 实体层设计--Table per subclass
- JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-003Table per concrete class with unions(@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)、<union-subclass>)
- 在JPA中,实体继承关系的映射策略共有三种:单表继承策略(table per class)、Joined策略(table per subclass)和Table_PER_Class策略。
- [置顶] html学习笔记,锚点,超链接,table布局,表头,h,sub,blockquote,ul,li,ol.dl,加入收藏,打印,弹出窗口
- hibernate继承关系的映射(single_table,class_per_table,joined)
- hibernate 继承映射关系( TABLE_PER_CLASS)
- 领域驱动设计-学习笔记 实体
- Hibernate 学习笔记(二)-- 实体映射
- html学习笔记,锚点,超链接,table布局,表头,h,sub,blockquote,ul,li,ol.dl,加入收藏,打印,弹出窗口
- hibernate继承关系的映射(single_table,class_per_table,joined)
- 实体粒度设计策略学习笔记
- (8) ebj学习: Jpa的SINGLE_TABLE,JOINED,TABLE_PER_CLASS三种继承策略
- 设计模式学习笔记--组合实体模式
- Hibernate 3学习笔记 - 简单的Persistent class和mapping file
- hibernate学习笔记 之 实体几种状态的区别
- 关于table per subclass继承映射的注意点
- Hibernate 3.2 学习笔记 映射实体关联关系
- Hibernate学习笔记之ORM实体间关系“OneToOne”详解