hibernate工具类更具配置文件创建table,和创建currentsession..
2009-11-09 14:52
477 查看
==============================根据hibernate配置文件(hibernate.cfg.xml和*.hbm.xml文件)创建table
package org.john.november.util;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDB {
public static void main(String[] args) {
//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
======================hibernate ,获取currentsession工具类==============
package org.john.november.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static SessionFactory factory;
static {
try {
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
}catch(Exception e) {
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
public static void closeSession(Session session) {
if (session != null) {
if (session.isOpen()) {
session.close();
}
}
}
}
package org.john.november.util;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class ExportDB {
public static void main(String[] args) {
//读取hibernate.cfg.xml文件
Configuration cfg = new Configuration().configure();
SchemaExport export = new SchemaExport(cfg);
export.create(true, true);
}
}
======================hibernate ,获取currentsession工具类==============
package org.john.november.util;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static SessionFactory factory;
static {
try {
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
}catch(Exception e) {
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
public static void closeSession(Session session) {
if (session != null) {
if (session.isOpen()) {
session.close();
}
}
}
}
相关文章推荐
- Hibernate读取配置文件,创建sessionFactory的执行过程以及用工具类优化
- Hibernate配置文件与映射文件的创建
- Spring讲述如何创建hibernate配置文件对象
- Hibernate学习笔记一:项目创建与基本配置文件
- 以Java属性文件的格式创建Hibernate的配置文件和DTD特殊符号作用
- Hibernate自动创建表--配置文件
- java中利用spring动态的创建hibernate的配置文件
- hibernate中根据model的配置文件创建数据库
- Hibernate动态建表,通过hbm.xml配置文件创建数据表,进行数据库操作, 动态模型
- hibernate使用配置文件创建联合主键往MySQL数据库插入数据
- Hibernate相关配置文件、映射文件案例、工具类、总结相关
- 使用hibernate自动在MYSQL中创建表,极其简单,改下配置文件。
- 初识Hibernate及简单的手动创建配置文件
- 通过hibernate配置文件创建数据库表
- Myeclipse如何自动创建hibernate以及配置struts2以及数据库表创建映射文件 详解
- 以Java属性文件的格式创建Hibernate的配置文件和DTD特殊符号作用
- hibernate通过配置文件和方法自动创建表。
- hibernate 配置文件 和一个 id生成类BaseEntity.java 和一个hibernate工具类 HibernatUtils.java
- hibernate配置文件工具类得到对应的表名、主键名、字段名和字段描述
- Hibernate深入理解----02HelloWorld详解(Hibernate.cfg.xml配置文件详解,创建持久化对象详解)