Code, JavaEE

Log – Hibernate Classic

Create a database named log as the root user and a grant all for user davidking with password davidking, then create the table log.



use log;

grant all on log.* to 'davidking'@'localhost' identified by 'davidking';

'CREATE TABLE `log` (\n `id` int(11) NOT NULL AUTO_INCREMENT,\n `descriptor` varchar(200) NOT NULL,\n `ts` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`)\n) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8'



<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"

 <!-- Database connection settings -->
 <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
 <property name="connection.url">jdbc:mysql://localhost:3306/log</property>
 <property name="connection.username">davidking</property>
 <property name="connection.password">davidking</property>

<!-- JDBC connection pool (use the built-in) -->
 <property name="connection.pool_size">1</property>

<!-- SQL dialect -->
 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

<!-- Enable Hibernate's automatic session context management -->
 <property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
 <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
 <property name="show_sql">false</property>

<property name="">validate</property>

<mapping resource="principles/Log.hbm.xml"/>


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"

<hibernate-mapping package="principles">

<class name="Log" table="log">
 <id name="id" column="ID">
 <generator class="native"/>
 <property name="descriptor" />
 <property name="ts" type="timestamp" />


package principles;

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) { = 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;

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

package principles;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

 * HibernateUtil
 * @author david.king
public class HibernateUtil {
 private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
 try {
 // Create the SessionFactory from hibernate.cfg.xml
 return new Configuration()
 } catch (Throwable ex) {
 System.err.println("Initial SessionFactory creation failed." + ex);
 throw new ExceptionInInitializerError(ex);

public static SessionFactory getSessionFactory() {
 return sessionFactory;

package principles;

import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;

import static org.junit.Assert.assertNotNull;

 * LogTest
 * @author david.king
public class LogTest {
 private Logger logger = LoggerFactory.getLogger(LogTest.class);
 private SessionFactory sessionFactory;

 public void testCreateLogEntry() {
 Session session = sessionFactory.openSession();

Log log = new Log();
 log.setDescriptor("Document creation.");
 log.setTs(new Timestamp(System.currentTimeMillis()));

Integer id = (Integer);

assertNotNull("Log id returned was null.", id);

 session.close();"Log saved: {}", log);

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

 public void tearDown() {
 sessionFactory = null;


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s