关于如何配置请参看:Hibernate学习笔记(一)--用MyEclipse 6.5+MySQL 5.0的环境跑起来
准备:建表
用MySQL在名为STMS数据库中建表Titems
src/org.lxh.hibernate4.TItems.java
1
package
org.lxh.hibernate4;
2 /** */ /**
3 * @author ∪∩BUG E-mail: [email protected]
4 * @version Aug 31, 2008 3:27:51 PM
5 * @父类
6 */
7 public class TItems {
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
2 /** */ /**
3 * @author ∪∩BUG E-mail: [email protected]
4 * @version Aug 31, 2008 3:27:51 PM
5 * @父类
6 */
7 public class TItems {
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.hibernate4.TDvds.java
1
package
org.lxh.hibernate4;
2 /** */ /**
3 * @author ∪∩BUG E-mail: [email protected]
4 * @version Aug 31, 2008 3:34:06 PM
5 * @ 继承父类TItems
6 */
7 public class TDvds extends TItems {
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
2 /** */ /**
3 * @author ∪∩BUG E-mail: [email protected]
4 * @version Aug 31, 2008 3:34:06 PM
5 * @ 继承父类TItems
6 */
7 public class TDvds extends TItems {
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.hibernate4.TBooks.java
1
package
org.lxh.hibernate4;
2
3 /** */ /**
4 * @author ∪∩BUG E-mail: [email protected]
5 * @version Aug 31, 2008 3:31:47 PM
6 * @ 继承父类TItems
7 */
8 public class TBooks extends TItems {
9
10 private int pageCount;
11
12 public int getPageCount() {
13 return pageCount;
14 }
15
16 public void setPageCount(int pageCount) {
17 this.pageCount = pageCount;
18 }
19}
20
2
3 /** */ /**
4 * @author ∪∩BUG E-mail: [email protected]
5 * @version Aug 31, 2008 3:31:47 PM
6 * @ 继承父类TItems
7 */
8 public class TBooks extends TItems {
9
10 private int pageCount;
11
12 public int getPageCount() {
13 return pageCount;
14 }
15
16 public void setPageCount(int pageCount) {
17 this.pageCount = pageCount;
18 }
19}
20
src/org.lxh.hibernate4.Titems.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 <class name="org.lxh.hibernate4.TItems" table="titems"
9 catalog="stms">
10 <id name="id" type="java.lang.String">
11 <column name="id" length="32" />
12 <generator class="assigned" />
13 id>
14 <property name="name" type="java.lang.String">
15 <column name="name" length="20" not-null="true" />
16 property>
17 <property name="manufacturer" type="java.lang.String">
18 <column name="manufacturer" length="20" not-null="true" />
19 property>
20
21
27 <discriminator type="string" column="category"> discriminator>
28 <subclass name="org.lxh.hibernate4.TBooks"
29 discriminator-value="1">
30 <property name="pageCount" type="java.lang.Integer">
31 <column name="pageCount" />
32 property>
33 subclass>
34 <subclass name="org.lxh.hibernate4.TDvds"
35 discriminator-value="2">
36 <property name="regionCode" type="java.lang.String">
37 <column name="regionCode" length="2" />
38 property>
39 subclass>
40 class>
41 hibernate-mapping>
42
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 <class name="org.lxh.hibernate4.TItems" table="titems"
9 catalog="stms">
10 <id name="id" type="java.lang.String">
11 <column name="id" length="32" />
12 <generator class="assigned" />
13 id>
14 <property name="name" type="java.lang.String">
15 <column name="name" length="20" not-null="true" />
16 property>
17 <property name="manufacturer" type="java.lang.String">
18 <column name="manufacturer" length="20" not-null="true" />
19 property>
20
21
27 <discriminator type="string" column="category"> discriminator>
28 <subclass name="org.lxh.hibernate4.TBooks"
29 discriminator-value="1">
30 <property name="pageCount" type="java.lang.Integer">
31 <column name="pageCount" />
32 property>
33 subclass>
34 <subclass name="org.lxh.hibernate4.TDvds"
35 discriminator-value="2">
36 <property name="regionCode" type="java.lang.String">
37 <column name="regionCode" length="2" />
38 property>
39 subclass>
40 class>
41 hibernate-mapping>
42
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/hibernate4/Titems.hbm.xml" />
28
29 session-factory>
30
31 hibernate-configuration>
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/hibernate4/Titems.hbm.xml" />
28
29 session-factory>
30
31 hibernate-configuration>
src/org.lxh.hibernate4.TItemsOperate.java
1
package
org.lxh.hibernate4;
2
3 import java.util.Iterator;
4
5 import org.hibernate.Query;
6 import org.hibernate.Session;
7 import org.hibernate.SessionFactory;
8 import org.hibernate.cfg.Configuration;
9
10 /** */ /**
11 * @author ∪∩BUG E-mail: [email protected]
12 * @version Aug 31, 2008 4:08:14 PM
13 * @具体操作Hibernate类
14 */
15 public class TItemsOperate {
16 private Session session;
17
18 public TItemsOperate() {
19 //找到Hibernate配置文件
20 Configuration config = new Configuration().configure();
21
22 //从全局文件中取出SessionFactory
23 SessionFactory factory = config.buildSessionFactory();
24
25 //从SessionFactory取出一个session
26 this.session = factory.openSession();
27 }
28
29 //插入操作
30 //TBook和TDvd类都是TItem的子类,所以只需往TItem里插入就可以了.
31 public void insert(TItems item) {
32 //执行语句
33 this.session.save(item);
34
35 //开始事务.提交事务
36 this.session.beginTransaction().commit();
37 this.session.close();
38 }
39
40 //查询操作
41 //TBooks和TDvds类都是TItems的子类,所以只需查询TItem的ID就可以了.
42 public TItems QueryById(String id) {
43 TItems item = new TItems();
44 String hql = "FROM TItems as t WHERE t.id=?";
45 Query q = this.session.createQuery(hql);
46 q.setString(0, id);
47 Iterator iter = q.list().iterator();
48 if(iter.hasNext()){
49 item = (TItems)iter.next();
50 }
51 return item;
52 }
53}
54
2
3 import java.util.Iterator;
4
5 import org.hibernate.Query;
6 import org.hibernate.Session;
7 import org.hibernate.SessionFactory;
8 import org.hibernate.cfg.Configuration;
9
10 /** */ /**
11 * @author ∪∩BUG E-mail: [email protected]
12 * @version Aug 31, 2008 4:08:14 PM
13 * @具体操作Hibernate类
14 */
15 public class TItemsOperate {
16 private Session session;
17
18 public TItemsOperate() {
19 //找到Hibernate配置文件
20 Configuration config = new Configuration().configure();
21
22 //从全局文件中取出SessionFactory
23 SessionFactory factory = config.buildSessionFactory();
24
25 //从SessionFactory取出一个session
26 this.session = factory.openSession();
27 }
28
29 //插入操作
30 //TBook和TDvd类都是TItem的子类,所以只需往TItem里插入就可以了.
31 public void insert(TItems item) {
32 //执行语句
33 this.session.save(item);
34
35 //开始事务.提交事务
36 this.session.beginTransaction().commit();
37 this.session.close();
38 }
39
40 //查询操作
41 //TBooks和TDvds类都是TItems的子类,所以只需查询TItem的ID就可以了.
42 public TItems QueryById(String id) {
43 TItems item = new TItems();
44 String hql = "FROM TItems as t WHERE t.id=?";
45 Query q = this.session.createQuery(hql);
46 q.setString(0, id);
47 Iterator iter = q.list().iterator();
48 if(iter.hasNext()){
49 item = (TItems)iter.next();
50 }
51 return item;
52 }
53}
54
src/org.lxh.hibernate4.Test.java
1
package
org.lxh.hibernate4;
2
3 import java.awt.print.Book;
4
5 /** */ /**
6 * @author ∪∩BUG E-mail: [email protected]
7 * @version Aug 31, 2008 10:52:25 PM
8 * 类说明
9 */
10 public class Test {
11
12 public static void main(String[] args) {
13
14 TItemsOperate to = new TItemsOperate();
15
16 //插入数据
17 TBooks books = new TBooks();
18 books.setId("03");
19 books.setManufacturer("Apache.org");
20 books.setName("Struts");
21 books.setPageCount(10);
22
23 to.insert(books);
24
25// TDvds dvds = new TDvds();
26// dvds.setId("02");
27// dvds.setManufacturer("MySQL.com");
28// dvds.setName("MySQL");
29// dvds.setRegionCode("31");
30//
31// to.insert(dvds);
32
33 //查询操作
34// TBooks books = (TBooks)to.QueryById("01");
35// System.out.println(books.getName());
36 }
37
38}
39
2
3 import java.awt.print.Book;
4
5 /** */ /**
6 * @author ∪∩BUG E-mail: [email protected]
7 * @version Aug 31, 2008 10:52:25 PM
8 * 类说明
9 */
10 public class Test {
11
12 public static void main(String[] args) {
13
14 TItemsOperate to = new TItemsOperate();
15
16 //插入数据
17 TBooks books = new TBooks();
18 books.setId("03");
19 books.setManufacturer("Apache.org");
20 books.setName("Struts");
21 books.setPageCount(10);
22
23 to.insert(books);
24
25// TDvds dvds = new TDvds();
26// dvds.setId("02");
27// dvds.setManufacturer("MySQL.com");
28// dvds.setName("MySQL");
29// dvds.setRegionCode("31");
30//
31// to.insert(dvds);
32
33 //查询操作
34// TBooks books = (TBooks)to.QueryById("01");
35// System.out.println(books.getName());
36 }
37
38}
39
例子结构: