【问题标题】:Obtaining a sessionFactory in hibernate在休眠中获取 sessionFactory
【发布时间】:2014-01-10 01:53:18
【问题描述】:

我刚刚创建了两个小型 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);
    }

我的 Spring 应用程序上下文

<!-- <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 的其他应用程序中,我只使用休眠。我必须从 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 配置是否在第一个应用程序中完成所有操作?

【问题讨论】:

  • 是的,Spring 在 webapp 中为你做这件事。

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


【解决方案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;

您可以阅读有关 Spring 中自动装配的更多信息 here

【讨论】:

    猜你喜欢
    • 2016-06-28
    • 2012-01-23
    • 2016-03-21
    • 2013-06-11
    • 2015-06-10
    • 2012-07-25
    • 2014-06-24
    • 2019-04-13
    • 2013-01-13
    相关资源
    最近更新 更多