Hibernate逍遥游记-第2章-使用hibernate.properties
1.
package mypack; import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import java.util.*; public class BusinessService{
public static SessionFactory sessionFactory; /** 初始化Hibernate,创建SessionFactory实例 */
static{
try{
// 根据默认位置的Hibernate配置文件的配置信息,创建一个Configuration实例
Configuration config = new Configuration();
//加载Monkey类的对象-关系映射文件
config.addClass(Monkey.class);
// 创建SessionFactory实例
sessionFactory = config.buildSessionFactory();
SessionFactory sessionFactory2 = config.buildSessionFactory();
System.out.println("---------->"+(sessionFactory2==sessionFactory)); }catch(RuntimeException e){e.printStackTrace();throw e;}
} /** 查询所有的Monkey对象,然后打印Monkey对象信息 */
public void findAllMonkeys(){
Session session = sessionFactory.openSession(); //创建一个会话
Transaction tx = null;
try {
tx = session.beginTransaction(); //开始一个事务
Query query=session.createQuery("from Monkey as m order by m.name asc");
List monkeys=query.list();
for (Iterator it = monkeys.iterator(); it.hasNext();) {
Monkey monkey=(Monkey) it.next();
System.out.println("ID="+monkey.getId()
+",姓名="+monkey.getName()
+",年龄="+monkey.getAge()
+",性别="+(monkey.getGender()=='M'?"公猴":"母猴"));
} tx.commit(); //提交事务 }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} /** 持久化一个Monkey对象 */
public void saveMonkey(Monkey monkey){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.save(monkey);
tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} /** 按照OID加载一个Monkey对象,然后修改它的属性 */
public void loadAndUpdateMonkey(Long monkey_id,int age){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction(); Monkey m=(Monkey)session.get(Monkey.class,monkey_id);
m.setAge(age);
tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} /**删除Monkey对象 */
public void deleteMonkey(Monkey monkey){
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
session.delete(monkey);
tx.commit(); }catch (RuntimeException e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
} public void test(){
Monkey monkey=new Monkey();
monkey.setName("智多星");
monkey.setAge(1);
monkey.setGender('M'); saveMonkey(monkey); findAllMonkeys();
loadAndUpdateMonkey(monkey.getId(),2);
findAllMonkeys();
deleteMonkey(monkey);
findAllMonkeys();
} public static void main(String args[]){
new BusinessService().test();
sessionFactory.close();
}
}
2.
<?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>
<class name="mypack.Monkey" table="MONKEYS"> <id name="id" column="ID" type="long">
<generator class="increment"/>
</id>
<property name="name" column="NAME" type="string" not-null="true" />
<property name="age" column="AGE" type="int" />
<property name="gender" column="GENDER" type="character"/>
</class> </hibernate-mapping>
3.
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/SAMPLEDB?useUnicode=true&characterEncoding=GBK
hibernate.connection.username=root
hibernate.connection.password=1234
hibernate.show_sql=true
4.
Hibernate逍遥游记-第2章-使用hibernate.properties的更多相关文章
- Hibernate逍遥游记-第6章 通过Hibernate操纵对象(select-before-update)
1. 2. 3. 4. 5. 6. 7.
- Hibernate逍遥游记-第15章处理并发问题-003乐观锁
1. 2. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; drop table if exists ...
- Hibernate逍遥游记-第15章处理并发问题-002悲观锁
1. 2. hibernate.dialect=org.hibernate.dialect.MySQLDialect hibernate.connection.driver_class=com.mys ...
- Hibernate逍遥游记-第13章 映射实体关联关系-006双向多对多(分解为一对多)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...
- Hibernate逍遥游记-第13章 映射实体关联关系-005双向多对多(使用组件类集合\<composite-element>\)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-004双向多对多(inverse="true")
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多
0. 1. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; create table MONKEYS ...
- Hibernate逍遥游记-第13章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
- Hibernate逍遥游记-第13章 映射实体关联关系-001用外键映射一对一(<many-to-one unique="true">、<one-to-one>)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...
随机推荐
- Linux下安装和配置java开发环境(jdk/eclipse/tomcat)
第一步 安装jdk 1.进入usr目录 cd /usr 2.在usr目录下建立java安装目录 mkdir java 3.将jdk-6u24-linux-i586.bin拷贝到java目录下 cp j ...
- css3学习笔记之图片
圆角图片 border-radius: 圆角图片: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <!DOCTYPE html> <htm ...
- feature.shape和feature.shapecopy的区别
以前在写AE代码的时候也没有注意到feature.shape和feature.shapecopy的区别,觉得两者也差不多: 今天写入库程序才明白过来. 如果取feature.shape,则得到的是该要 ...
- 关于delete
上面三图在debug下,delete的时候会以fe ee覆盖指针所指向要回收内存前后较大一块区域的值. 下图对于release下delete的时候会先用fe ee覆盖指针所指位置起6*4=24字节, ...
- Mysql创建函数时报错
先去查询 show variables like '%func%' ; 这个语句,如果该语句最后输出的值是OFF 那么就用下面的语句去修改就可以:set global log_bin_trust_f ...
- <c:forEach>用法
<c:forEach items="${xqsCheckDataList}" var="ch"> <c:if test="${ch. ...
- ASP.NET工具
每个开发人员现在应该下载的十种必备工具 发布日期: 7/20/2004 | 更新日期: 7/20/2004 本文自发布以来已经增加了新信息. 请参阅下面的编辑更新. 本文讨论: • 用于编写单元测试的 ...
- Spark小课堂Week3 FirstSparkApp(RDD开发)
Spark小课堂Week3 FirstSparkApp 问题:Java有哪些数据结构 大致有如下几种,其中List与Map是最重要的: List Map Set Array Heap Stack Qu ...
- Opencv Mat的操作
cout << mat 有错误的原因 You are using OpenCV built with VS10. The ostream operator << in the ...
- mongodb命令使用
最近学习mongodb的使用,整理了一些常用命令 windows服务安装:mongod --install --serviceName MongoDB --serviceDisplayName Mon ...