UP | HOME

Hibernate Spring/Spring Security

Table of Contents

1 Connecting a database

We use the Maven-plugin to load required libraries, and Hibernate to generate java-classes and the database schema from xml mapping files, See Chapter 5, Hibernate Community Documentation how to define the mappings. Finally the database connection is tested by persisting some data.

  1. Create Java Project
    • Select File -> New -> Project -> Java Project, call it BookCase, leave default selections unchanged and finish.
  2. Add Library files
    • In Package Explorer rightclick on Project select Maven -> Enable Dependency Management. Give it a name, e.g. MvnBookCase and click on Next.
    • Click Add to add the hibernate, mysql and hsqldb artifacts. (Enter groupId or artifactId and select from Search Results). Click Finish
  3. Copy the Hibernate Configuration file hibernate.cfg.xml and the log4j-propertiy file log4j.properties into the src folder of the project.
    • Rightclick on src-folder, select New -> File and enter the filename, use Source tab to paste the content.
  4. Add src-folder to build path (if necessary). Rightclick on src-folder choose Build Path -> Add to Build Path.
  5. Rightclick on src-folder, choose New -> Package and create package de.botzenhart.bookcase.data.
  6. Copy the xml-description files Book.hbm.xml, Author.hbm.xml and Keyword.hbm.xml into the package de.botzenhart.bookcase.data.
  7. Create a new Hibernate configuration.
    • Select Hibernate Perspective (click on Open-Perspective-Icon in top right lash, left most icon).
    • In vew Hibernate Configurations rightclick and select Add Configuration (or click the icon in menue bar)
    • Enter a name, e.g. BookCase
    • Select Project BookCase, select Configuration file, Use existing and select BookCase/src/hibernate.cfg.xml. - Click /Apply and OK
  1. Generate Java-classes. Select Run -> Hibernate Code Generation and
    • select rin configuration BookCaseConfiguration, if it has already been created otherwise select Hibernate Code Generation Configurations … and create new launch configuration
      • select New launch configuration (leftmost icon, top menu of the view)
      • choose name BookCaseConfiguration and select \BookCase\src as Output directory.
      • switch to tab Exporters and select Domain Code
      • click Apply and and Run (twice).
  2. Generate database schema.
    • Make sure the connection URL in the file hibernate.cfg.xml is set properly to reflect the /path/to/copybase/database/bookcase.
    • In view Hibernate Configurations rightclick on project and select Run SchemaExport. This has to be done twice, since the table BOOK_AUTHOR has not been created.
  3. Open package de.botzenhart.bookcase.data in the src folder. You should find classes Book.java, Author.java, and Keyword.java
  4. Open the database using the ant build.xml in directory /copybase/test and run ant from that directory. You should find tables Book, Author, Keywords, and Book_Authors.
  5. Copy the classes BookCaseHibernateSessionFactory.java, BookCasePersistence.java into package de.botzenhart.bookcase.persistence (which you have to create).
  6. Create a new java-class TestHibernateBookcase.java (in package de.botzenhart.bookcase.test.persistence) to test the access to the database.
  7. Run your class as Java Application:
    • In Package explorer rightclick on your project and select Run As -> Java Application
  8. Open the database. You should find the expected entries in the tables. Alternatively you could open /copybase/database/bookcase.script, where you find the SQL-statements to build the database. (HSQLDB database is in memory).

2 Import a Spring Security Project

The project uses user-authentication and role based authorization to control acces to resources. The user information is stored in a MySQL database. When you start the application a user test with password test is stored in the application.

  1. Import project SecuredBookCase
    • In Package Explorer rightclick on Import, select General -> Existing Projects into Workspace and click Next
    • Select SecuredBookCase as root directory check Copy projects into workspace, and click Finish.
  2. Start MySQL server.
  3. Create database test.
  4. Run SecuredBookCase on server:
    • Rightclick on project and select Run As -> Run on Server, select tomcat6 as server and click Finish.

Author: Rainer Schuler

Date: 2011-02-11 Fr

HTML generated by org-mode 7.4 in emacs 22