Criteria interface

The Criteria interface is similar to the Query interface--it allows the developers to write criteria query objects to get a result based on some restrictions or criteria.

In the Spring Framework, developers have a choice of using the SessionFactory instance or HibernateTemplate to perform Hibernate integrations. The SessionFactory interface is obtained from the configuration of database connectivity parameters and location of the mapping, which, then using DI, can be used in the Spring application. The SessionFactory interface can be configured as follows:

<bean id="sessionFactory" 
  class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 
  <property name="dataSource" ref="dataSource"/> 
  <property name="mappingResources"> 
    <list> 
      <value>book.hbm.xml</value> 
    </list>  
  </property> 
  <property name="hibernateProperties"> 
    <props>   
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect </prop> <prop key="hibernate.show_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">update</prop> </props> </property> </bean>

The attributes used in the configuration are as follows:

  • dataSource: This provides information about the database properties
  • mappingResource: This specifies the name of the files that provide information about mapping of the data members to the table and its columns
  • hibernateProperties: This provides information about the following hibernate properties:
    • dialect: This is used by the framework to generate SQL queries as per the underlying database
    • show_sql: This displays the SQL query fired by the framework on the console
    • hbm2ddl.auto: This provides information on whether to create or update the table with which operations to perform

While using SessionFactory, the developers do not write code that uses Spring APIs. However, we've already discussed the template classes a few pages back. HibenateTemplate is one such template that helps developers write decoupled applications. The HibernateTemplate configuration is as follows:

<bean id="hibernateTemplate"class=
"org.springframework.orm.hibernate5.HibernateTemplate"> <property name="sessionFactory"ref="sessionFactory"></property> </bean>

Let's integrate SessionFactory in our Book project one by one with the help of the following steps.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.235.104