博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Use Hibernate4 native API
阅读量:5749 次
发布时间:2019-06-18

本文共 3394 字,大约阅读时间需要 11 分钟。

  hot3.png

#Use Hibernate4 native API

In Spring 3.1, a new package named ~.hibernate4 is included( in spring-orm maven dependency). With this new APIs, using Hibernate 4 in Spring projects becomes more easy than before, you are not required to extend the HiberanteDaoSupport class or use HibernateTemplate in your implemnetation class.

  1. Register a DataSource bean

<pre> &lt;jdbc:embedded-database id="dataSource" > &lt;/jdbc:embedded-database> </pre>

  1. Declare Spring specific LocalSessionFactoryBean bean.

<pre> &lt;bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> &lt;property name="dataSource" ref="dataSource" /> &lt;property name="packagesToScan"> &lt;list> &lt;value>com.hantsylabs.example.spring.model&lt;/value> &lt;/list> &lt;/property> &lt;property name="hibernateProperties"> &lt;value> hibernate.dialect=org.hibernate.dialect.HSQLDialect hibernate.format_sql=true hibernate.show_sql=true hibernate.hbm2ddl.auto=create &lt;/value> &lt;/property> &lt;/bean> </pre>

The legacy Spring Hibernate3 integration provides two version of SessionFactoryBean, ~.hibernate3.LocalSessionFactoryBean targets the legacy Hibernate XML mapping configuration, ~.hibernate3.annotation.AnnotationSessionFactoryBean is use for annotation based configuration.

  1. Register a transaction manager.

<pre> &lt;bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> &lt;property name="sessionFactory" ref="sessionFactory" /> &lt;/bean> </pre>

  1. Now you can inject SessionFactory in your implementation class freely.

<pre> @Repository public class Hibernate4ConferenceDaoImpl implements ConferenceDao { private static final Logger log = LoggerFactory .getLogger(Hibernate4ConferenceDaoImpl.class); @Autowired SessionFactory sessionFactory; private Session session() { return sessionFactory.getCurrentSession(); } @Override public Conference findById(Long id) { return (Conference) session().load(Conference.class, id); } // other methods... } </pre>

It is very simple and stupid. All the codes are based on Hiberante Session APIs now.

NOTE: Hiberante 3 also can be configured like these, I do not demonstrate the steps here.

For the newest project, you can also use the Spring fluent java configuration API for all Spring configurations instead of the XML configuration.

The following is an example of the Java configuration, it is equivalent to the XML format above.

<pre> @Configuration @ComponentScan(basePackages={"com.hantsylabs.example.spring.dao","com.hantsylabs.example.spring.hibernate4"}) public class HibernateConfig { @Bean public DataSource dataSource() { return new EmbeddedDatabaseBuilder().build(); } @Bean public SessionFactory sessionFactory() { LocalSessionFactoryBuilder builder = new LocalSessionFactoryBuilder( dataSource()); builder.scanPackages("com.hantsylabs.example.spring.model") .addProperties(hibernateProperties()); return builder.buildSessionFactory(); } private Properties hibernateProperties() { Properties extraProperties = new Properties(); extraProperties.put("hibernate.format_sql", "true"); extraProperties.put("hibernate.show_sql", "true"); extraProperties.put("hibernate.hbm2ddl.auto", "create"); return extraProperties; } @Bean public PlatformTransactionManager transactionManager() { return new HibernateTransactionManager(sessionFactory()); } } </pre>

转载于:https://my.oschina.net/hantsy/blog/134679

你可能感兴趣的文章
Javascript String类的属性及方法
查看>>
[LeetCode] Merge Intervals
查看>>
Struts2 学习小结
查看>>
在 Linux 系统中安装Load Generator ,并在windows 调用
查看>>
whereis、find、which、locate的区别
查看>>
一点不懂到小白的linux系统运维经历分享
查看>>
桌面支持--打不开网页上的pdf附件解决办法(ie-tools-compatibility)
查看>>
nagios监控windows 改了NSclient++默认端口 注意事项
查看>>
干货 | JAVA代码引起的NATIVE野指针问题(上)
查看>>
POI getDataFormat() 格式对照
查看>>
nginx rewrite
查看>>
CSS中规则@media的用法
查看>>
pychecker:分析你的python代码
查看>>
系列3:WAS Liberty Profile hello mysql jdbc
查看>>
基础知识:python模块的导入
查看>>
Android MVC之我的实现
查看>>
CCNA实验之:网络地址转换(NAT)实验
查看>>
/etc/resolv.conf文件详解
查看>>
【转】VC的MFC中重绘函数的使用总结(整理)
查看>>
JQuery日记_5.13 Sizzle选择器(六)选择器的效率
查看>>