设为首页 加入收藏

TOP

在spring中使用Hibernate5(二)
2019-09-17 15:45:19 】 浏览:32
Tags:spring 使用 Hibernate5
:h2:mem:db;DB_CLOSE_DELAY=-1"); dataSource.setUsername("sa"); dataSource.setPassword("sa"); return dataSource; } @Bean public PlatformTransactionManager hibernateTransactionManager() { HibernateTransactionManager transactionManager = new HibernateTransactionManager(); transactionManager.setSessionFactory(sessionFactory().getObject()); return transactionManager; } private final Properties hibernateProperties() { Properties hibernateProperties = new Properties(); hibernateProperties.setProperty( "hibernate.hbm2ddl.auto", "create-drop"); hibernateProperties.setProperty( "hibernate.dialect", "org.hibernate.dialect.H2Dialect"); return hibernateProperties; } }

4.2. Using XML Configuration

As a secondary option, we can also configure Hibernate 5 with an XML-based configuration:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="...">
 
    <bean id="sessionFactory"
      class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="dataSource"
          ref="dataSource"/>
        <property name="packagesToScan"
          value="com.baeldung.hibernate.bootstrap.model"/>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">
                    create-drop
                </prop>
                <prop key="hibernate.dialect">
                    org.hibernate.dialect.H2Dialect
                </prop>
            </props>
        </property>
    </bean>
 
    <bean id="dataSource"
      class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
        <property name="driverClassName" value="org.h2.Driver"/>
        <property name="url" value="jdbc:h2:mem:db;DB_CLOSE_DELAY=-1"/>
        <property name="username" value="sa"/>
        <property name="password" value="sa"/>
    </bean>
 
    <bean id="txManager"
      class="org.springframework.orm.hibernate5.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
</beans>

As we can easily see, we’re defining exactly the same beans and parameters as in the Java-based configuration earlier.

To bootstrap the XML into the Spring context, we can use a simple Java configuration file if the application is configured with Java configuration:

@Configuration
@EnableTransactionManagement
@ImportResource({"classpath:hibernate5Configuration.xml"})
public class HibernateXMLConf {
    //
}

Alternatively, we can simply provide the XML file to the Spring Context, if the overall configuration is purely XML.

5. Usage

At this point, Hibernate 5 is fully configured with Spring, and we can inject the raw Hibernate SessionFactory directly whenever we need to:

public abstract class BarHibernateDAO {
 
    @Autowired
    private SessionFactory sessionFactory;
 
    // ...
}

6. Supported Databases

Unfortunately, the Hibernate project doesn’t exactly provide an official list of supported databases.

That being said, it’s easy to see if a particular database type might be supp

首页 上一页 1 2 3 下一页 尾页 2/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇PlayJava Day002 下一篇Jsp学习笔记(4)——分页查询

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目