<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Source Allies Blog &#187; hibernate</title>
	<atom:link href="http://blogs.sourceallies.com/tag/hibernate/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.sourceallies.com</link>
	<description>Technical and process thinking from Source Allies employees</description>
	<lastBuildDate>Fri, 03 Feb 2012 17:45:38 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Hibernate Date vs Timestamp</title>
		<link>http://blogs.sourceallies.com/2012/02/hibernate-date-vs-timestamp/</link>
		<comments>http://blogs.sourceallies.com/2012/02/hibernate-date-vs-timestamp/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 20:24:21 +0000</pubDate>
		<dc:creator>David Kessler</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[date]]></category>
		<category><![CDATA[equals]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[timestamp]]></category>
		<category><![CDATA[UserType]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=2559</guid>
		<description><![CDATA[I encountered a subtle hibernate mapping issue involving Dates and Timestamps.  The following test recreates this issue.


package com.sourceallies.logging;
&#160;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
&#160;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
&#160;
import javax.annotation.Resource;
&#160;
import org.apache.commons.lang.time.DateUtils;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.classic.Session;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
&#160;
import com.sourceallies.Person;
&#160;
@RunWith&#40;SpringJUnit4ClassRunner.class&#41;
@ContextConfiguration
@TransactionConfiguration&#40;defaultRollback=false&#41;
public class HibernateDateTimeTest &#123;
&#160;
	@Resource
	private SessionFactory sessionFactory;
&#160;
	private Session currentSession;
&#160;
	private Date birthDateWithTime;
	private Date birthDateWithoutTime;
	private Date createDate;
	private [...]]]></description>
			<content:encoded><![CDATA[<p>I encountered a subtle hibernate mapping issue involving <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Date.html">Dates</a> and <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/Timestamp.html">Timestamps</a>.  The following test recreates this issue.<br />
<span id="more-2559"></span></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.logging</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertEquals</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertFalse</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertTrue</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Timestamp</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Calendar</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.annotation.Resource</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.lang.time.DateUtils</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.Criteria</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.SessionFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.classic.Session</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Before</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Test</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.runner.RunWith</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.test.context.ContextConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.test.context.junit4.SpringJUnit4ClassRunner</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.test.context.transaction.TransactionConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.transaction.annotation.Transactional</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sourceallies.Person</span><span style="color: #339933;">;</span>
&nbsp;
@RunWith<span style="color: #009900;">&#40;</span>SpringJUnit4ClassRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
@ContextConfiguration
@TransactionConfiguration<span style="color: #009900;">&#40;</span>defaultRollback<span style="color: #339933;">=</span><span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HibernateDateTimeTest <span style="color: #009900;">&#123;</span>
&nbsp;
	@Resource
	<span style="color: #000000; font-weight: bold;">private</span> SessionFactory sessionFactory<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> Session currentSession<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDateWithTime<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDateWithoutTime<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> createDate<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> modifyDate<span style="color: #339933;">;</span>
&nbsp;
	@Before
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Exception</span><span style="color: #009900;">&#123;</span>
		currentSession <span style="color: #339933;">=</span> sessionFactory.<span style="color: #006633;">getCurrentSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		birthDateWithTime <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		birthDateWithoutTime <span style="color: #339933;">=</span> DateUtils.<span style="color: #006633;">truncate</span><span style="color: #009900;">&#40;</span>birthDateWithTime, <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">DATE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		createDate <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		modifyDate <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testSave<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		Person person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testFirst&quot;</span>, <span style="color: #0000ff;">&quot;testLast&quot;</span>, 
                      birthDateWithTime, createDate, modifyDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		saveOrUpdate<span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testFind<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, people.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Person foundPerson <span style="color: #339933;">=</span> people.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>birthDateWithTime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>birthDateWithoutTime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>createDate<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>modifyDate<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> saveOrUpdate<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		currentSession.<span style="color: #006633;">saveOrUpdate</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		currentSession.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Criteria criteria <span style="color: #339933;">=</span> currentSession.<span style="color: #006633;">createCriteria</span><span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> criteria.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> people<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here is the mapping for person.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">...
@<span style="color: #003399;">Entity</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
&nbsp;
	@Id
	@GeneratedValue<span style="color: #009900;">&#40;</span>strategy<span style="color: #339933;">=</span>GenerationType.<span style="color: #006633;">AUTO</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> id <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>1L<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> firstName<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> lastName<span style="color: #339933;">;</span>
&nbsp;
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;timestamp&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> createDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> modifyDate<span style="color: #339933;">;</span>
	...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There are three <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Date.html">Date</a> fields: &#8216;birthdate&#8217;, &#8216;createDate&#8217;, and &#8216;modifyDate&#8217;.  The fields &#8216;birthDate&#8217; and &#8216;createDate&#8217; have a specified <a href="http://docs.jboss.org/hibernate/core/3.5/javadoc/org/hibernate/annotations/Type.html">Type</a>.  The field &#8216;modifyDate&#8217; however, does not have a specified Type.  The generated SQL from Hibernate is as follows.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> Person <span style="color: #66cc66;">&#40;</span>id bigint generated <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">AS</span> identity <span style="color: #66cc66;">&#40;</span>start <span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> 
birthDate date<span style="color: #66cc66;">,</span> createDate timestamp<span style="color: #66cc66;">,</span> firstName varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> 
lastName varchar<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">255</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">,</span> modifyDate timestamp<span style="color: #66cc66;">,</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span>id<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>We see that Hibernate created &#8216;birthDate&#8217; as a &#8216;Date&#8217; and the other two were created as a &#8216;Timestamp&#8217;.  The primary difference between &#8216;Date&#8217; and &#8216;Timestamp&#8217; in SQL is that &#8216;Timestamp&#8217; holds the date and time while the &#8216;Date&#8217; only holds the date.</p>
<p>At first glance this is not a big deal, but let&#8217;s take a closer look at the test.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testSave<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		Person person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testFirst&quot;</span>, <span style="color: #0000ff;">&quot;testLast&quot;</span>, 
                           birthDateWithTime, createDate, modifyDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>person.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		saveOrUpdate<span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Person is created with three <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Date.html">Dates</a> that all include date and time values.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testFind<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, people.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		Person foundPerson <span style="color: #339933;">=</span> people.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>birthDateWithTime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertTrue<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getBirthDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>birthDateWithoutTime<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getCreateDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>createDate<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		assertFalse<span style="color: #009900;">&#40;</span>foundPerson.<span style="color: #006633;">getModifyDate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>modifyDate<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>When Hibernate retrieves a Person from the database the &#8216;createDate&#8217; and &#8216;modifyDate&#8217; have been converted to <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/Timestamp.html">Timestamps</a>.  At first glance this doesn&#8217;t appear to be a big deal.  <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/Timestamp.html">java.sql.Timestamp</a> extends <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Date.html">java.util.Date</a>.  But why are they not equal.  I found a helpful answer to this question in <a href="http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683">Effective Java (2nd Edition)</a> page 41.</p>
<p><em><br />
There are some classes in the Java platform libraries that do extend an instantiable<br />
class and add a value component. For example, java.sql.Timestamp<br />
extends java.util.Date and adds a nanoseconds field. The equals implementation<br />
for Timestamp does violate symmetry and can cause erratic behavior if<br />
Timestamp and Date objects are used in the same collection or are otherwise intermixed.<br />
The Timestamp class has a disclaimer cautioning programmers against<br />
mixing dates and timestamps. While you won’t get into trouble as long as you<br />
keep them separate, there’s nothing to prevent you from mixing them, and the<br />
resulting errors can be hard to debug. This behavior of the Timestamp class was a<br />
mistake and should not be emulated. (Bloch, Effective Java, 2nd Ed.)<br />
</em></p>
<p>If you want to read more about &#8216;equals&#8217; read chapter 3, Item 8 in <a href="http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683">Effective Java (2nd Edition)</a>.  Apart from a hack there are two primary ways to solve this issue.  The first way involves typing &#8216;createDate&#8217; and &#8216;modifyDate&#8217; to <a href="http://docs.oracle.com/javase/6/docs/api/java/sql/Timestamp.html">Timestamp</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	...
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Timestamp</span> createDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Timestamp</span> modifyDate<span style="color: #339933;">;</span>
        ...</pre></div></div>

<p>The second approach uses a custom <a href="http://docs.jboss.org/hibernate/orm/3.5/api/org/hibernate/usertype/UserType.html">UserType</a>.  Here is a custom UserType that converts Timestamp to Date and vise versa.  There is a side effect with this solution.  Converting Timestamp to Date drops the nanosecond precision.  If this is not acceptable then the previous solution should be used.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Serializable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.PreparedStatement</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.ResultSet</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.SQLException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Timestamp</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.sql.Types</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.HibernateException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.usertype.UserType</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DateTimeUserType  <span style="color: #000000; font-weight: bold;">implements</span> UserType <span style="color: #009900;">&#123;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> sqlTypes<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		 <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #003399;">Types</span>.<span style="color: #006633;">TIMESTAMP</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;rawtypes&quot;</span><span style="color: #009900;">&#41;</span>
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">Class</span> returnedClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">Date</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> equals<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> x, <span style="color: #003399;">Object</span> y<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		 <span style="color: #000000; font-weight: bold;">return</span> x <span style="color: #339933;">==</span> y <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>x <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> y <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> x.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span>y<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> hashCode<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> x<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> x.<span style="color: #006633;">hashCode</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> nullSafeGet<span style="color: #009900;">&#40;</span><span style="color: #003399;">ResultSet</span> rs, <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> names, <span style="color: #003399;">Object</span> owner<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">throws</span> HibernateException, <span style="color: #003399;">SQLException</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">Timestamp</span> timestamp <span style="color: #339933;">=</span> rs.<span style="color: #006633;">getTimestamp</span><span style="color: #009900;">&#40;</span>names<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>rs.<span style="color: #006633;">wasNull</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span>timestamp.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> nullSafeSet<span style="color: #009900;">&#40;</span><span style="color: #003399;">PreparedStatement</span> st, <span style="color: #003399;">Object</span> value, <span style="color: #000066; font-weight: bold;">int</span> index<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">throws</span> HibernateException, <span style="color: #003399;">SQLException</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>value <span style="color: #339933;">==</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            st.<span style="color: #006633;">setNull</span><span style="color: #009900;">&#40;</span>index, <span style="color: #003399;">Types</span>.<span style="color: #006633;">TIMESTAMP</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">Date</span> date <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Date</span><span style="color: #009900;">&#41;</span> value<span style="color: #339933;">;</span>
            <span style="color: #003399;">Timestamp</span> timestamp <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Timestamp</span><span style="color: #009900;">&#40;</span>date.<span style="color: #006633;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            st.<span style="color: #006633;">setTimestamp</span><span style="color: #009900;">&#40;</span>index, timestamp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> deepCopy<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> value<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> value<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> isMutable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Serializable</span> disassemble<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> value<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		 <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Serializable</span><span style="color: #009900;">&#41;</span> value<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> assemble<span style="color: #009900;">&#40;</span><span style="color: #003399;">Serializable</span> cached, <span style="color: #003399;">Object</span> owner<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> cached<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Object</span> replace<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> original, <span style="color: #003399;">Object</span> target, <span style="color: #003399;">Object</span> owner<span style="color: #009900;">&#41;</span>
			<span style="color: #000000; font-weight: bold;">throws</span> HibernateException <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> original<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here is the way to use it in the Person class.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@<span style="color: #003399;">Entity</span>
@TypeDefs<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		@TypeDef<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dateTimeUserType&quot;</span>, typeClass<span style="color: #339933;">=</span>DateTimeUserType.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
	...
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dateTimeUserType&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> createDate<span style="color: #339933;">;</span>
&nbsp;
	@Column
	@Type<span style="color: #009900;">&#40;</span>type<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;dateTimeUserType&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> modifyDate<span style="color: #339933;">;</span>
        ...</pre></div></div>

<p>Either of these solutions will prevent the &#8216;equals&#8217; issue that we encountered before.  This issue is not a Hibernate issue it is truly a Java issue as is stated in <a href="http://www.amazon.com/Effective-Java-2nd-Joshua-Bloch/dp/0321356683">Effective Java (2nd Edition)</a>.  Hibernate can be challenging enough without these subtle issues that are easy to miss until a bug is reported.</p>
<p>Here is the complete code. <a href="http://blogs.sourceallies.com/wp-content/uploads/2012/02/HibernateDateTime3.zip" > Hibernate Logging Zip </a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2012/02/hibernate-date-vs-timestamp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hibernate Logging</title>
		<link>http://blogs.sourceallies.com/2012/01/hibernate-logging/</link>
		<comments>http://blogs.sourceallies.com/2012/01/hibernate-logging/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 14:54:22 +0000</pubDate>
		<dc:creator>David Kessler</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[JUnit]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[interceptor]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=2541</guid>
		<description><![CDATA[Through the years I&#8217;ve encountered a recurring requirement.  Clients need to log changes to the database for auditing and legal purposes.  To satisfy this requirement you could add logging to every save/update/delete call in your code.  Or better yet, you could create an aspect that wraps these calls.  While these would [...]]]></description>
			<content:encoded><![CDATA[<p>Through the years I&#8217;ve encountered a recurring requirement.  Clients need to log changes to the database for auditing and legal purposes.  To satisfy this requirement you could add logging to every save/update/delete call in your code.  Or better yet, you could create an <a href="http://goo.gl/DNo6a">aspect</a> that wraps these calls.  While these would certainly work Hibernate provides a convenient <a href="http://goo.gl/K0pJr">interceptor</a>.</p>
<p>In this article I will show you how to add a simple logger to Hibernate.<br />
<span id="more-2541"></span></p>
<h2>Start with a Test</h2>
<p>As an avid <a href="http://goo.gl/1KEI0">TDD </a> practitioner I will of coarse start with a test.  Here is the final version of the test that I used to drive my code.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.logging</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">junit</span>.<span style="color: #000000; font-weight: bold;">Assert</span>.<span style="color: #006633;">assertEquals</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #006633;">mock</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #006633;">verify</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">mockito</span>.<span style="color: #006633;">Mockito</span>.<span style="color: #006633;">verifyNoMoreInteractions</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.List</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.annotation.Resource</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.logging.Log</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.Criteria</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.SessionFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.classic.Session</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Before</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.Test</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.junit.runner.RunWith</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.test.context.ContextConfiguration</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.test.context.junit4.SpringJUnit4ClassRunner</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.transaction.annotation.Transactional</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.sourceallies.Person</span><span style="color: #339933;">;</span>
&nbsp;
@RunWith<span style="color: #009900;">&#40;</span>SpringJUnit4ClassRunner.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
@ContextConfiguration
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HibernateInterceptorTest <span style="color: #009900;">&#123;</span>
&nbsp;
	@Resource
	<span style="color: #000000; font-weight: bold;">private</span> SessionFactory sessionFactory<span style="color: #339933;">;</span>
&nbsp;
	@Resource
	<span style="color: #000000; font-weight: bold;">private</span> HibernateInterceptor hibernateInterceptor<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> Session currentSession<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> Log logger<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Date</span> NOW <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@Before
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		logger <span style="color: #339933;">=</span> mock<span style="color: #009900;">&#40;</span>Log.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		currentSession <span style="color: #339933;">=</span> sessionFactory.<span style="color: #006633;">getCurrentSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		hibernateInterceptor.<span style="color: #006633;">setLogger</span><span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testSaveLogging<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		saveOrUpdate<span style="color: #009900;">&#40;</span>createPerson<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		verify<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Saving Person [firstName=testFirst, lastName=testLast, birthDate=&quot;</span> <span style="color: #339933;">+</span> NOW <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;].&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testUpdateLogging<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		saveOrUpdate<span style="color: #009900;">&#40;</span>createPerson<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, people.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		Person firstPerson <span style="color: #339933;">=</span> people.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		firstPerson.<span style="color: #006633;">setFirstName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testFirst2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		saveOrUpdate<span style="color: #009900;">&#40;</span>firstPerson<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		currentSession.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		verify<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Saving Person [firstName=testFirst, lastName=testLast, birthDate=&quot;</span> <span style="color: #339933;">+</span> NOW <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;].&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		verify<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Updating Person [firstName=testFirst2, lastName=testLast, birthDate=&quot;</span> <span style="color: #339933;">+</span> NOW <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;].&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Test
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> testDeleteLogging<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		saveOrUpdate<span style="color: #009900;">&#40;</span>createPerson<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		assertEquals<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, people.<span style="color: #006633;">size</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		Person firstPerson <span style="color: #339933;">=</span> people.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		delete<span style="color: #009900;">&#40;</span>firstPerson<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		currentSession.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		verify<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Saving Person [firstName=testFirst, lastName=testLast, birthDate=&quot;</span> <span style="color: #339933;">+</span> NOW <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;].&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		verify<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Deleting Person [firstName=testFirst, lastName=testLast, birthDate=&quot;</span> <span style="color: #339933;">+</span> NOW <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;].&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
		verifyNoMoreInteractions<span style="color: #009900;">&#40;</span>logger<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> saveOrUpdate<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		currentSession.<span style="color: #006633;">saveOrUpdate</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		currentSession.<span style="color: #006633;">flush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Transactional
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> delete<span style="color: #009900;">&#40;</span>Person person<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		currentSession.<span style="color: #006633;">delete</span><span style="color: #009900;">&#40;</span>person<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unchecked&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> findAll<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Criteria criteria <span style="color: #339933;">=</span> currentSession.<span style="color: #006633;">createCriteria</span><span style="color: #009900;">&#40;</span>Person.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		List<span style="color: #339933;">&lt;</span>Person<span style="color: #339933;">&gt;</span> people <span style="color: #339933;">=</span> criteria.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> people<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> Person createPerson<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		Person person <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Person<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;testFirst&quot;</span>, <span style="color: #0000ff;">&quot;testLast&quot;</span>, NOW<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> person<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I used a mock logger to gather information from the interceptor.</p>
<h2>Spring Configuration</h2>
<p>Here is the configuration required for the interceptor.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
                ...
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;dataSource&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;entityInterceptor&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;hibernateInterceptor&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;transactionManager&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.orm.hibernate3.HibernateTransactionManager&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;sessionFactory&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
         ...	
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context:component-scan</span> <span style="color: #000066;">base-package</span>=<span style="color: #ff0000;">&quot;com.sourceallies&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        ...</pre></div></div>

<p>The &#8216;entityInterceptor&#8217; property on the &#8217;sessionFactory&#8217; takes a Hibernate interceptor.</p>
<h2>Entity</h2>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@<span style="color: #003399;">Entity</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #009900;">&#123;</span>
&nbsp;
	@Id
	@GeneratedValue<span style="color: #009900;">&#40;</span>strategy<span style="color: #339933;">=</span>GenerationType.<span style="color: #006633;">AUTO</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> id <span style="color: #339933;">=</span> <span style="color: #339933;">-</span>1L<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> firstName<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> lastName<span style="color: #339933;">;</span>
&nbsp;
	@Column
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> birthDate<span style="color: #339933;">;</span>
...
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> toString<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #0000ff;">&quot;Person [firstName=&quot;</span> <span style="color: #339933;">+</span> firstName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, lastName=&quot;</span>
				<span style="color: #339933;">+</span> lastName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, birthDate=&quot;</span> <span style="color: #339933;">+</span> birthDate <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;]&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>For this example you need to implement the &#8216;toString&#8217; method on the entity being logged.</p>
<h2>Interceptor</h2>
<p>Here is the interceptor code.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.sourceallies.logging</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Serializable</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.logging.Log</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.apache.commons.logging.LogFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.EmptyInterceptor</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.type.Type</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.springframework.stereotype.Component</span><span style="color: #339933;">;</span>
&nbsp;
@<span style="color: #003399;">Component</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HibernateInterceptor <span style="color: #000000; font-weight: bold;">extends</span> EmptyInterceptor <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> serialVersionUID <span style="color: #339933;">=</span> 1L<span style="color: #339933;">;</span>
	<span style="color: #000000; font-weight: bold;">private</span> Log logger <span style="color: #339933;">=</span> LogFactory.<span style="color: #006633;">getLog</span><span style="color: #009900;">&#40;</span>HibernateInterceptor.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onDelete<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> entity, <span style="color: #003399;">Serializable</span> id, <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> state,
			<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> propertyNames, Type<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> types<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		log<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Deleting&quot;</span>, id, entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onDelete</span><span style="color: #009900;">&#40;</span>entity, id, state, propertyNames, types<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onFlushDirty<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> entity, <span style="color: #003399;">Serializable</span> id,
			<span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> currentState, <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> previousState,
			<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> propertyNames, Type<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> types<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		log<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Updating&quot;</span>, id, entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onFlushDirty</span><span style="color: #009900;">&#40;</span>entity, id, currentState, previousState,
				propertyNames, types<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	@Override
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">boolean</span> onSave<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> entity, <span style="color: #003399;">Serializable</span> id, <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> state,
			<span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> propertyNames, Type<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> types<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		log<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Saving&quot;</span>, id, entity<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">onSave</span><span style="color: #009900;">&#40;</span>entity, id, state, propertyNames, types<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> setLogger<span style="color: #009900;">&#40;</span>Log logger<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">logger</span> <span style="color: #339933;">=</span> logger<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> log<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> action, <span style="color: #003399;">Serializable</span> id, <span style="color: #003399;">Object</span> entity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		logger.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span>action <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">+</span> entity <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>While the &#8216;onSave&#8217; and &#8216;onDelete&#8217; methods are clearly named the &#8216;onUpdate&#8217; method appears to be missing.  With a little research it quickly becomes obvious that &#8216;onFlushDirty&#8217; is used for updates.</p>
<p>Logging database activity is this simple.</p>
<p>Here is the complete code. <a href="http://blogs.sourceallies.com/wp-content/uploads/2012/01/HibernateLogging.zip" > Hibernate Logging Zip </a></p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2012/01/hibernate-logging/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Hibernate Embeddable Objects</title>
		<link>http://blogs.sourceallies.com/2010/01/hibernate-embeddable-objects/</link>
		<comments>http://blogs.sourceallies.com/2010/01/hibernate-embeddable-objects/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 14:00:06 +0000</pubDate>
		<dc:creator>Max  Kuipers</dc:creator>
				<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Embeddable Objects]]></category>
		<category><![CDATA[hibernate]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=579</guid>
		<description><![CDATA[Hibernate Embeddable Objects are a really neat way to organize your data model.  Especially, if you have the same few columns in a number of different tables, that all pertain to the same thing. The example commonly used is Addresses.  You may have a number of tables that each have fields pertaining to address information [...]]]></description>
			<content:encoded><![CDATA[<p>Hibernate Embeddable Objects are a really neat way to organize your data model.  Especially, if you have the same few columns in a number of different tables, that all pertain to the same thing. The <a title="example commonly used" href="http://docs.jboss.org/hibernate/stable/annotations/reference/en/html_single/#d0e569">example commonly used </a>is Addresses.  You may have a number of tables that each have fields pertaining to address information and you don&#8217;t want to have to do all the mappings for each entity again and again.  If the column names are the same across each table, you can just add an @Embeddable annotation.<br />
<span id="more-579"></span><br />
Consider the following example:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Embeddable
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Address <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span> <span style="color: #009900;">&#123;</span>
...
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> lineOne<span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> lineTwo<span style="color: #339933;">;</span>
   <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> city<span style="color: #339933;">;</span>
....
   @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;LINEONE&quot;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getLineOne<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">lineOne</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
...
   @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CITY&quot;</span><span style="color: #009900;">&#41;</span>
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getCity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">city</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
...</pre></div></div>

<p>This creates address as an embeddable object. Then to actually embed it in your entity, you simply do something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@<span style="color: #003399;">Entity</span>
@Table<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Person&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span><span style="color: #009900;">&#123;</span>
...
   <span style="color: #000000; font-weight: bold;">private</span> Address address<span style="color: #339933;">;</span>
...
   @Embedded
   <span style="color: #000000; font-weight: bold;">public</span> getAddress<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">address</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>There is, however, one huge problem with this, especially when working with JSF.  If all of the fields in the embeddable object are null for some reason or another, then the embeddable object never actually gets instantiated.  There is a solution/hack that works around this &#8211; you modify the setter method to look something like the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@<span style="color: #003399;">Entity</span>
@Table<span style="color: #009900;">&#40;</span>name<span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Person&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Person <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span><span style="color: #009900;">&#123;</span>
...
   <span style="color: #000000; font-weight: bold;">private</span> Address address<span style="color: #339933;">;</span>
...
   <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setAddress<span style="color: #009900;">&#40;</span>Address address<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>address <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">address</span> <span style="color: #339933;">=</span> address<span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">address</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Address<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> 
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>It&#8217;s not particularly pretty, but it will dry your tears in a pinch. More on this solution can be found <a title="here" href="https://jira.jboss.org/jira/browse/HIBERNATE-50">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2010/01/hibernate-embeddable-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building Multi-Criteria Search Queries in Hibernate</title>
		<link>http://blogs.sourceallies.com/2009/12/building-multi-criteria-search-queries-in-hibernate/</link>
		<comments>http://blogs.sourceallies.com/2009/12/building-multi-criteria-search-queries-in-hibernate/#comments</comments>
		<pubDate>Wed, 09 Dec 2009 18:02:53 +0000</pubDate>
		<dc:creator>Vaithi  Subramanian</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[Hql]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=399</guid>
		<description><![CDATA[
In this post I am going to show how to write queries multi-criteria search screens. There are two approaches for making this possible.


HQL for building the Query
Building Query using Criteria API

HQL for building the Query
Here I am going to show 2 approaches to building the HQL and try to point out the better approach.
Approach I:String [...]]]></description>
			<content:encoded><![CDATA[<p>
In this post I am going to show how to write queries multi-criteria search screens. There are two approaches for making this possible.
</p>
<ul>
<li>HQL for building the Query</li>
<li>Building Query using Criteria API</li>
</ul>
<p><b>HQL for building the Query</b></p>
<p>Here I am going to show 2 approaches to building the HQL and try to point out the better approach.</p>
<p><strong>Approach I:String concatenation</strong><br />
This approach uses String concatenation and setting up the values directly in the query.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>startDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>firstClause<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      query <span style="color: #339933;">=</span> query <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; where &quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
      query <span style="color: #339933;">=</span> query <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; and &quot;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   query <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot; s.date &gt;= '&quot;</span> <span style="color: #339933;">+</span> startDate <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Using the above approach  there might be a chance of SQL Injection attack and using string concatenation is inherently error-prone.</p>
<p><strong>Approach 2: Criteria as Named Parameters</strong></p>
<p>In this one we create two map to hold parameter name and value which could be binded to the HQL during the execution.</p>
<p>Here is brief example of the approach.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">List</span> search<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   StringBuilder aQuery <span style="color: #339933;">=</span> 
      <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; from document p where p.id is not null &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003399;">HashMap</span> parameterMap <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #003399;">HashMap</span> parameterListMap <span style="color: #339933;">=</span>  <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">HashMap</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//Collection Criteria</span>
   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>countyList <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      buildCollectionCriterion<span style="color: #009900;">&#40;</span>aQuery, parameterListMap, <span style="color: #0000ff;">&quot;listID&quot;</span>, countyList<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #666666; font-style: italic;">//Date Criteria</span>
   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>startDate<span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">||</span> endDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      buildDateCriterion<span style="color: #009900;">&#40;</span>aQuery, parameterMap, <span style="color: #0000ff;">&quot;dateField&quot;</span>,startDate, endDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   Query query <span style="color: #339933;">=</span> hibSession.<span style="color: #006633;">createQuery</span><span style="color: #009900;">&#40;</span>aQuery.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key <span style="color: #339933;">:</span> parameterMap.<span style="color: #006633;">keySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      query.<span style="color: #006633;">setParameter</span><span style="color: #009900;">&#40;</span>key, parameterMap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key <span style="color: #339933;">:</span> parameterListMap.<span style="color: #006633;">keySet</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      query.<span style="color: #006633;">setParameterList</span><span style="color: #009900;">&#40;</span>key, parameterListMap.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
&nbsp;
   <span style="color: #003399;">List</span> results <span style="color: #339933;">=</span> query.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//Helper Methods for different type of criteria</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> buildCollectionCriterion<span style="color: #009900;">&#40;</span>StringBuilder aQuery, 
                                        <span style="color: #003399;">Map</span> parameterListMap, 
                                        <span style="color: #003399;">String</span> aFieldName, 
                                        <span style="color: #003399;">Collection</span> aList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>aList <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span>aList.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      aQuery
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; and p.&quot;</span><span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>aFieldName<span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; in (:&quot;</span><span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>aFieldName<span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      parameterListMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span>aFieldName, aList<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> buildDateCriterion<span style="color: #009900;">&#40;</span>StringBuilder aQuery, 
                               <span style="color: #003399;">Map</span> parameterMap, 
                               <span style="color: #003399;">String</span> aFieldName, 
                               <span style="color: #003399;">Date</span> aStartDate, 
                               <span style="color: #003399;">Date</span> anEndDate<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>aStartDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> anEndDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      aQuery
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; and ( p.&quot;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>aFieldName<span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; between :aStartDate and :anEndDate)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      parameterMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;aStartDate&quot;</span>, aStartDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      parameterMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;anEndDate&quot;</span>, anEndDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
   <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>aStartDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      aQuery
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; and  (p.&quot;</span><span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>aFieldName<span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &gt;= :aStartDate)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      parameterMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;aStartDate&quot;</span>, aStartDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span> 
   <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>anEndDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      aQuery
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; and (p.&quot;</span><span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>aFieldName<span style="color: #009900;">&#41;</span>
         .<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &lt;=:anEndDate&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      parameterMap.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;anEndDate&quot;</span>, anEndDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><b>Building Query using Criteria API</b></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">List</span> search<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        Criteria c <span style="color: #339933;">=</span> hibSession.<span style="color: #006633;">createCriteria</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Document</span>.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        c.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>Restrictions.<span style="color: #006633;">notNull</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>countryList <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                c.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>Restrictions.<span style="color: #006633;">in</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;listId&quot;</span>, countryList<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>startDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                c.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>Restrictions.<span style="color: #006633;">ge</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dateField&quot;</span>, startDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>endDate <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                c.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>Restrictions.<span style="color: #006633;">le</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dateField&quot;</span>, endDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> c.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>Conclusion</strong></p>
<p>The Hibernate Criteria API is a powerful and elegant library which is well adapted for implementing multi-criteria search functionality and also HQL queries must be built &#8216;on-the-fly&#8217;. Using it in appropriate circumstances will result in cleaner, clearer, more reliable and more maintainable code. </p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2009/12/building-multi-criteria-search-queries-in-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hibernate Criteria trick</title>
		<link>http://blogs.sourceallies.com/2009/10/hibernate-criteria-trick/</link>
		<comments>http://blogs.sourceallies.com/2009/10/hibernate-criteria-trick/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 14:32:55 +0000</pubDate>
		<dc:creator>Aaron King</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[criteria]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[not in]]></category>
		<category><![CDATA[restrictions]]></category>

		<guid isPermaLink="false">http://blogs.sourceallies.com/?p=61</guid>
		<description><![CDATA[So here&#8217;s the situation.
Let&#8217;s say I have this query here:

SELECT * FROM employees
WHERE employee_id NOT IN &#40; 1234 , 3456 , 5678 &#41;;

How do we do that with the Hibernate Criteria object with a Restriction?   You would think that the Restrictions API would have a &#8220;not in&#8221; method, since it does have a not [...]]]></description>
			<content:encoded><![CDATA[<p>So here&#8217;s the situation.</p>
<p>Let&#8217;s say I have this query here:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> <span style="color: #993333; font-weight: bold;">FROM</span> employees
<span style="color: #993333; font-weight: bold;">WHERE</span> employee_id <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">IN</span> <span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">1234</span> <span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">3456</span> <span style="color: #66cc66;">,</span> <span style="color: #cc66cc;">5678</span> <span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>How do we do that with the Hibernate Criteria object with a Restriction?   You would think that the Restrictions API would have a &#8220;not in&#8221; method, since it does have a not equals method(ne), but alas, there is nothing&#8230;</p>
<p>Well, here&#8217;s the solution:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">//Create the criteria</span>
Criteria crit <span style="color: #339933;">=</span> factory.<span style="color: #006633;">getCurrentSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">createCriteria</span><span style="color: #009900;">&#40;</span>Employee.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//add my restriction where idList is a list of emp ids that need to be excluded</span>
crit.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>Restrictions.<span style="color: #006633;">not</span><span style="color: #009900;">&#40;</span>Restrictions.<span style="color: #006633;">in</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;employeeId&quot;</span>, idList<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//get some results</span>
<span style="color: #003399;">List</span> employees <span style="color: #339933;">=</span> crit.<span style="color: #006633;">list</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There you go!  Now you know this neat little trick and you can use it in your own app&#8230;  Be forewarned though, it can be slow&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://blogs.sourceallies.com/2009/10/hibernate-criteria-trick/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

