gpt4 book ai didi

java - hibernate中获取sessionFactory

转载 作者:太空宇宙 更新时间:2023-11-04 07:10:29 24 4
gpt4 key购买 nike

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

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

在 DAOImpl 中的 Web 应用程序中,我只是注入(inject) 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 的应用程序中,我只使用 hibernate。我必须从annotationConfiguration 获取sessionFactory,然后打开 session 并开始事务。

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 配置的这一部分正在配置 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 设置 Hibernate session 工厂的更多信息 here

这部分 DAO 代码负责要求 Spring 注入(inject) session 工厂:

@Autowired
private SessionFactory sessionFactory;

您可以阅读有关 Spring Autowiring 的更多信息 here

关于java - hibernate中获取sessionFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20716547/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com