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.

SQL


CREATE DATABASE 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'

 

hibernate.cfg.xml


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

<hibernate-configuration>
 <session-factory>
 <!-- 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="hbm2ddl.auto">validate</property>

<mapping resource="principles/Log.hbm.xml"/>
 </session-factory>
</hibernate-configuration>

 Log.hbm.xml


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="principles">

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

</hibernate-mapping>

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 +
 '}';
 }
}

HibernateUtil.java


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()
 .configure()
 .buildSessionFactory();
 } catch (Throwable ex) {
 System.err.println("Initial SessionFactory creation failed." + ex);
 throw new ExceptionInInitializerError(ex);
 }
 }

public static SessionFactory getSessionFactory() {
 return sessionFactory;
 }
}

LogTest.java


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;

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

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

Integer id = (Integer)session.save(log);

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

session.getTransaction().commit();
 session.close();

logger.info("Log saved: {}", log);
 }

@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