Prerequisite Steps
1) Create java class (bean) Employee
2) Create Database and table employee
3) Create hbm.xml
4) make all mappings
3.4) Creating the Spring Configuration File
This section deals with configuring the various information needed for the Spring Framework. In Spring, all the business objects are configured in Xml file and the configured business objects are called Spring Beans. These Spring Beans are maintained by the IOC which is given to the Client Application upon request. Let us define a data source as follows,
spring-hibernate.xml
Refer : http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-3.html
The above bean defines a data-source of type 'org.apache.commons.dbcp.BasicDataSource'
. More importantly, it defines the various connection properties that are needed for accessing the database. For accessing the MySql database, we need MySql database driver which can be downloaded from http://dev.mysql.com/downloads/connector/j/5.1.html. The first property called driverClassName
should point to the class name of the MySql Database Driver. The second property url represents the URL string which is needed to connect to the MySql Database. The third and the fourth properties represent the database username
and the password
needed to open up a database session.
Now, let us define the second Spring Bean which is the SessionFactoryBean
. If you would have programmed in Hibernate, you will realize that SessionFactoryBean
is responsible for creating Session
objects through which Transaction
and Data accessing is done. Now the same SessionFactoryBean
has to be configured in Spring's way as follows,
Refer :
http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-3.html
To make the SessionFactoryBean
to get properly configured, we have given two mandatory information. One is the data-source information which contains the details for accessing the database. This we have configured already in the previous step and have referred it here using the 'ref'
attribute in the 'property'
tag. The second one is a list of Mapping files which contains the mapping information between the database tables and the Java class names. We have defined one such mapping file in section 2 and have referenced the same here with the 'list'
tag.
The 3rd important Spring Bean is the Hibernate Template. It provides a wrapper for low-level data accessing and manipulation. Precisely, it contains methods for inserting/delting/updating/finding data in the database. For the Hibernate Template to get configured, the only argument is the SessionFactoryBean
object as represented in the following section,
Refer : http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-3.html
The final Bean definition is the Dao class which is the client facing class. Since this class has to be defined in the Application level, it can contain any number of methods for wrapping data access to the Client. Since we know that it is the Hibernate Template class that interacts with the database, it will be ideal a refer an instance of Hibernate Template to the Dao class.
Refer : http://www.javabeat.net/articles/42-integrating-spring-framework-with-hibernate-orm-framewo-3.html
Reference Link : http://www.javabeat.net/articles/
42-integrating-spring-framework-with-hibernate-orm-framewo-1.html
No comments:
Post a Comment