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 ...
随机推荐
- spring aop配置及用例说明(3)
欢迎转载交流:http://www.cnblogs.com/shizhongtao/p/3476336.html 1.这里说一下aop的@Around标签,它提供了在方法开始和结束,都能添加用户业务逻 ...
- Codevs 2296 仪仗队 2008年省队选拔赛山东
2296 仪仗队 2008年省队选拔赛山东 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题解 题目描述 Description 作为体育委员,C君负责这次运动 ...
- 比较X与Y的大小,绝对精准!!!!!!
代码! #include<stdio.h> int max(int a,int b) { int x; x=a+b; return x; } int main() { int i,n,t; ...
- (POJ 2318)TOYS 向量叉积
题目链接:http://poj.org/problem?id=2318 #include<stdio.h> #include<cstdlib> #include<cstr ...
- 《RedHatlinux系统修复(通过FTP进行修复)》
比如我们删除了grub文件的initrd然后我们来修复 Linux系统下装的虚拟机boot options 位置,我们选网络修复,提前是我已经做好了ftpbootlaoder的配置. Win系统下以V ...
- nginx禁止访问某个后缀名的文件
猛然发现通过http://ip:端口号/路径/config.ini竟然能下载项目代码的配置文件,检查nginx配置,原来是没有加限制,立即加上,并记录如下: location ~* \.(ini|do ...
- (转载)SQLServer存储过程返回值总结
1. 存储过程没有返回值的情况(即存储过程语句中没有return之类的语句) 用方法 int count = ExecuteNonQuery(..)执行存储过程其返回值只有两种情况 (1)假如通过查询 ...
- ISO-9126 软件质量模型
摘要 在软件开发过程中,软件的质量是一个重要的因素,而软件体系结构在整个过程中显得尤为重要.软件的质量需求是在开发初期的非功能性需求,对软件的体系结构影响很大.但是并不意味着一味的追求质量,必须在效率 ...
- How to install Hadoop
1.How to install Hadoop 3.0.0 http://blog.sina.com.cn/s/blog_4a1f59bf01010kx3.html 2.How to install ...
- jQuery 的 json 格式的处理问题
在实际的使用中时常会发生一些ajax验证的诡异事件,如我在一个文件中使用json传送数据,结果出现了当数据发送到服务器端时(后端主要呈现的是对json数据对象的数据库查询操作),结果显示的是数据已经插 ...