0

我有这个代码:

SessionFactory sessionFactory = new Configuration().configure("hibernate.cfg.xml");

我如何将其配置为 bean?hibernate.cfg.xml 包含以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="sessionFactory">
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.password">scott</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@x.x:1521:x</property>
        <property name="hibernate.connection.username">scott</property>
        <property name="hibernate.default_schema">SCOTT</property>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="hibernate.search.autoregister_listeners">false</property>
        <mapping resource="ormbyxml/domain/Emp.hbm.xml" />
        <mapping resource="ormbyxml/domain/SalaryLog.hbm.xml" />
        <mapping resource="ormbyxml/domain/Dept.hbm.xml" />
        <mapping resource="ormbyxml/domain/Salgrade.hbm.xml" />
        <mapping resource="ormbyxml/domain/Bonus.hbm.xml" />
    </session-factory>
</hibernate-configuration>
4

1 回答 1

2

当然,这在 Spring 参考文档中进行了解释。请参阅http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/orm.html#orm-session-factory-setup

你可以像这样传递你的休眠配置文件的路径:

<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
于 2011-09-12T11:17:58.233 回答