您的位置:首页 > 运维架构

Hibernate逍遥游记-第2章-使用hibernate.properties

2016-03-19 20:44 495 查看
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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: