<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Junky's IT Notebook

    統(tǒng)計(jì)

    留言簿(8)

    積分與排名

    WebSphere Studio

    閱讀排行榜

    評(píng)論排行榜

    Spring AOP with Hibernate

    One of the sweetiest things the Spring framework gives is Hibernate support is built-in by the time it's born.

    Following is a quick guide to configure Hibernate's SessionFactory in Spring. For a detailed version about Spring's Hibernate support, read http://www.springframework.org/docs/data_access.html .



    With Spring, Hibernate's SessionFactory no longer needs to bind itself to JNDI; nor use Hibernate's own hibernate.cfg.xml method, which is a little bit tricky to code in Hibernate 2.x (as Hibernate2 doesn't use the once-and-only-once configure() anymore).

    Instead, we use org.springframework.orm.hibernate.LocalSessionFactoryBean .

    <bean?id= "MySessionFactory"? class = "org.springframework.orm.hibernate.LocalSessionFactoryBean" >

    ??? <property?name= "mappingResources" >

    ?????? <list>

    ???????? <value>mappings/Book.hbm.xml</value>

    ???????? <value>mappings/Patron.hbm.xml</value>

    ???????? <value>mappings/BorrowRecord.hbm.xml</value>

    ?????? </list>

    ??? </property>

    ??? <property?name= "hibernateProperties" >

    ?????? <props>

    ???????? <prop?key= "hibernate.dialect" >net.sf.hibernate.dialect.MySQLDialect</prop>

    ???????? <prop?key= "hibernate.query.substitutions" > true = 1? false = 0 </prop>

    ???????? <prop?key= "hibernate.show_sql" > false </prop>

    ???????? <prop?key= "hibernate.use_outer_join" > false </prop>

    ?????? </props>

    ??? </property>

    ??? <property?name= "dataSource" ><ref?bean= "MyDataSource" /></property>

    </bean>

    The above parameters are simple and verbose:

    However, we don't need to configure a transaction manager inside the SessionFactory, as we will see below.

    After configuring this, we need to provide a setter method in our business objects that need to use Hibernate's SessionFactory:

    import? net.sf.hibernate.SessionFactory;

    ....

    public?class? MyBusinessObjectImpl? implements? MyBusinessObject

    {

    ??? private? SessionFactory?sessionFactory;

    ??

    ?

    public? void? setSessionFactory(SessionFactory?sessionFactory)

    ??? {

    ?????? this .sessionFactory?=?sessionFactory;

    ??? }

    ??? public? SessionFactory?getSessionFactory()

    ??? {

    ?????? return?this .sessionFactory;

    ??? }

    ....

    and hook it up to Spring.

    <bean?id= "MyBusinessObject"? class = "library.MyBusinessObjectImpl" >

    ??? <property?name= "sessionFactory" >

    ?????? <ref?bean= "MySessionFactory" />

    ??? </property>

    </bean>

    ?

    How about transactions support? And how do I get Hibernate's Session inside my business objects?

    That's Spring framework's another power - HibernateInterceptor and TransactionInterceptor. With them, together with configurations made in Spring, methods inside business objects don't need to write a single line of code for that; instead, a Session will be bound to the business object's current thread, opened and closed automatically; and a transaction will also begin and end automatically.

    The sequence is like this:

    1. Transaction begins
    2. Hibernate session opened and bound to the current thread
    3. Actual method execution
    4. Hibernate session bound to the current thread closed
    5. Transaction ended

    This is done with the help of Spring's AOP capability.

    However, we need to configure a transaction manager first.

    <bean?id= "MyTransactionManager"? class = "org.springframework.transaction.jta.JtaTransactionManager" />

    The above configures a transaction manager that will access an UserTransaction inside the environment, usually in a J2EE container, or a servlet container with transaction support.

    Alternately you may want to have a look at org.springframework.orm.hibernate.HibernateTransactionManager .

    Next, we need to define the transaction attribute for our business methods. This is done in org.springframework.transaction.interceptor.TransactionInterceptor ? .

    <bean?id= "MyTransactionInterceptor"?

    class = "org.springframework.transaction.interceptor.TransactionInterceptor" >

    ??? <property?name= "transactionManager" ><ref?bean= "MyTransactionManager" /></property>

    ??? <property?name= "transactionAttributeSource" >

    ?? <value>

    ????????? library.MyBusinessImpl.borrowBook=PROPAGATION_REQUIRED

    ????????? library.MyBusinessImpl.returnBook=PROPAGATION_REQUIRED

    ????????? library.BookSearchImpl.*=PROPAGATION_SUPPORTS

    ?? </value>

    ??? </property>

    </bean>

    You can use a wildcard to tell every method in that business object uses the same transaction attribute. However it is not recommended, as the private methods used inside the business object will have transactions too, which you may not need to.

    By the way, the TransactionInterceptor's default behaviour is to commit an transaction anyway, and rollback whenever a RuntimeException is caught - much the same as EJB's behaviour. If you want to control TransactionInterceptor's behaviour when exception is caught, you may tell it with a plus (+) or minus (-) sign, followed by the name of the exception, to commit or rollback a transaction even if an exception mentioned is caught.

    An example:

    library.MyBusinessImpl.addBook=PROPAGATION_REQUIRED,-SeriesNotFoundException,+CategoryNotFoundException

    This means that when SeriesNotFoundException is thrown inside the addBook() method, the transaction will roll back; on the other hand when CategoryNotFoundException is thrown the transaction will be commited anyway.

    In most cases you don't need to specify the exception's name with the package it belongs; you just need to specify simply the exception's name.

    Then our business objects will need to define as an AOP "target". This is just a change in a name; make sure your code will not call these business objects directly. So change the above business object declaration to

    <bean?id= "MyBusinessObjectTarget"? class = "library.MyBusinessObjectImpl" >

    ??? <property?name= "sessionFactory" >

    ?????? <ref?bean= "MySessionFactory" />

    ??? </property>

    </bean>

    Final step is to expose the business object on Spring's ApplicationContext, but not the business object itself; instead we use a ProxyFactoryBean, provided by Spring.

    <bean?id= "MyBusinessObject"? class = "org.springframework.aop.framework.ProxyFactoryBean" >

    ??? <property?name= "proxyInterfaces" >

    ?????? <value>library.MyBusinessObject</value>

    ??? </property>

    ??? <property?name= "interceptorNames" >

    ?????? <list>

    ????????? <value>MyTransactionInterceptor</value>

    ????????? <value>MyHibernateInterceptor</value>

    ????????? <value>MyBusinessObjectTarget</value>

    ?????? </list>

    ??? </property>

    </bean>

    • proxyInterfaces: the interface implemented by your business object.
    • interceptorNames: the interceptors to be applied when methods on the business interface is called. Be careful, they have to be placed in order.

    Then in our business method we just need to obtain the Session instance, and ignore everything else - everything is done behind the scene.

    import? net.sf.hibernate.*;

    import? org.springframework.orm.hibernate.SessionFactoryUtils;

    ....

    public? Book?findBook( int? bookID)? throws? BookNotFoundException,?DataAccessException

    {

    ??? //get?the?Session?instance?already?bound?to?current?thread?and?opened

    ??? Session?session?=?SessionFactoryUtils.getSession(getSessionFactory(),? false );

    ??? try

    ??? {

    ?????? Book?book?=?(Book)session.load(Book.class,?bookID);

    ?????? return? book;

    ??? }

    ??? catch (ObjectNotFoundException?e)

    ??? {

    ?????? throw?new? BookNotFoundException();

    ??? }

    ??? catch (HibernateException?e)

    ??? {

    ?????? throw? SessionFactoryUtils.convertHibernateAccessException(e);

    ??? }

    }

    ?

    Alternately you may also use HibernateTemplate and TransactionTemplate, though the above method is simpler. Read http://www.hibernate.org/110.html for details (this page seems more updated than the one in Spring?).

    posted on 2006-10-17 14:08 junky 閱讀(477) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): spring

    主站蜘蛛池模板: 免费一级毛片清高播放| 亚洲第一成年男人的天堂| 中文字幕不卡免费高清视频| 亚洲AV无码久久精品成人 | 中国极品美軳免费观看| 亚洲日本一区二区三区| 热99re久久免费视精品频软件| 亚欧乱色国产精品免费视频| 亚洲1234区乱码| 在线观看亚洲成人| 丁香花在线观看免费观看| 一区二区免费电影| 亚洲午夜无码久久久久小说| 亚洲欧洲成人精品香蕉网| 毛片网站免费在线观看| 高清永久免费观看| 亚洲人成色77777在线观看| 国产亚洲免费的视频看| 国产精品va无码免费麻豆| 久久久久成人精品免费播放动漫| 亚洲精品久久无码av片俺去也| 亚洲AV无码一区东京热| vvvv99日韩精品亚洲| 成年免费大片黄在线观看岛国| 水蜜桃视频在线观看免费播放高清| 亚洲午夜福利在线视频| 亚洲伦理一区二区| 亚洲午夜AV无码专区在线播放| 成人奭片免费观看| 最近免费2019中文字幕大全| 一级毛片免费播放试看60分钟| 亚洲jjzzjjzz在线观看| 亚洲高清在线视频| 久久亚洲2019中文字幕| 国产小视频在线观看免费| 亚洲欧洲免费无码| xxxxx免费视频| 99爱在线精品视频免费观看9| 免费一区二区无码视频在线播放| 美女视频黄免费亚洲| 亚洲图片校园春色|