要创建SessionFactory , 首先要创建Configuration 对象。
这个对象就是去读取hibernate 的一些配置信息。
默认状况下, hibernate会到 classPath 目录下加载hibernate.cfg.xml 文件。
这里延续上一篇的例子:
[Hibernate系列—] 1. 下载与试用Hibernate(MySQL与Oracle 配置)
在Eclipse 中进行开发。
这个配置文件的方式可以有多种, 可以是xml , 可以是properties , 也可以直接在代码中写配置。
方式1. 在src 目录下放入 hibernate.cfg.xml, 类似上篇的例子
方式2. 在 src 目录下放入 hibernate.properties
内容如下:
[html] view plaincopy在CODE上查看代码片派生到我的代码片
01.hibernate.dialect=org.hibernate.dialect.MySQLDialect
02.hibernate.connection.driver_class=com.mysql.jdbc.Driver
03.hibernate.connection.url=jdbc:mysql://localhost:3306/test
04.hibernate.connection.username=root
05.hibernate.connection.password=123456
06.#hibernate.hbm2ddl.auto=create
可以看出, 这种方式无法添加 User.hbm.xml 的配置, 所以可以在代码中添加:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
01.Configuration configuration = new Configuration().addResource("com/oscar999/Usr.hbm.xml");
方式3. 可以直接在代码中进行设置, 类似
[java] view plaincopy在CODE上查看代码片派生到我的代码片
01.Configuration configuration = new Configuration().addResource("com/oscar999/Usr.hbm.xml")
02. .setProperty("hibernate.connection.driver_class","com.mysql.jdbc.Driver")
03. .setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/test")
04. .setProperty("hibernate.connection.username", "root")
05. .setProperty("hibernate.connection.password", "123456")
06. .setProperty("hibernate.dialect","org.hibernate.dialect.MySQLDialect")
07. .setProperty("hibernate.hbm2ddl.auto", "update");
也可以通过
[java] view plaincopy在CODE上查看代码片派生到我的代码片
01.Configuration configuration = new Configuration().addClass(com.oscar999.Usr.class)
添加映射文件。
一般状况下, 添加 hibernate.cfg.xml 会比较常用, .properties 和 .xml 也可以并存。
除此之外, 如果不想使用默认的文件名, 也可以这样:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
01.File file = new File("src/com/oscar999/myhibernate.xml");
02.Configuration config = new Configuration();
03.config.configure(file);
SessionFactory 对象的创建
Configuration 创建完成之后, 接下来就是创建 SessionFactory 了。
在Hibernate 3中,创建SessionFactory 的方式是:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
01.SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
但是在, Hibernate 4 中, 这种方法已经过时了。
目前推荐的使用方式是:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
01.ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
02. .applySettings(configuration.getProperties()).build();
03.SessionFactory sessionFactory = configuration
04. .buildSessionFactory(serviceRegistry);
至于为什么要使用这种方式, 可以参考:
http://planet.jboss.org/post/hibernate_orm_service_registry
session 的使用
sessionFactory 有了, 接下来就简单了,直接贴一个例子
[java] view plaincopy在CODE上查看代码片派生到我的代码片
01.Configuration configuration = new Configuration().addClass(com.oscar999.Usr.class);
02.ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
03. .applySettings(confi www.hnnedu.com guration.getProperties()).build();
04.SessionFactory sessionFactory = configuration
05. .buildSessionFactory(serviceRegistry);
06.Session session = sessionFactory.openSession();
07.session.beginTransaction();
08.session.save(new Usr("uesr3"));
09.session.getTransaction().commit();
10.session.close();
11.sessionFactory.close();
hibernate实体类注解怎么创建sessionfactory
spring 2.5.6新特性之packagestoscan
如果你使用早前版本的spring,又恰好采用了annotation注解方式(而非传统xml方式)配置hibernate对象关系映射,那么在通过org.springframework.orm.hibernate3.annotation.annotationsessionfactorybean配置sessionfactory时,你一定对annotatedclasses、annotatedpackages有一种说不出的胸闷的感觉,如此以高配置性见长的spring,怎么在这一个小小的环节上就不能做得再灵活些呢,一定要一个个手写class路径么
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字
上传中....