`

在程序中设置hibernate configuration的Property

    博客分类:
  • java
阅读更多
在程序中设置hibernate configuration的Property
2009-08-01 20:26
在使用Hibernate时一般都会编写一个hibernate.cfg.xml文件,并在其中配置一些连接数据库的属性。这在很多情况下都是非常合适的,但是如果需要动态获得数据库链接信息,这种方法就不行了。假设我们要根据用户的输入信息连接响应的数据库,该怎么办呢?

我们都知道,Hibernate的SessionFactory是通过Configuration获得的,hibernate.cfg.xml文件中配置很多信息都对应为Configuration的Property,因此只要可以为Configuration设置属性并添加hbm.xml信息,就可以动态的获得SessionFactory了。具体代码如下:


Properties jdbcPros = new Properties();
jdbcPros.setProperty("hibernate.connection.useUnicode", "true");
jdbcPros.setProperty("hibernate.connection.characterEncoding", "UTF-8");
jdbcPros.setProperty("hibernate.connection.driver_class", "org.gjt.mm.mysql.Driver");
jdbcPros.setProperty("hibernate.connection.url", "jdbc:mysql://localhost:3306/your_db");
jdbcPros.setProperty("hibernate.connection.username", "root");
jdbcPros.setProperty("hibernate.connection.password", "your_pw");
jdbcPros.setProperty("hibernate.dialect", "net.sf.hibernate.dialect.MySQLDialect");
jdbcPros.setProperty("hibernate.show_sql", "false");
jdbcPros.setProperty("hibernate.use_outer_join", "true");
jdbcPros.setProperty("hibernate.transaction.factory_class", "net.sf.hibernate.transaction.JTATransactionFactory");

Configuration cfg =
new Configuration();
cfg.setProperties(jdbcPros);

try
{
    cfg.addFile("A.hbm.xml");
    cfg.addFile("B.hbm.xml");
}
catch (MappingException e) {
    e.printStackTrace();
}

try {
    SessionFactory sessionFactory = cfg.buildSessionFactory();
}
catch (HibernateException e) {
    e.printStackTrace();
分享到:
评论

相关推荐

    hibernate小程序源码

    "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <!-- Generated by MyEclipse Hibernate Tools. --> <hibernate-configuration> ...

    Hibernate开发jar包

    <property name="hibernate.connection.url">jdbc:mysql:///hibernate</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">root</...

    Hibernate配置数据连接

    PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <!?JDBC驱动程序--> <property name=...

    Hibernate配置各种数据源详解

    <hibernate-configuration> <!– 各属性的配置–> <!—为true表示将Hibernate发送给数据库的sql显示出来 –> <property name=”show_sql”>true</property> <!– SQL方言,这边设定的是MySQL –> <property ...

    hibernate配置

    <property name="数据库中表的其它字段所对应的持久化对象中的属性名" column="对应数据库中表的其它字段名" type="java.lang.String(数据库中其它字段的数据类型)"/> </hibernate-mapping> 三.配置hibernate...

    Hibernate1

    <property name="connection.url">jdbc:mysql://127.0.0.1/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">1234</property> <!-- SQL ...

    Hibernate注解编程实例

    <!-- EJB注解编程 --> <hibernate-configuration> <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/test </property> <property name="hibernate.connection.driver_cl

    Hibernate使用技巧汇总

    property-ref:关联类中用于与主控类相关联的属性名,默认为关联类的主键属性名 单向一对多需在一方配置,双向一对多需在双方进行配置 8.lazy=false:被动方的记录由hibernate负责记取,之后存放在主控...

    三步完成Hibernate + sql server200 + proxool连接池

    <property name="hibernate.proxool.pool_alias">dbpool</property> <property name="hibernate.proxool.xml"> ProxoolConf.xml </property> <property name="connection.provider_class"> org.hibernate....

    Myeclipse开发struts+hibernate+spring新手入门--环境配置---项目开发示例

    同加载struts方式类似,选择Myeclipse后在弹出的二级菜单中选择Add Hibernate Capabilities…后提示如下图所示: 可以根据提示,选择Hibernate Specification:本项目选择Hibernate3,其他默认,Next> 默认后直接...

    车间信息管理lxf_GraduationProject

    DOCTYPE hibernate-configuration (View Source for full doctype...)> - <!-- Generated by MyEclipse Hibernate Tools. --> - <hibernate-configuration> - <property name="myeclipse.connection.profile">...

    Hibernate Reference Documentation3.1

    1.2.3. Hibernate configuration 1.2.4. Building with Ant 1.2.5. Startup and helpers 1.2.6. Loading and storing objects 1.3. Part 2 - Mapping associations 1.3.1. Mapping the Person class 1.3.2. A ...

    自己写的Hibernate

    Here show you the dialog(My configuration): - TEST_COMMON + src + JRE System Liberary[jre1.5.0] + J2EE 1.4 Liberaries + commons-logging-1.0.4.jar - TEST_DAO + src + JRE System Library[jre...

    mysql+jdbc+jsp+Hibernate3.2+tomcattomcat5.028成功测试

    mysql,jdbc,Hibernate,tomcat [mysql+jdbc+jsp+Hibernate3.2+tomcattomcat5.028成功测试-简单] <br> 1 .数据库设计:库mydb 表events <br>EVENT_ID BIGINT(20) NOT NULL AUTOINC EVENT_DATE DATETIME...

    基于J2EE框架的个人博客系统项目毕业设计论文(源码和论文)

    Tomcat是一个免费的开源的Serlvet容器,在Tomcat中,应用程序的部署很简单,你只需将你的WAR放到Tomcat的webapp目录下,Tomcat会自动检测到这个文件,并将其解压。你在浏览器中访问这个应用的Jsp时,通常第一次会很...

    OA项目SSH整合框架

    1,在web.xml配置监听器(Spring Reference 15.2 Common configuration) <!-- 集成Spring --> <listener-class>org.springframework.web.context.ContextLoaderListener <param-name>...

    springmybatis

    2. 设置mybatis 配置文件:Configuration.xml, 在src_user目录下建立此文件,内容如下: 程序代码 程序代码 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" ...

    hibernate3.6 文档(pdf 格式)

    1.1.4. Hibernate configuration ............................................................................. 7 1.1.5. Building with Maven .................................................................

    从J2SE到J2EE知识点介绍

    3. 关于jsp在MyEclipse中打开的乱码问题 138 4. 关于html页面在eclipse中打开出现乱码情况 139 5. JSP页面通过URL传递中文参数的乱码问题 139 四、 Struts2 140 (一) 基于struts2的web应用的开发步骤 140 1. MVC设计...

    客户关系管理系统框架搭建(二)

    <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.show_sql">true</...

Global site tag (gtag.js) - Google Analytics