1

I'm used to have persistence.xml in my projects in which I define the provider to use (hibernate in most cases).

However, I'm starting a new project in which it is mandatory to use Spring framework. I've seen some blogs describing the integration of Hibernate in Spring and I've understood that I should declare a session factory in spring's beans descriptor org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean

All the examples I've seen don't mention the use of persistence.xml, persistence context, entity manager...

I'm not sure I understand this point, I always thought that Hibernate is just a provider of JPA unless the factory declared in application-context.xml is doing something in background. If it is the case, I would like to understand how it is working..

thanks in advance...

javaxiss
  • 680
  • 3
  • 13
  • 34

1 Answers1

3

AnnotationSessionFactoryBean is Factory bean implemented by Spring to create Hibernate Session Factory and shared to Spring's Application Context.if you are planning to use Direct Hibernate ( in case you dont need persistent.xml / per-cont.xml / entityManager) you can provide the properties in AnnotationSession FactoryBean. and can be injected in Any DAO.
How ever if you are planing to wire through JPA. Then you need ( persistent and persistent-context and entity Manager). In order to do that you required three steps
1. declare / configure Spring's JPA Adapter to create EntityManager instance for you

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">


Inside JPA Bean declaration provide details about your database and who is ORM provider such as ( hibernate /toplink / ....) in your case Hibernate

<property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="false" />
            <property name="databasePlatform"  value="org.hibernate.dialect.Oracle9Dialect"/>
        </bean>
    </property>

then Provide information about your persistent entity details in persistent.xml or some way

    <property name="persistenceXmlLocation" value="classpath:persistence.xml" />


if you have any specific JPA properties then

 <property name="jpaProperties">
      <props>
    <!--      <prop key="hibernate.cache.provider_class">
                org.hibernate.cache.EhCacheProvider
        </prop>
         -->
        <prop key="hibernate.format_sql">false</prop>
        <prop key="hibernate.use_sql_comments">false</prop>
      </props>
    </property>


Bottom line, Spring provies adapters to directly to Hibernate ( in that case your dont need Jpa files such as persistent.xml and so but you need hibernate related files like hbm files) or adapters to the JPA ( in that case you have provide details about who is JPA vendor and instrut spring how to connect to the JPA vendor).


Hope the above clarifies.

Mani
  • 3,274
  • 2
  • 17
  • 27
  • Thanks for your answer :-) , what do you mean by using Direct Hibernate. In my opinion, it is mandatory first of all to use JPA and to choose after that a provider. Mapping annotations in entities are not Hibernate annotations. Sorry but I still a little bit confused... – javaxiss Dec 24 '13 at 09:25
  • We can implement orm with hibernatr only by using hbm or hibernate annotations , sessionfactory without jpa.jpa is common spec for orm and it does provide interfaces not implementation. If you implement using hibernate you cant easily switch to other orm tools easily such as toplink, ibatis and do.. but if you implement using jpa and jpa annotations then your app will be lously coupled. So it cant be easily switch any orm without changing code (in spring you just need to change confuguration to supply implementation ) – Mani Dec 24 '13 at 16:04
  • In netshell it like usibg jpa similar to using interface and usibg hibernate is similar to tied to concerte classes. . – Mani Dec 24 '13 at 16:06