Big Data/Analytics Zone is brought to you in partnership with:

Over 25 years experience, specializing in business technology in the areas of web application development and collaborative computing I been working as an architect-developer for applications or product development. I am particularly interested in organizations that use leading technologies such as Android, Springs, Spring-WebFlow, JSP’s, Servlets, XML, Java, Struts, PHP, LDAP, Maven, JUnit and Web Application Servers. I have a very solid background with Java, OOP, Design-Patterns and Multi-Threading. I am also able to interface with all levels of management! My consulting services enable businesses maximize value from their business process initiatives and IT investments leading to improved organizational performance. Our services leverage business knowledge, process excellence, technology expertise and substantial domain experience in chosen industry verticals. Johnathan Mark has posted 7 posts at DZone. You can read more from them at their website. View Full User Profile

How to Use Spring's JavaConfig Style with Maven, JUnit, Log4J, Hibernate and HyperSQL

06.23.2013
| 9843 views |
  • submit to reddit

 

Spring’s Java Configuration (JavaConfig) style with Maven, JUnit, Log4J, Hibernate and HyperSQL (hsqldb)

The days of using MySQL, DB2, PostgreSQL etc for development are over. I don’t know why any programmer would develop with them.

Every developer should be running some in memory database like HSQLDB as part of the project for development and testing then move the a full size database for unit testing, staging and production.

This is a sample Spring Project to show how to use Spring’s Java Configuration (JavaConfig) style with Maven, JUnit, Log4J, Hibernate and HyperSQL (hsqldb).

This example also will show how to use @PropertySource for reading properties and using the Environment Object to add properties to your objects.

How to use Spring’s Java Configuration (JavaConfig) style and not XML files for configuation

Consider replacing Spring XML configuration with Spring’s Java Configuration (JavaConfig) style is the right way to go right now.

Using Spring XML configuration is so 2000’s the time has come to push the XML away and look at Spring’s Java Configuration (JavaConfig) style.

Here is the main code to my sample project

 public static void main(String[] args)
    {
        /**
         *
         * This is going to setup the database configuration in the applicationContext
         * you can see that I am using the new Spring's Java Configuration style and not some OLD XML file
         *
         */
        ApplicationContext context = new AnnotationConfigApplicationContext(DatabaseConfiguration.class);

        MessageService mService = context.getBean(MessageService.class);

        /**
         *
         *   This is going to create a message object and set the message to "Hello World" then pass the object to t
         *   the service layer for inserting into the database
         *
         */
        Message message = new Message();
        message.setMessage("Hello World");
        mService.SaveMessage(message);
        /**
         *
         * This is going to do a 2nd Message in database.
         *
         */
        message.setMessage("I love Regan");
        mService.SaveMessage(message);

        /**
         *
         * This is going to get the messages from database and do the following:
         *    - display number of message(s)
         *    - display each message in database
         *
         */
        List<Message> myList = mService.listMessages();
        LOGGER.debug("You Have " + myList.size() + " Message(s) In The Database");

        for (Message i : myList)
        {
            LOGGER.debug("Message: ID: " + i.getId() + ", Message: " + i.getMessage() + ".");
        }

        /**
         *
         * This is the end!!!
         *
         */
        LOGGER.debug("This is the end!!!!");
    }

Now lets take a good look at how I setup the database with Spring’s Java Configuration (JavaConfig) style and not in a XML file.

@Configuration
@EnableTransactionManagement
@ComponentScan(basePackageClasses = {Main.class})
@PropertySource("classpath:application.properties")
public class DatabaseConfiguration
{
    /**
     *
     * This is used to setup the database. It will load the schema.sql file which does a create table so we have
     * a table to work with in the project
     */
    @Bean
    public DataSourceInitializer dataSourceInitializer(DataSource dataSource)
    {
        ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
        resourceDatabasePopulator.addScript(new ClassPathResource("/schema.sql"));

        DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
        dataSourceInitializer.setDataSource(dataSource);
        dataSourceInitializer.setDatabasePopulator(resourceDatabasePopulator);
        return dataSourceInitializer;
    }

    /**
     *
     * This will be setting up a datasource using HyperSQL (hsqldb) in memory
     */
    @Bean
    public DataSource hsqlDataSource()
    {
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName(org.hsqldb.jdbcDriver.class.getName());
        basicDataSource.setUsername("sa");
        basicDataSource.setPassword("");
        basicDataSource.setUrl("jdbc:hsqldb:mem:mydb");
        return basicDataSource;
    }

