在hibernate中获取sessionFactory

时间:2013-12-21 07:08:36

标签: java spring hibernate java-ee spring-mvc

我刚创建了两个小的crud应用程序,一个是Web应用程序,另一个是我从main方法运行。

我对如何在两个应用程序中获取sessionFactory对象感到困惑。

在DAOImpl的web应用程序中,我只是注入了sessionFactory对象并执行了

@Repository
public class ContactDaoImpl implements ContactDao {

    @Autowired
    private SessionFactory sessionFactory;

    public void addContact(Contact contact) { 

        //save: Persist the given transient instance
        sessionFactory.getCurrentSession().save(contact);
    }

My Spring Application Context

<!-- <context:property-placeholder> XML element automatically registers a new PropertyPlaceholderConfigurer 
    bean in the Spring Context. -->
    <context:property-placeholder location="classpath:database.properties" />
    <context:component-scan base-package="com.contactmanager"/>

    <!-- enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="hibernateTransactionManager"/>

    <!-- View Resolver Configured -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix">
            <value>/WEB-INF/views/</value>
        </property>
        <property name="suffix">
            <value>.jsp</value>
        </property>
    </bean>

<!-- Creating DataSource -->
<bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${database.driver}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.user}" />
        <property name="password" value="${database.password}" />
    </bean>




<!-- To persist the object to database, the instance of SessionFactory interface is created. 
SessionFactory is a singleton instance which implements Factory design pattern. 
SessionFactory loads hibernate.cfg.xml and with the help of TransactionFactory and ConnectionProvider 
implements all the configuration settings on a database. -->

<!-- Configuring SessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>com.contactmanager.model.Contact</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>             
            </props>
        </property>
    </bean>

<!-- Configuring Hibernate Transaction Manager -->
    <bean id="hibernateTransactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

但在其他应用程序中我不使用Spring我只使用hibernate。我必须从annotationConfiguration获取sessionFactory然后打开会话并开始事务。

AnnotationConfiguration().configure().buildSessionFactory();

   Session session = HibernateUtil.getSessionFactory().openSession();

    session.beginTransaction();

    Stock stock = new Stock();

    stock.setStockCode("4715");
    stock.setStockName("GENM");

    session.save(stock);
    session.getTransaction().commit();

任何人都可以告诉我为什么我必须编写更多行代码来保存对象。 Spring配置是否在第一个应用程序中执行所有操作?

1 个答案:

答案 0 :(得分:1)

Spring配置的这一部分是配置sessionFactory bean:

<!-- Configuring SessionFactory -->
<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="annotatedClasses">
        <list>
            <value>com.contactmanager.model.Contact</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">${hibernate.dialect}</prop>
            <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>             
        </props>
    </property>
</bean>

您可以阅读有关使用Spring here

设置Hibernate会话工厂的更多信息

这部分DAO代码负责要求Spring注入会话工厂:

@Autowired
private SessionFactory sessionFactory;

您可以在春季here了解更多有关自动装配的信息