Code, JavaEE

Log – Hibernate using Spring Framework and Spring Test

The database and table/user from the Hibernate – Classic post are used for this example.

 

Log.java


package principles;

import java.io.Serializable;
import java.sql.Timestamp;

/**
 * Log
 *
 * @author david.king
 */
public class Log implements Serializable {

private Integer id;
 private String descriptor;
 private Timestamp ts;

public Log() {}

public Log(String descriptor) {
 this.descriptor = descriptor;
 }

public Integer getId() {
 return id;
 }

public void setId(Integer id) {
 this.id = id;
 }

public String getDescriptor() {
 return descriptor;
 }

public void setDescriptor(String descriptor) {
 this.descriptor = descriptor;
 }

public Timestamp getTs() {
 return ts;
 }

public void setTs(Timestamp ts) {
 this.ts = ts;
 }

@Override
 public String toString() {
 return "Log{" +
 "id=" + id +
 ", descriptor='" + descriptor + '\'' +
 ", ts=" + ts +
 '}';
 }
}

LogDao.java


package principles;

/**
 * LogDao
 *
 * @author david.king
 */
public interface LogDao {
 public Integer save(Log log);
}

LogService.java


package principles;

/**
 * LogService
 *
 * @author david.king
 */
public interface LogService {
 public Integer save(Log log);
}

LogDaoImpl.java


package principles;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

/**
 * LogDaoImpl
 *
 * @author david.king
 */
@Repository("logDao")
public class LogDaoImpl extends HibernateDaoSupport implements LogDao {
 @Override
 public Integer save(Log log) {
 return (Integer)getHibernateTemplate().save(log);
 }
}

LogServiceImpl.java


package principles;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * LogServiceImpl
 *
 * @author david.king
 */
@Service("logService")
public class LogServiceImpl implements LogService {
 @Autowired
 LogDao logDao;

@Override
 public Integer save(Log log) {
 return logDao.save(log);
 }
}

dataSource.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:jms="http://www.springframework.org/schema/jms"
 xmlns:amq="http://activemq.apache.org/schema/core"
 xmlns:jee="http://www.springframework.org/schema/jee"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
 http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.2.xsd
 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
 http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
 <property name="location">
 <value>principles/datasource.properties</value>
 </property>
 </bean>

<bean id="dataSource"
 class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName" value="${jdbc.driverClassName}" />
 <property name="url" value="${jdbc.url}" />
 <property name="username" value="${jdbc.username}" />
 <property name="password" value="${jdbc.password}" />
 </bean>

<!-- Hibernate session factory -->
 <bean id="sessionFactory"
 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource">
 <ref bean="dataSource"/>
 </property>

<property name="hibernateProperties">
 <props>
 <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
 <prop key="hibernate.show_sql">true</prop>
 </props>
 </property>

<property name="mappingResources">
 <list>
 <value>/principles/Log.hbm.xml</value>
 </list>
 </property>

</bean>

<bean id="logDao" class="principles.LogDaoImpl">
 <property name="sessionFactory" ref="sessionFactory" />
 </bean>
</beans>

datasource.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/log
jdbc.username=davidking
jdbc.password=davidking

applicationContext.xml


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:p="http://www.springframework.org/schema/p"
 xmlns:jms="http://www.springframework.org/schema/jms"
 xmlns:amq="http://activemq.apache.org/schema/core"
 xmlns:jee="http://www.springframework.org/schema/jee"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
 http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.2.xsd
 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
 http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">

<context:annotation-config />

<context:component-scan base-package="principles" />

<import resource="principles/dataSource.xml" />
</beans>

LogTest.java


package principles;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.sql.Timestamp;

import static org.junit.Assert.assertNotNull;

/**
 * LogTest
 *
 * @author david.king
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext.xml")
public class LogTest {
 private Logger logger = LoggerFactory.getLogger(LogTest.class);
 private SessionFactory sessionFactory;
 @Autowired
 private ApplicationContext applicationContext;
 @Autowired
 private LogService logService;

@Test
 public void testApplicationContext() {
 assertNotNull("ApplicationContext was null", applicationContext);
 System.out.format("ApplicationContext instance -> %s", applicationContext);
 }

@Test
 public void testCreateLogEntry() {
 Log log = new Log();
 log.setDescriptor("Document creation.");
 log.setTs(new Timestamp(System.currentTimeMillis()));

Integer savedLog = logService.save(log);

System.out.format("Log saved: %s", savedLog);
 }

@Before
 public void setUp() {
 sessionFactory = HibernateUtil.getSessionFactory();
 }

@After
 public void tearDown() {
 sessionFactory = null;
 }
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s