    /**
     *
     * This setups the session factory
     */
    @Bean
    public LocalSessionFactoryBean sessionFactory(Environment environment,
                                                  DataSource dataSource)
    {

        /**
         *
         * Getting packageOfModelBean from package of message bean
         *
         */
        String packageOfModelBeans = Message.class.getPackage().getName();

        LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean();

        factoryBean.setDataSource(dataSource);
        factoryBean.setHibernateProperties(buildHibernateProperties(environment));
        factoryBean.setPackagesToScan(packageOfModelBeans);
        return factoryBean;
    }

    /**
     *
     * Loading all the hibernate properties from a properties file
     */
    protected Properties buildHibernateProperties(Environment env)
    {
        Properties hibernateProperties = new Properties();

        hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
        hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
        hibernateProperties.setProperty("hibernate.use_sql_comments", env.getProperty("hibernate.use_sql_comments"));
        hibernateProperties.setProperty("hibernate.format_sql", env.getProperty("hibernate.format_sql"));
        hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));

        hibernateProperties.setProperty("hibernate.generate_statistics", env.getProperty("hibernate.generate_statistics"));

        hibernateProperties.setProperty("javax.persistence.validation.mode", env.getProperty("javax.persistence.validation.mode"));

        //Audit History flags
        hibernateProperties.setProperty("org.hibernate.envers.store_data_at_delete", env.getProperty("org.hibernate.envers.store_data_at_delete"));
        hibernateProperties.setProperty("org.hibernate.envers.global_with_modified_flag", env.getProperty("org.hibernate.envers.global_with_modified_flag"));

        return hibernateProperties;
    }

    /**
     *
     * This is setting up the hibernate transaction manager
     *
     */
    @Bean
    public HibernateTransactionManager hibernateTransactionManager(SessionFactory sessionFactory)
    {
        return new HibernateTransactionManager(sessionFactory);
    }
}

You can see how easy it is to use Spring’s Java Configuration (JavaConfig) style and Not XML.. The time of using XML files with Springs is over…

You can see the project on github at https://github.com/JohnathanMarkSmith/NoMySQL

Download and Run

So now its time to download the project from github and run it, You need to do the following commands:

git clone git@github.com:JohnathanMarkSmith/NoMySQL.git
cd NoMySQL
mvn package
cd target
java -jar NoMySQL.jar

Thats it and you should see the following line on the console:

2013-05-24 08:28:23,401 [main] DEBUG com.johnathanmarksmith.noMySQL.Main - You Have 2 Message(s) In The Database
2013-05-24 08:28:23,401 [main] DEBUG com.johnathanmarksmith.noMySQL.Main - Message: ID: 1, Message: Hello World.
2013-05-24 08:28:23,401 [main] DEBUG com.johnathanmarksmith.noMySQL.Main - Message: ID: 2, Message: I love Regan.
2013-05-24 08:28:23,401 [main] DEBUG com.johnathanmarksmith.noMySQL.Main - This is the end!!!!

This Project is using Java, Spring, Hibernate, Maven, jUnit, Log4J, HSQLDB and Github.

If you have any questions or comments please email me at john@johnathanmarksmith.com

Published at DZone with permission of its author, Johnathan Mark Smith.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Nicolas Frankel replied on Sun, 2013/06/23 - 9:06am

The days of using MySQL, DB2, PostgreSQL etc for development are over. I don’t know why any programmer would develop with them.

Hint: perhaps to test your software in real conditions, including schema creation? I'm not sure the same SQL is used across all those different platforms... yet.

PS: please use code formatting for code, it's unreadable as it stands.

Stephane Eybert replied on Mon, 2013/06/24 - 5:55am in response to: Nicolas Frankel

Hello Nicolas,

I'm still using MySQL server on a Maven Spring Data JPA project for integration testing because sometimes HSQLDB gets my Maven build to fail.

Lately it was this one issue that stopped the show: the JPA annotation for a LOB TEXT 65535 string does not work on HSQLDB server. I'm using the 2.2.9 version.

I posted the issue at https://forum.hibernate.org/viewtopic.php?f=1&t=1026647

Maybe you'd have an idea...

Kind Regards,

Nicolas Frankel replied on Mon, 2013/06/24 - 6:10am in response to: Stephane Eybert

 Try using another in-memory DB, like H2. I think H2 is the newer version of HSQLDB

Stephane Eybert replied on Mon, 2013/06/24 - 7:10am in response to: Nicolas Frankel

Are you sure the HSQLDB project is being replaced by H2 ?

I can see the HSQLDB project is still actively maintained.

After all, your blog article was advocating its use instead of other database servers.

On top of that, the H2 server is suffering from an issue related to JPA or so it seems: https://jira.springsource.org/browse/DATAJPA-354

I would therefore still like to have the HSQLDB server handle correctly the LOB TEXT 65535 string.

Kind Regards,

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.