<?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/"
	>

<channel>
	<title>Technical blog</title>
	<atom:link href="http://puretech.paawak.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://puretech.paawak.com</link>
	<description>My musings on software</description>
	<pubDate>Thu, 25 Nov 2010 20:52:25 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>[Example] Hibernate One-To-Many bidirectional relation</title>
		<link>http://puretech.paawak.com/2010/11/26/example-hibernate-one-to-many-bidirectional-relation/</link>
		<comments>http://puretech.paawak.com/2010/11/26/example-hibernate-one-to-many-bidirectional-relation/#comments</comments>
		<pubDate>Thu, 25 Nov 2010 20:29:13 +0000</pubDate>
		<dc:creator>paawak</dc:creator>
		
		<category><![CDATA[database]]></category>

		<category><![CDATA[hibernate]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[oracle]]></category>

		<guid isPermaLink="false">http://puretech.paawak.com/?p=408</guid>
		<description><![CDATA[Let us consider the relation between Semester and Subjects. A Semester has a collection of Subjects. Each Subject, in turn, has a reference to the Semester.
This is illustrated as below:

public class Semester &#123;
&#160;
    private Long semesterId;
&#160;
    private String semesterName;
&#160;
    private Set&#60;Subject&#62; subjects;
&#160;
...
&#160;
&#125;
&#160;
public class Subject &#123;
&#160;
  [...]]]></description>
			<content:encoded><![CDATA[<p>Let us consider the relation between Semester and Subjects. A Semester has a collection of Subjects. Each Subject, in turn, has a reference to the Semester.</p>
<p>This is illustrated as below:</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: #000000; font-weight: bold;">class</span> Semester <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> semesterId<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> semesterName<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> Set<span style="color: #339933;">&lt;</span>Subject<span style="color: #339933;">&gt;</span> subjects<span style="color: #339933;">;</span>
&nbsp;
...
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Subject <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> subjectId<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> subjectName<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> Semester semester<span style="color: #339933;">;</span>
...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is how I convert it to Hibernate entities:</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> Semester <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span> <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> <span style="color: #339933;">-</span>6067841723974478563L<span style="color: #339933;">;</span>
&nbsp;
    @Id
    @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SEMESTER_ID&quot;</span><span style="color: #009900;">&#41;</span>
    @SequenceGenerator<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;seq&quot;</span>, allocationSize <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>, initialValue <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>, sequenceName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SEQ_SEMESTER&quot;</span><span style="color: #009900;">&#41;</span>
    @GeneratedValue<span style="color: #009900;">&#40;</span>strategy <span style="color: #339933;">=</span> GenerationType.<span style="color: #006633;">SEQUENCE</span>, generator <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;seq&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> semesterId<span style="color: #339933;">;</span>
&nbsp;
    @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SEMESTER_NAME&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> semesterName<span style="color: #339933;">;</span>
&nbsp;
    @OneToMany<span style="color: #009900;">&#40;</span>mappedBy <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;semester&quot;</span>, cascade <span style="color: #339933;">=</span> CascadeType.<span style="color: #006633;">ALL</span>, fetch <span style="color: #339933;">=</span> FetchType.<span style="color: #006633;">EAGER</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Set<span style="color: #339933;">&lt;</span>Subject<span style="color: #339933;">&gt;</span> subjects<span style="color: #339933;">;</span>
&nbsp;
...
<span style="color: #009900;">&#125;</span>
&nbsp;
@<span style="color: #003399;">Entity</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Subject <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span> <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> 5705981176568667418L<span style="color: #339933;">;</span>
&nbsp;
    @Id
    @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SUBJECT_ID&quot;</span><span style="color: #009900;">&#41;</span>
    @SequenceGenerator<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;seq&quot;</span>, allocationSize <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>, initialValue <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>, sequenceName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SEQ_SUBJECT&quot;</span><span style="color: #009900;">&#41;</span>
    @GeneratedValue<span style="color: #009900;">&#40;</span>strategy <span style="color: #339933;">=</span> GenerationType.<span style="color: #006633;">SEQUENCE</span>, generator <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;seq&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> subjectId<span style="color: #339933;">;</span>
&nbsp;
    @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SUBJECT_NAME&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> subjectName<span style="color: #339933;">;</span>
&nbsp;
    @ManyToOne
    @JoinColumn<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SEMESTER_ID_FK&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> Semester semester<span style="color: #339933;">;</span>
&nbsp;
...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Further, I should be able to insert it with the following code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">        Set<span style="color: #339933;">&lt;</span>Subject<span style="color: #339933;">&gt;</span> subjects <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>Subject<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        Semester semester <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Semester<span style="color: #009900;">&#40;</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: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            Subject subject <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Subject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            subject.<span style="color: #006633;">setSubjectName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;subject_&quot;</span> <span style="color: #339933;">+</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            subject.<span style="color: #006633;">setSemester</span><span style="color: #009900;">&#40;</span>semester<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            subjects.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span>subject<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
        semester.<span style="color: #006633;">setSemesterName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;1st sem&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        semester.<span style="color: #006633;">setSubjects</span><span style="color: #009900;">&#40;</span>subjects<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        Transaction tr <span style="color: #339933;">=</span> session.<span style="color: #006633;">beginTransaction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        session.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>semester<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        tr.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The SQL script is:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> SEQUENCE  SEQ_SEMESTER
START <span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #cc66cc;">1</span>
INCREMENT <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #cc66cc;">1</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> SEMESTER <span style="color: #66cc66;">&#40;</span>
&nbsp;
	SEMESTER_ID NUMBER <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	SEMESTER_NAME VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
&nbsp;
<span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> SEQUENCE  SEQ_SUBJECT
START <span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #cc66cc;">1</span>
INCREMENT <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #cc66cc;">1</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> SUBJECT <span style="color: #66cc66;">&#40;</span>
&nbsp;
	SUBJECT_ID NUMBER <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	SUBJECT_NAME VARCHAR<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	SEMESTER_ID_FK NUMBER <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	CONSTRAINT CNSTR_SEMESTER_ID_FK <span style="color: #993333; font-weight: bold;">FOREIGN</span> <span style="color: #993333; font-weight: bold;">KEY</span><span style="color: #66cc66;">&#40;</span>SEMESTER_ID_FK<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">REFERENCES</span> SEMESTER<span style="color: #66cc66;">&#40;</span>SEMESTER_ID<span style="color: #66cc66;">&#41;</span>
&nbsp;
<span style="color: #66cc66;">&#41;</span>;</pre></div></div>

<p>The sources can be found <a href="http://puretech.paawak.com/resources/java/hibernate/OneToManyExample.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://puretech.paawak.com/2010/11/26/example-hibernate-one-to-many-bidirectional-relation/feed/</wfw:commentRss>
		</item>
		<item>
		<title>[How To] Handle Oracle TimeStamp with TimeZone from Java</title>
		<link>http://puretech.paawak.com/2010/11/02/how-to-handle-oracle-timestamp-with-timezone-from-java/</link>
		<comments>http://puretech.paawak.com/2010/11/02/how-to-handle-oracle-timestamp-with-timezone-from-java/#comments</comments>
		<pubDate>Mon, 01 Nov 2010 21:09:46 +0000</pubDate>
		<dc:creator>paawak</dc:creator>
		
		<category><![CDATA[database]]></category>

		<category><![CDATA[hibernate]]></category>

		<category><![CDATA[java]]></category>

		<category><![CDATA[oracle]]></category>

		<guid isPermaLink="false">http://puretech.paawak.com/?p=395</guid>
		<description><![CDATA[Oracle TimeStamp with TimeZone: A Critical Analysis
For storing the TimeZone information in TimeStamp, Oracle (10g) has two options:

TIMESTAMP WITH TIME ZONE data type:  As the name indicates, this stores the TimeZone data along with the TimeStamp. The TimeZone can be either stored in the UTC format (eaxmple: +05:30) or the TimeZoneRegion format (example: Asia/Calcutta or [...]]]></description>
			<content:encoded><![CDATA[<h2>Oracle TimeStamp with TimeZone: A Critical Analysis</h2>
<p>For storing the TimeZone information in TimeStamp, Oracle (10g) has two options:</p>
<ol>
<li><span style="color: #800000;">TIMESTAMP WITH TIME ZONE</span> data type:  As the name indicates, this stores the TimeZone data along with the TimeStamp. The TimeZone can be either stored in the UTC format (eaxmple: <em>+05:30</em>) or the TimeZoneRegion format (example: <em>Asia/Calcutta</em> or <em>Indian Standard Time</em>).</li>
<li><span style="color: #800000;">TIMESTAMP WITH LOCAL TIME ZONE</span> data type: This does not store the TimeZone information <em>per se</em>. While inserting data, it adjusts for the offset of the client&#8217;s TimeZone and stores it according to the db server&#8217;s local TimeZone, without the actual TimeZone information. Similarly, while querying, the TimeStamp will be offseted against the client&#8217;s TimeZone, without returning any explicit TimeZone information. This data type is very confusing and is to be avoided at all costs.</li>
</ol>
<p>As far as JDBC is concerned, things are plain and simple when we talk about Statement. The trouble starts the moment we think about PreparedStatement. Both these data types map to the <em>java.sql.Timestamp</em> in JDBC. But <em>java.sql.Timestamp</em> does not have any TimeZone information. In this scenario, we should be using the <a href="http://download.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#setTimestamp(int,%20java.sql.Timestamp,%20java.util.Calendar)" target="_blank">PreparedStatement.</a><a href="http://download.oracle.com/javase/6/docs/api/java/sql/PreparedStatement.html#setTimestamp(int,%20java.sql.Timestamp,%20java.util.Calendar)" target="_blank">setTimestamp(int parameterIndex, Timestamp x, Calendar cal)</a> to pass the TimeZone information to the db driver. There is another twist to this tale. The <span style="color: #800000;">TIMESTAMP WITH LOCAL TIME ZONE</span> data type needs to adjust the time against the client&#8217;s TimeZone. When we open a Oracle client session, the TimeZone is already set. But that is not the case with JDBC Connection. The TimeZone info is not set by the driver, for reasons best known to themselves. So a simple <a href="http://download.oracle.com/javase/6/docs/api/java/sql/ResultSet.html#getTimestamp(int)" target="_blank">ResultSet.getTimestamp(int columnIndex)</a> will not work. Instead, we have to use the <a href="http://download.oracle.com/javase/6/docs/api/java/sql/ResultSet.html#getTimestamp(int,%20java.util.Calendar)" target="_blank">ResultSet.getTimestamp(int columnIndex, Calendar cal)</a> to pass the TimeZone to the db driver.</p>
<h2>Simple JDBC example</h2>
<p>Consider the following table having both types of TimeStamp with TimeZone:</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> TIMESTAMP_DEMO <span style="color: #66cc66;">&#40;</span>
	ID                                                    NUMBER<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">6</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	NAME                                             VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	TIME_WITH_ZONE                     TIMESTAMP<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">WITH</span> TIME ZONE <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	TIME_WITH_ZONE_LOCAL      TIMESTAMP<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">9</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #993333; font-weight: bold;">LOCAL</span> TIME ZONE
    <span style="color: #66cc66;">&#41;</span> ;</pre></div></div>

<p>This is the SQL statement for inserting data:</p>
<blockquote><p>INSERT INTO TIMESTAMP_DEMO (ID, NAME, TIME_WITH_ZONE, TIME_WITH_ZONE_LOCAL) VALUES (0, &#8216;manualInsert&#8217;, TO_TIMESTAMP_TZ(&#8217;2010-09-26 11:30:00 Australia/Adelaide&#8217;,'YYYY-MM-DD HH24:MI:SS TZR&#8217;), TO_TIMESTAMP_TZ(&#8217;2010-09-26 11:30:00 Australia/Adelaide&#8217;,'YYYY-MM-DD HH24:MI:SS TZR&#8217;));</p></blockquote>
<p>We will try to insert data from plain JDBC and then read it back to ensure that the information persisted is correct.</p>
<h3>INSERT Approach 1</h3>
<p>Consider the following code snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">        <span style="color: #003399;">PreparedStatement</span> pStat <span style="color: #339933;">=</span> con
                .<span style="color: #006633;">prepareStatement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO TIMESTAMP_DEMO &quot;</span>
                        <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; (ID, NAME, TIME_WITH_ZONE, TIME_WITH_ZONE_LOCAL) &quot;</span>
                        <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; VALUES &quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; (?, ?, ?, ?)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>        
&nbsp;
        pStat.<span style="color: #006633;">setInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        pStat.<span style="color: #006633;">setString</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span>, <span style="color: #0000ff;">&quot;insert_1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">String</span> timeZoneId <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Australia/Adelaide&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">TimeZone</span> timeZone <span style="color: #339933;">=</span> <span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span>timeZoneId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">Calendar</span> timeWithZone <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span>timeZone<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        timeWithZone.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">HOUR_OF_DAY</span>, <span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        timeWithZone.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MINUTE</span>, <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        timeWithZone.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">SECOND</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        timeWithZone.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MILLISECOND</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">Timestamp</span> ts <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>timeWithZone.<span style="color: #006633;">getTimeInMillis</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;
        pStat.<span style="color: #006633;">setTimestamp</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span>, ts, timeWithZone<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        pStat.<span style="color: #006633;">setTimestamp</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span>, ts, timeWithZone<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        pStat.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        pStat.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now run the following query from any Oracle SQL Client:</p>
<blockquote><p>SELECT TO_CHAR(TIME_WITH_ZONE, &#8216;YYYY-MM-DD HH24:MI:SS:FF TZR&#8217;) AS TIME_WITH_ZONE FROM TIMESTAMP_DEMO;</p></blockquote>
<p>You will find that the returned value is:</p>
<blockquote><p>2010-11-02 11:30:00:000000000 +05:30</p></blockquote>
<p>Note that the TimeZone info is wrongly stored. The only conclusion that we can have is that the JDBC driver is buggy. How do we get around this problem?</p>
<h3>INSERT Approach 2</h3>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">        <span style="color: #003399;">String</span> timeZoneId <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Australia/Adelaide&quot;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">TimeZone</span> timeZone <span style="color: #339933;">=</span> <span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span>timeZoneId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">Calendar</span> timeWithZone <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span>timeZone<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        timeWithZone.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">HOUR_OF_DAY</span>, <span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        timeWithZone.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MINUTE</span>, <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        timeWithZone.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">SECOND</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        timeWithZone.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MILLISECOND</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">String</span> dateFormat <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;yyyy-MM-dd HH:mm:ss:SSS&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">DateFormat</span> df <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">SimpleDateFormat</span><span style="color: #009900;">&#40;</span>dateFormat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// this is very important</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>timeZone <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>
            df.<span style="color: #006633;">setTimeZone</span><span style="color: #009900;">&#40;</span>timeZone<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003399;">String</span> dateTime <span style="color: #339933;">=</span> df.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>timeWithZone.<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: #003399;">String</span> tzId <span style="color: #339933;">=</span> timeWithZone.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getID</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        dateTime <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">+</span> tzId<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">PreparedStatement</span> pStat <span style="color: #339933;">=</span> con
                .<span style="color: #006633;">prepareStatement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO TIMESTAMP_DEMO &quot;</span>
                        <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; (ID, NAME, TIME_WITH_ZONE, TIME_WITH_ZONE_LOCAL) &quot;</span>
                        <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; VALUES &quot;</span>
                        <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; (?, ?, TO_TIMESTAMP_TZ(?, 'YYYY-MM-DD HH24:MI:SS:FF TZR'), ?)&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        pStat.<span style="color: #006633;">setInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        pStat.<span style="color: #006633;">setString</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span>, <span style="color: #0000ff;">&quot;insert_2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">Timestamp</span> ts <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>timeWithZone.<span style="color: #006633;">getTimeInMillis</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;
        pStat.<span style="color: #006633;">setString</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span>, dateTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        pStat.<span style="color: #006633;">setTimestamp</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span>, ts, timeWithZone<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        pStat.<span style="color: #006633;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        pStat.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>On running the select statement, the data got back is:</p>
<blockquote><p>2010-11-02 11:30:00:000000000 AUSTRALIA/ADELAIDE</p></blockquote>
<p>This is precisely the data which was inserted. The trcik here is to bypass the JDBC data and use Oracle function <em>TO_TIMESTAMP_TZ(timeString, format)</em>.</p>
<h3>SELECT Approach 1</h3>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">        <span style="color: #003399;">Statement</span> stat <span style="color: #339933;">=</span> con.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">ResultSet</span> res <span style="color: #339933;">=</span> stat
                .<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM TIMESTAMP_DEMO  ORDER BY ID&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>res.<span style="color: #006633;">next</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>
&nbsp;
            <span style="color: #003399;">Timestamp</span> timestamp <span style="color: #339933;">=</span> res.<span style="color: #006633;">getTimestamp</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;TIME_WITH_ZONE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #003399;">Timestamp</span> timestampLocal <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
                timestampLocal <span style="color: #339933;">=</span> res.<span style="color: #006633;">getTimestamp</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;TIME_WITH_ZONE_LOCAL&quot;</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;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">SQLException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                e.<span style="color: #006633;">printStackTrace</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;">break</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;TIME=&quot;</span> <span style="color: #339933;">+</span> timestamp <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, TIME_LOCAL=&quot;</span>
                    <span style="color: #339933;">+</span> timestampLocal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
        stat.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        res.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>On running the above, we get the following exception:</p>
<blockquote><p><span style="color: #ff0000;">java.sql.SQLException: The database session time zone is not set<br />
at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:70)<br />
at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:110)<br />
</span></p></blockquote>
<p>This is got on the line</p>
<blockquote><p>res.getTimestamp(&#8221;TIME_WITH_ZONE_LOCAL&#8221;);</p></blockquote>
<p>as the TimeZone is not set in the JDBC Connection object.</p>
<h3>SELECT Approach 2</h3>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">        <span style="color: #003399;">Statement</span> stat <span style="color: #339933;">=</span> con.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">ResultSet</span> res <span style="color: #339933;">=</span> stat
                .<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT * FROM TIMESTAMP_DEMO  ORDER BY ID&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>res.<span style="color: #006633;">next</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>
&nbsp;
            <span style="color: #003399;">Timestamp</span> timestamp <span style="color: #339933;">=</span> res.<span style="color: #006633;">getTimestamp</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;TIME_WITH_ZONE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #003399;">Timestamp</span> timestampLocal <span style="color: #339933;">=</span> res.<span style="color: #006633;">getTimestamp</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;TIME_WITH_ZONE_LOCAL&quot;</span>,
                    <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">GregorianCalendar</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getDefault</span><span style="color: #009900;">&#40;</span><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: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>res.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;NAME&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;: &quot;</span>
                    <span style="color: #339933;">+</span> res.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ID&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, TIME=&quot;</span>
                    <span style="color: #339933;">+</span> getTimeWithZone<span style="color: #009900;">&#40;</span>timestamp<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, TIME_LOCAL=&quot;</span>
                    <span style="color: #339933;">+</span> getTimeWithZone<span style="color: #009900;">&#40;</span>timestampLocal<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
        stat.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        res.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>And this is how <em>getTimeWithZone()</em> looks like:</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: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> getTimeWithZone<span style="color: #009900;">&#40;</span><span style="color: #003399;">Timestamp</span> timestamp<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003399;">Calendar</span> cal <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">GregorianCalendar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        cal.<span style="color: #006633;">setTime</span><span style="color: #009900;">&#40;</span>timestamp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> getTimeWithZone<span style="color: #009900;">&#40;</span>cal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> getTimeWithZone<span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span> cal<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003399;">String</span> dateFormat <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;HH:mm:ss:SSS zzzz&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">DateFormat</span> df <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">SimpleDateFormat</span><span style="color: #009900;">&#40;</span>dateFormat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// this is very important</span>
        <span style="color: #003399;">TimeZone</span> timeZone <span style="color: #339933;">=</span> cal.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</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>timeZone <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>
            df.<span style="color: #006633;">setTimeZone</span><span style="color: #009900;">&#40;</span>timeZone<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003399;">String</span> dateTime <span style="color: #339933;">=</span> df.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>cal.<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>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> dateTime<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>We get the following sysouts:</p>
<blockquote><p>insert_1: 1, TIME=11:30:00:000 India Standard Time, TIME_LOCAL=06:00:00:000 India Standard Time<br />
insert_2: 2, TIME=06:30:00:000 India Standard Time, TIME_LOCAL=06:00:00:000 India Standard Time</p></blockquote>
<p>Note that in both cases, the returned TimeZone is wrongly fetched. Again, the JDBC driver is buggy.</p>
<h3>SELECT Approach 3</h3>
<p>As with INSERT, we will try to bypass the JDBC driver as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">        <span style="color: #003399;">Statement</span> stat <span style="color: #339933;">=</span> con.<span style="color: #006633;">createStatement</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">ResultSet</span> res <span style="color: #339933;">=</span> stat.<span style="color: #006633;">executeQuery</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT ID, NAME, &quot;</span>
                <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; TO_CHAR(TIME_WITH_ZONE, 'HH24:MI:SS:FF TZR'), &quot;</span>
                <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; TO_CHAR(TIME_WITH_ZONE_LOCAL, 'HH24:MI:SS:FF TZR') &quot;</span>
                <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; FROM TIMESTAMP_DEMO  ORDER BY ID&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>res.<span style="color: #006633;">next</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>
&nbsp;
            <span style="color: #003399;">String</span> timestamp <span style="color: #339933;">=</span> res.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> timestampLocal <span style="color: #339933;">=</span> res.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>res.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;NAME&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;: &quot;</span>
                    <span style="color: #339933;">+</span> res.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;ID&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, TIME=&quot;</span> <span style="color: #339933;">+</span> timestamp
                    <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;, TIME_LOCAL=&quot;</span> <span style="color: #339933;">+</span> timestampLocal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
        stat.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        res.<span style="color: #006633;">close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The following sysouts are got:</p>
<blockquote><p>insert_1: 1, TIME=11:30:00:000000000 +05:30, TIME_LOCAL=11:30:00:000000000 +05:30<br />
insert_2: 2, TIME=11:30:00:000000000 AUSTRALIA/ADELAIDE, TIME_LOCAL=11:30:00:000000000 +05:30</p></blockquote>
<p>Note that the values returned are exactly as intended.</p>
<h2>Hibernate Example</h2>
<p>Let us try and translate the above code into Hibernate. The first inference that we can draw from the above discussion is that <em>get()</em> on <span style="color: #800000;">TIMESTAMP WITH LOCAL TIME ZONE</span> will not work with the usual approach, and we have to define our custom data type to customise get() on PreparedStatement.</p>
<h3>Hibernate Approach 1</h3>
<p>This is how my custom data type (to map to <em>TIMESTAMP WITH LOCAL TIME ZONE</em>) looks like:</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: #000000; font-weight: bold;">class</span> TimestampType <span style="color: #000000; font-weight: bold;">implements</span> UserType <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// private static final Logger LOG = Logger.getLogger(TimestampType.class);</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;
    @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;">Calendar</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>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</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: #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;
        <span style="color: #000000; font-weight: bold;">return</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>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</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: #009900;">&#41;</span> <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;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</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>
&nbsp;
        <span style="color: #003399;">Calendar</span> cal <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">GregorianCalendar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>, cal<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>timestamp <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>
            cal.<span style="color: #006633;">setTime</span><span style="color: #009900;">&#40;</span>timestamp<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>
            cal <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> cal<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>
&nbsp;
        <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;">DATE</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>
&nbsp;
            doInstanceCheck<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Calendar</span> cal <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</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>cal.<span style="color: #006633;">getTimeInMillis</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, cal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <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>
&nbsp;
        <span style="color: #003399;">Calendar</span> clone <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <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>
&nbsp;
            doInstanceCheck<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Calendar</span> cal <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span><span style="color: #009900;">&#41;</span> value<span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// just copying the timezone and time</span>
            clone <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">GregorianCalendar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            clone.<span style="color: #006633;">setTimeInMillis</span><span style="color: #009900;">&#40;</span>cal.<span style="color: #006633;">getTimeInMillis</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: #003399;">TimeZone</span> tz <span style="color: #339933;">=</span> cal.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            clone.<span style="color: #006633;">setTimeZone</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span>tz.<span style="color: #006633;">getID</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;
        <span style="color: #000000; font-weight: bold;">return</span> clone<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;">true</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>
&nbsp;
        <span style="color: #003399;">Calendar</span> cal <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <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>
&nbsp;
            doInstanceCheck<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            cal <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span><span style="color: #009900;">&#41;</span> deepCopy<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> cal<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> disassemble<span style="color: #009900;">&#40;</span>cached<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> 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> disassemble<span style="color: #009900;">&#40;</span>original<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> doInstanceCheck<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</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: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>value <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Calendar</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;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span>value.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                    <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; not supported, expecting type &quot;</span>
                    <span style="color: #339933;">+</span> <span style="color: #003399;">Calendar</span>.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</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;
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And this is how my entity looks like:</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;TIMESTAMP_DEMO&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> TimestampDemo <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span> <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> <span style="color: #339933;">-</span>5902132666472097299L<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Calendar</span> timeWithZone<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Calendar</span> timeWithZoneLocal<span style="color: #339933;">;</span>
&nbsp;
    @Id
    @SequenceGenerator<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;seq&quot;</span>, sequenceName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;TIMESTAMP_DEMO_SEQ&quot;</span><span style="color: #009900;">&#41;</span>
    @GeneratedValue<span style="color: #009900;">&#40;</span>strategy <span style="color: #339933;">=</span> GenerationType.<span style="color: #006633;">SEQUENCE</span>, generator <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;seq&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> getId<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> id<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Column
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<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> name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;TIME_WITH_ZONE&quot;</span><span style="color: #009900;">&#41;</span>
    @Temporal<span style="color: #009900;">&#40;</span>TemporalType.<span style="color: #006633;">TIMESTAMP</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Calendar</span> getTimeWithZone<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> timeWithZone<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;TIME_WITH_ZONE_LOCAL&quot;</span><span style="color: #009900;">&#41;</span>
    @Type<span style="color: #009900;">&#40;</span>type <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com.swayam.demo.oracle.hibernate.custom.TimestampType&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Calendar</span> getTimeWithZoneLocal<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> timeWithZoneLocal<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> setId<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span> id<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;">id</span> <span style="color: #339933;">=</span> id<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> setName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<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;">name</span> <span style="color: #339933;">=</span> name<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> setTimeWithZone<span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span> timeWithZone<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;">timeWithZone</span> <span style="color: #339933;">=</span> timeWithZone<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> setTimeWithZoneLocal<span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span> timeWithZoneLocal<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;">timeWithZoneLocal</span> <span style="color: #339933;">=</span> timeWithZoneLocal<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>As expected, this will not save/get correct TimeZone data as it depends on the buggy JDBC driver.</p>
<h3>Hibernate Approach 2</h3>
<p>Here, as before, we use the <em>TO_TIMESTAMP_TZ()</em> function to circumvent the buggy JDBC driver. We keep the <em>TimestampType</em> for mapping to <em>TIMESTAMP WITH LOCAL TIME ZONE</em> and add <em>TimestampType2</em> to map to <em>TIMESTAMP WITH TIME ZONE</em>.</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: #000000; font-weight: bold;">class</span> TimestampType2 <span style="color: #000000; font-weight: bold;">extends</span> TimestampType <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> Logger LOG <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>TimestampType2.<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: #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>
&nbsp;
        <span style="color: #003399;">Calendar</span> cal <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> timestamp <span style="color: #339933;">=</span> rs.<span style="color: #006633;">getString</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>
&nbsp;
        LOG.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;raw timestamp=&quot;</span> <span style="color: #339933;">+</span> timestamp<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>timestamp <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>
            cal <span style="color: #339933;">=</span> parseOracleTimestampWithZone<span style="color: #009900;">&#40;</span>timestamp<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> cal<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>
&nbsp;
        <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;">DATE</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>
&nbsp;
            doInstanceCheck<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Calendar</span> cal <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span><span style="color: #009900;">&#41;</span> value<span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #003399;">String</span> dateTime <span style="color: #339933;">=</span> getOracleFormattedTimeWithZone<span style="color: #009900;">&#40;</span>cal<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            LOG.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dateTime=&quot;</span> <span style="color: #339933;">+</span> dateTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            LOG.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;index=&quot;</span> <span style="color: #339933;">+</span> index<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            st.<span style="color: #006633;">setString</span><span style="color: #009900;">&#40;</span>index, dateTime<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Converts raw time-stamp with time-zone string from Oracle to Calendar
     * containing the time-zone
     *
     * @param rawTimestamp
     *            in the format &lt;em&gt;2010-9-26 11.30.0.0 Australia/Adelaide&lt;/em&gt;
     * @return
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Calendar</span> parseOracleTimestampWithZone<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> rawTimestamp<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003399;">Calendar</span> cal <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">String</span> dateFormat <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;yyyy-MM-dd HH.mm.ss.SSS&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">SimpleDateFormat</span> df <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">SimpleDateFormat</span><span style="color: #009900;">&#40;</span>dateFormat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <span style="color: #003399;">Date</span> date <span style="color: #339933;">=</span> df.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>rawTimestamp<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Calendar</span> tempCal <span style="color: #339933;">=</span> <span style="color: #003399;">Calendar</span>.<span style="color: #006633;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            tempCal.<span style="color: #006633;">setTime</span><span style="color: #009900;">&#40;</span>date<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #003399;">String</span> timeZoneId <span style="color: #339933;">=</span> rawTimestamp.<span style="color: #006633;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">TimeZone</span> timeZone <span style="color: #339933;">=</span> <span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span>timeZoneId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            cal <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">GregorianCalendar</span><span style="color: #009900;">&#40;</span>timeZone<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// setting the date and time-zone does not work, as Calendar adjusts</span>
            <span style="color: #666666; font-style: italic;">// for time zone, so the below circus</span>
&nbsp;
            cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">YEAR</span>, tempCal.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">YEAR</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MONTH</span>, tempCal.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MONTH</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">DATE</span>, tempCal.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">DATE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">HOUR_OF_DAY</span>, tempCal.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">HOUR_OF_DAY</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MINUTE</span>, tempCal.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MINUTE</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">SECOND</span>, tempCal.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">SECOND</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            cal.<span style="color: #006633;">set</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MILLISECOND</span>, tempCal.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span>.<span style="color: #006633;">MILLISECOND</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">ParseException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            LOG.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not convert string `&quot;</span> <span style="color: #339933;">+</span> rawTimestamp
                    <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;` to Calendar&quot;</span>, e<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> cal<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">String</span> getOracleFormattedTimeWithZone<span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span> timeWithZone<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003399;">String</span> dateFormat <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;yyyy-MM-dd HH:mm:ss:SSS&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">DateFormat</span> df <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">SimpleDateFormat</span><span style="color: #009900;">&#40;</span>dateFormat<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// this is very important</span>
        <span style="color: #003399;">TimeZone</span> timeZone <span style="color: #339933;">=</span> timeWithZone.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</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>timeZone <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>
            df.<span style="color: #006633;">setTimeZone</span><span style="color: #009900;">&#40;</span>timeZone<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #003399;">String</span> dateTime <span style="color: #339933;">=</span> df.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>timeWithZone.<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: #003399;">String</span> tzId <span style="color: #339933;">=</span> timeWithZone.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getID</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        dateTime <span style="color: #339933;">+=</span> <span style="color: #0000ff;">&quot; &quot;</span> <span style="color: #339933;">+</span> tzId<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> dateTime<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And this is how my modified entity looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@SQLInsert<span style="color: #009900;">&#40;</span>sql <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO TIMESTAMP_DEMO (NAME, TIME_WITH_ZONE, TIME_WITH_ZONE_LOCAL, ID) values (?, TO_TIMESTAMP_TZ(?, 'YYYY-MM-DD HH24:MI:SS:FF TZR'), ?, ?)&quot;</span><span style="color: #009900;">&#41;</span>
@Table<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;TIMESTAMP_DEMO&quot;</span><span style="color: #009900;">&#41;</span>
@<span style="color: #003399;">Entity</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> TimestampDemo2 <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span> <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> 4940963602672391841L<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Calendar</span> timeWithZone<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Calendar</span> timeWithZoneLocal<span style="color: #339933;">;</span>
&nbsp;
    @Id
    @SequenceGenerator<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;seq&quot;</span>, sequenceName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;TIMESTAMP_DEMO_SEQ&quot;</span><span style="color: #009900;">&#41;</span>
    @GeneratedValue<span style="color: #009900;">&#40;</span>strategy <span style="color: #339933;">=</span> GenerationType.<span style="color: #006633;">SEQUENCE</span>, generator <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;seq&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> getId<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> id<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Column
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<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> name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;TIME_WITH_ZONE&quot;</span><span style="color: #009900;">&#41;</span>
    @Type<span style="color: #009900;">&#40;</span>type <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com.swayam.demo.oracle.hibernate.custom.TimestampType2&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Calendar</span> getTimeWithZone<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> timeWithZone<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;TIME_WITH_ZONE_LOCAL&quot;</span><span style="color: #009900;">&#41;</span>
    @Type<span style="color: #009900;">&#40;</span>type <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com.swayam.demo.oracle.hibernate.custom.TimestampType&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Calendar</span> getTimeWithZoneLocal<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> timeWithZoneLocal<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> setId<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span> id<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;">id</span> <span style="color: #339933;">=</span> id<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> setName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<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;">name</span> <span style="color: #339933;">=</span> name<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> setTimeWithZone<span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span> timeWithZone<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;">timeWithZone</span> <span style="color: #339933;">=</span> timeWithZone<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> setTimeWithZoneLocal<span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span> timeWithZoneLocal<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;">timeWithZoneLocal</span> <span style="color: #339933;">=</span> timeWithZoneLocal<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This set up gives us the desirable save/get results with the correct TimeZone value.</p>
<h3>Conclusion</h3>
<p>I take this opportunity to illustrate the advantage of Open Source Software as opposed to proprietary ones. If the ojdbc sources were in public domain, we could have patched the bug instead of having to circumvent them as in the present instance.</p>
<h3>Resources</h3>
<p>The sources can be found <a href="http://puretech.paawak.com/resources/java/hibernate/OracleTimeZoneTest.zip" target="_blank">here</a>. There are three test cases which illustrates the above discussion. I have used the ojdbc driver 5 for this example. Since I am using Maven for building this, the libraries are not a part of this distribution. You can grab any Oracle JDBC driver from <a href="http://www.oracle.com/technetwork/database/features/jdbc/index-091264.html" target="_blank">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://puretech.paawak.com/2010/11/02/how-to-handle-oracle-timestamp-with-timezone-from-java/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Using custom data type in Hibernate</title>
		<link>http://puretech.paawak.com/2010/10/31/using-custom-data-type-in-hibernate/</link>
		<comments>http://puretech.paawak.com/2010/10/31/using-custom-data-type-in-hibernate/#comments</comments>
		<pubDate>Sun, 31 Oct 2010 13:36:36 +0000</pubDate>
		<dc:creator>paawak</dc:creator>
		
		<category><![CDATA[hibernate]]></category>

		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://puretech.paawak.com/?p=389</guid>
		<description><![CDATA[Why need custom data type?
I have a java.util.Calendar object which I want to persist in the db, along with the TimeZone. Suppose I am working with a db which does not have support for storing TimeZone. How do I proceed? One of the easiest solutions would be to store the entire timestamp as a VARCHAR. [...]]]></description>
			<content:encoded><![CDATA[<h2>Why need custom data type?</h2>
<p>I have a <em>java.util.Calendar</em> object which I want to persist in the db, along with the <em>TimeZone</em>. Suppose I am working with a db which does not have support for storing <em>TimeZone</em>. How do I proceed? One of the easiest solutions would be to store the entire timestamp as a <em>VARCHAR</em>. Granted, but how do I instruct Hibernate to use a <em>java.util.Calendar</em> object instead of a String? Its done by annotating with <em>@Type(type=&#8221;")</em>, where <em>type</em> should be the fully qualified name of a class implementing the <em>org.hibernate.usertype.UserType</em> interface.</p>
<h2>Custom UserType</h2>

<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: #000000; font-weight: bold;">class</span> TimeWithZone <span style="color: #000000; font-weight: bold;">implements</span> UserType <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> Logger LOG <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>TimeWithZone.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Define the supported column types
     */</span>
    @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;">VARCHAR</span> <span style="color: #009900;">&#125;</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: #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;">Calendar</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>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</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: #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;
        <span style="color: #000000; font-weight: bold;">return</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>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</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: #009900;">&#41;</span> <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;
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #cc66cc;">0</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>
&nbsp;
        <span style="color: #003399;">Calendar</span> cal <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> timestampStr <span style="color: #339933;">=</span> rs.<span style="color: #006633;">getString</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>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>timestampStr <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>
            cal <span style="color: #339933;">=</span> getTimeWithZone<span style="color: #009900;">&#40;</span>timestampStr<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> cal<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>
&nbsp;
        <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;">VARCHAR</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>
&nbsp;
            doInstanceCheck<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Calendar</span> cal <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span><span style="color: #009900;">&#41;</span> value<span style="color: #339933;">;</span>
            st.<span style="color: #006633;">setString</span><span style="color: #009900;">&#40;</span>index, getTimeWithZone<span style="color: #009900;">&#40;</span>cal<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <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>
&nbsp;
        <span style="color: #003399;">Calendar</span> clone <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <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>
&nbsp;
            doInstanceCheck<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">Calendar</span> cal <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span><span style="color: #009900;">&#41;</span> value<span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// just copying the timezone and time</span>
            clone <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">GregorianCalendar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            clone.<span style="color: #006633;">setTimeInMillis</span><span style="color: #009900;">&#40;</span>cal.<span style="color: #006633;">getTimeInMillis</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: #003399;">TimeZone</span> tz <span style="color: #339933;">=</span> cal.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            clone.<span style="color: #006633;">setTimeZone</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span>tz.<span style="color: #006633;">getID</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;
        <span style="color: #000000; font-weight: bold;">return</span> clone<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;">true</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>
&nbsp;
        <span style="color: #003399;">Calendar</span> cal <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <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>
&nbsp;
            doInstanceCheck<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            cal <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span><span style="color: #009900;">&#41;</span> deepCopy<span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">return</span> cal<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> disassemble<span style="color: #009900;">&#40;</span>cached<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> 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> disassemble<span style="color: #009900;">&#40;</span>original<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> doInstanceCheck<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</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: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>value <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Calendar</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;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">UnsupportedOperationException</span><span style="color: #009900;">&#40;</span>value.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                    <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; not supported, expecting type &quot;</span>
                    <span style="color: #339933;">+</span> <span style="color: #003399;">Calendar</span>.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</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;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Converts a String &lt;em&gt;2010-09-26 11:30:00 Australia/Adelaide&lt;/em&gt; to
     * Calendar
     *
     * @param timeWithZone
     * @return
     */</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Calendar</span> getTimeWithZone<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> timeWithZone<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003399;">String</span> timeZoneId <span style="color: #339933;">=</span> timeWithZone.<span style="color: #006633;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\\</span>s&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">TimeZone</span> tz <span style="color: #339933;">=</span> <span style="color: #003399;">TimeZone</span>.<span style="color: #006633;">getTimeZone</span><span style="color: #009900;">&#40;</span>timeZoneId<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">String</span> format <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;yyyy-MM-dd HH:mm:ss&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">DateFormat</span> df <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">SimpleDateFormat</span><span style="color: #009900;">&#40;</span>format<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        df.<span style="color: #006633;">setTimeZone</span><span style="color: #009900;">&#40;</span>tz<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            df.<span style="color: #006633;">parse</span><span style="color: #009900;">&#40;</span>timeWithZone<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">ParseException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            LOG.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;could not parse date string: &quot;</span> <span style="color: #339933;">+</span> timeWithZone, e<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> df.<span style="color: #006633;">getCalendar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> getTimeWithZone<span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span> timeWithZone<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003399;">String</span> format <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;yyyy-MM-dd HH:mm:ss zzzz&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">DateFormat</span> df <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">SimpleDateFormat</span><span style="color: #009900;">&#40;</span>format<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        df.<span style="color: #006633;">setTimeZone</span><span style="color: #009900;">&#40;</span>timeWithZone.<span style="color: #006633;">getTimeZone</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;">return</span> df.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>timeWithZone.<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>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Hibenate Entity</h2>

<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;CUSTOM_DEMO&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> CustomDemo <span style="color: #009900;">&#123;</span>
&nbsp;
    @Id
    @SequenceGenerator<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;seq&quot;</span>, allocationSize <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>, initialValue <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>, sequenceName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;CUSTOM_DEMO_SEQ&quot;</span><span style="color: #009900;">&#41;</span>
    @GeneratedValue<span style="color: #009900;">&#40;</span>strategy <span style="color: #339933;">=</span> GenerationType.<span style="color: #006633;">SEQUENCE</span>, generator <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;seq&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #339933;">;</span>
&nbsp;
    @Column
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
    @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;TIME_WITH_ZONE&quot;</span><span style="color: #009900;">&#41;</span>
    @Type<span style="color: #009900;">&#40;</span>type <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;com.swayam.demo.oracle.hibernate.TimeWithZone&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Calendar</span> timeWithZone<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> getId<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> id<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> setId<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span> id<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;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<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> name<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> setName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<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;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">Calendar</span> getTimeWithZone<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> timeWithZone<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> setTimeWithZone<span style="color: #009900;">&#40;</span><span style="color: #003399;">Calendar</span> timeWithZone<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;">timeWithZone</span> <span style="color: #339933;">=</span> timeWithZone<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Resources</h2>
<p>The sources can be found <a href="http://puretech.paawak.com/resources/java/hibernate/HibernateCustomDataType.zip">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://puretech.paawak.com/2010/10/31/using-custom-data-type-in-hibernate/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Id generation in Hibernate with Sequence</title>
		<link>http://puretech.paawak.com/2010/10/31/id-generation-in-hibernate-with-sequence/</link>
		<comments>http://puretech.paawak.com/2010/10/31/id-generation-in-hibernate-with-sequence/#comments</comments>
		<pubDate>Sun, 31 Oct 2010 13:17:33 +0000</pubDate>
		<dc:creator>paawak</dc:creator>
		
		<category><![CDATA[hibernate]]></category>

		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://puretech.paawak.com/?p=386</guid>
		<description><![CDATA[I have the following SQL Script:

CREATE SEQUENCE  MY_SEQ
START WITH 1
INCREMENT BY 1;
&#160;
CREATE TABLE  SEQ_DEMO &#40;
	ID             INTEGER PRIMARY KEY NOT NULL,
	NAME     VARCHAR2&#40;20&#41; NOT NULL
    &#41; ;

The Hibernate entity for this would be:

@Entity
@Table&#40;name = &#34;SEQ_DEMO&#34;&#41;
public class [...]]]></description>
			<content:encoded><![CDATA[<p>I have the following SQL Script:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> SEQUENCE  MY_SEQ
START <span style="color: #993333; font-weight: bold;">WITH</span> <span style="color: #cc66cc;">1</span>
INCREMENT <span style="color: #993333; font-weight: bold;">BY</span> <span style="color: #cc66cc;">1</span>;
&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span>  SEQ_DEMO <span style="color: #66cc66;">&#40;</span>
	ID             INTEGER <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
	NAME     VARCHAR2<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span>
    <span style="color: #66cc66;">&#41;</span> ;</pre></div></div>

<p>The Hibernate entity for this would be:</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;SEQ_DEMO&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> SeqDemo <span style="color: #009900;">&#123;</span>
&nbsp;
    @Id
    @SequenceGenerator<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;seq&quot;</span>, allocationSize <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>, initialValue <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span>, sequenceName <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;MY_SEQ&quot;</span><span style="color: #009900;">&#41;</span>
    @GeneratedValue<span style="color: #009900;">&#40;</span>strategy <span style="color: #339933;">=</span> GenerationType.<span style="color: #006633;">SEQUENCE</span>, generator <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;seq&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> id<span style="color: #339933;">;</span>
&nbsp;
    @Column
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> name<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">long</span> getId<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> id<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> setId<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span> id<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;">id</span> <span style="color: #339933;">=</span> id<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getName<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> name<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> setName<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<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;">name</span> <span style="color: #339933;">=</span> name<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://puretech.paawak.com/2010/10/31/id-generation-in-hibernate-with-sequence/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Annotation based Spring MVC, how tos</title>
		<link>http://puretech.paawak.com/2010/09/05/annotation-based-spring-mvc-how-tos/</link>
		<comments>http://puretech.paawak.com/2010/09/05/annotation-based-spring-mvc-how-tos/#comments</comments>
		<pubDate>Sun, 05 Sep 2010 05:56:42 +0000</pubDate>
		<dc:creator>paawak</dc:creator>
		
		<category><![CDATA[java]]></category>

		<category><![CDATA[spring]]></category>

		<category><![CDATA[spring mvc]]></category>

		<guid isPermaLink="false">http://puretech.paawak.com/?p=360</guid>
		<description><![CDATA[I will list here the common how to-s for the new Annotation based Spring MVC. Please note that I am using Spring version 3.0.0.RELEASE. This is a bit, only a small bit, different from the version 2.5.x. So, if you are using 2.5.x, you have to tweak things a bit before you get the same [...]]]></description>
			<content:encoded><![CDATA[<p>I will list here the common how to-s for the new Annotation based Spring MVC. Please note that I am using Spring version <strong>3.0.0.RELEASE</strong>. This is a bit, only a small bit, different from the version <strong>2.5.x</strong>. So, if you are using 2.5.x, you have to tweak things a bit before you get the same effect.</p>
<p>Consider the following screen:</p>
<p><img class="aligncenter size-full wp-image-361" title="samplehtml" src="http://puretech.paawak.com/wp-content/uploads/2010/09/samplehtml.png" alt="samplehtml" width="561" height="404" /></p>
<p>The fantatstic thing about Spring MVC is it lets you take an Object Oriented approach while designing the Model (Form Bean) and lets you bind that seamlessly to the View (JSP Form).  For the above scenario, we can break down the top section into a list of rows. I would denote the row with a class having select, itemName, price&#8230; attributes, as shown below (this is an inner class in my <em>ItemBean</em> class, which is my Form Bean):</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: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> ItemRow <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">boolean</span> selected<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> itemName<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span> price<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> quantity<span style="color: #339933;">;</span>
&nbsp;
        ...
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then, in my ItemBean class, I can have a <em>List</em> of <em>ItemRow</em>, as shown:</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: #000000; font-weight: bold;">class</span> ItemBean <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> List<span style="color: #339933;">&lt;</span>ItemRow<span style="color: #339933;">&gt;</span> items<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span> totalPrice<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> expectedDelivery<span style="color: #339933;">;</span>
...
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>How do I display/bind my Model to View?</h2>
<p>This is how the  View (<em>Item.jsp</em>) looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="xhtml" style="font-family:monospace;">                                           &lt;c:forEach var=&quot;item&quot; items=&quot;${command.items}&quot; varStatus=&quot;count&quot;&gt;
						&lt;tr&gt;
							&lt;td&gt;
								&lt;form:checkbox id=&quot;chkSelect&quot; path=&quot;items[${count.index}].selected&quot; /&gt;
							&lt;/td&gt;
							&lt;td&gt;
								&lt;c:out value=&quot;${item.itemName}&quot;/&gt;
							&lt;/td&gt;
							&lt;td&gt;
								&lt;input id=&quot;txtUnitPrice&quot; type=&quot;text&quot; disabled=&quot;disabled&quot; value=&quot;${item.price}&quot; size=&quot;4&quot;/&gt;
							&lt;/td&gt;
							&lt;td&gt;								
								&lt;form:input id=&quot;txtQuantity&quot; path=&quot;items[${count.index}].quantity&quot; size=&quot;3&quot; disabled=&quot;${!item.selected}&quot;/&gt;
							&lt;/td&gt;
							&lt;td&gt;
								&lt;input id=&quot;txtItemPrice&quot; type=&quot;text&quot; readonly=&quot;readonly&quot; value=&quot;${item.price * item.quantity}&quot; size=&quot;6&quot;/&gt;
							&lt;/td&gt;
						&lt;/tr&gt;
						&lt;/c:forEach&gt;
                                                &lt;tr&gt;
							&lt;td style=&quot;height:30px;&quot;&gt;
							&lt;/td&gt;
						&lt;/tr&gt;
						&lt;tr&gt;
							&lt;td colspan=&quot;3&quot;&gt;
								Total Price:
							&lt;/td&gt;
							&lt;td&gt;
								&lt;form:input id=&quot;txtTotalPrice&quot; path=&quot;totalPrice&quot; readonly=&quot;true&quot; size=&quot;6&quot; /&gt;
							&lt;/td&gt;
						&lt;/tr&gt;</pre></div></div>

<p>Especially note the<br />
<blockquote><span style="color: #0000ff;">path=&#8221;items[${count.index}].selected&#8221; </span></p></blockquote>
<p> in the <span style="color: #0000ff;">form:checkbox </span> tag. This is the key. It binds a particular index of the <em>items</em> in the <em>ItemBean</em> class to the JSP.</p>
<h2>How to map a given url to my Controller method</h2>
<p>After the introduction of Annotation <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/stereotype/Controller.html" target="_blank">@Controller</a>, all Spring MVC controllers have become like <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/index.html?org/springframework/web/servlet/mvc/multiaction/MultiActionController.html" target="_blank">MultiActionController</a>, that is, you can handle multiple requests in the same controller. Mapping a url is very simple: you should annotate any public method with the <em>@RequestMapping(&#8221;/DesiredUrl.do&#8221;)</em>. Of course, you should make sure:</p>
<p>1.&gt; The class is annotated with <em>@Controller</em><br />
2.&gt; The following line is present in the Spring context file:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><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.swayam.demo.web.controller&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>

<p>The method can have a motley combination of parameters and return type. Read the <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/bind/annotation/RequestMapping.html" target="_blank">javadocs</a> for details. This annotation takes an optional parameter <em>method</em>, which can be any enum <em>RequestMethod</em> type. If you do not specify anything, your method will handle all types of requests like GET, POST, DELETE, etc.</p>
<h2>How do I pre-load or customise my Form Bean before it reaches my controller method?</h2>
<p>In Spring MVC, the framework creates an instance of the Form Bean and binds it to the fields in JSP in case of a POST. In many cases, I would like to customise the Form Bean before it starts binding to the JSP fields. In older versions of Spring MVC, this was done by overriding the <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/servlet/mvc/AbstractFormController.html#formBackingObject(javax.servlet.http.HttpServletRequest)" target="_blank">formBackingObject() method in AbstractFormController</a>.<br />
With annotations, you do it by specifying the <em>@ModelAttribute</em> before the FormBean attribute in the request handler method and then again on a public method returning the customised Form Bean as shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    @RequestMapping<span style="color: #009900;">&#40;</span>value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/checkout.htm&quot;</span>, method <span style="color: #339933;">=</span> RequestMethod.<span style="color: #006633;">POST</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> ModelAndView checkout<span style="color: #009900;">&#40;</span>
            @ModelAttribute<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;postBean&quot;</span><span style="color: #009900;">&#41;</span> ItemBean formBean,
            BindingResult errors<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        ModelAndView model <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        model.<span style="color: #006633;">addObject</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;command&quot;</span>, formBean<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
...
        <span style="color: #000000; font-weight: bold;">return</span> model<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @ModelAttribute<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;postBean&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> ItemBean initBeanForPost<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        ItemBean bean <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ItemBean<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        populateBean<span style="color: #009900;">&#40;</span>bean, <span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> bean<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Note that its always a good practice to specify the name in the <em>@ModelAttribute</em> annotation. This way you can pre-load different beans for different methods. As shown in the example above, the name specified in the <em>@ModelAttribute</em> annotation in the request handler method <em>checkout()</em> and that in the method <em>initBeanForPost()</em> should be an exact match. By the way, this works for GET as well as POST or any other request type.</p>
<h2>How do I do the validation?</h2>
<p>Spring version 3.x onwards, doing validations have become very easy. I will describe this in the following few steps:</p>
<h4>1. Write a Validator</h4>
<p>Your validator should implement the <em>Validator</em> interface. It has two methods that you need to override. The <em>supports()</em> method makes sure that the Validator can validate a given FormBean. The <em>validate()</em> method does the actual job of validating. A typical Validator will look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> ItemValidator <span style="color: #000000; font-weight: bold;">implements</span> Validator <span style="color: #009900;">&#123;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> validate<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> target, Errors errors<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        ItemBean bean <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ItemBean<span style="color: #009900;">&#41;</span> target<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>bean.<span style="color: #006633;">getTotalPrice</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            errors.<span style="color: #006633;">rejectValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;totalPrice&quot;</span>, <span style="color: #0000ff;">&quot;noItemsSelected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <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> supports<span style="color: #009900;">&#40;</span>Class<span style="color: #339933;">&lt;?&gt;</span> clazz<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> clazz <span style="color: #339933;">==</span> ItemBean.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<h4>2. Set the Validator</h4>
<p>You do this in a public method annotated with <em>@InitBinder</em> in the controller.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    @InitBinder
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> initBinder<span style="color: #009900;">&#40;</span>WebDataBinder binder, WebRequest webRequest<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>binder.<span style="color: #006633;">getTarget</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">instanceof</span> ItemBean<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            binder.<span style="color: #006633;">setValidator</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> ItemValidator<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: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>I am setting the validator inside the <em>if (binder.getTarget() instanceof ItemBean)</em> block as otherwise I get an <strong>ugly exception</strong> in case there are any <strong>validation errors</strong>. I am putting the stack trace below:</p>
<blockquote><p><span style="color: #ff0000;"><br />
java.lang.IllegalStateException: Invalid target for Validator [com.swayam.demo.web.controller.ItemController$1@11e7cc6]: org.springframework.validation.BeanPropertyBindingResult: 1 errors<br />
Field error in object &#8216;postBean&#8217; on field &#8216;totalPrice&#8217;: rejected value [0.0]; codes [noItemsSelected.postBean.totalPrice,noItemsSelected.totalPrice,noItemsSelected.float,noItemsSelected]; arguments []; default message [null]] with root cause<br />
java.lang.IllegalStateException: Invalid target for Validator [com.swayam.demo.web.controller.ItemController$1@11e7cc6]: org.springframework.validation.BeanPropertyBindingResult: 1 errors<br />
Field error in object &#8216;postBean&#8217; on field &#8216;totalPrice&#8217;: rejected value [0.0]; codes [noItemsSelected.postBean.totalPrice,noItemsSelected.totalPrice,noItemsSelected.float,noItemsSelected]; arguments []; default message [null]<br />
	at org.springframework.validation.DataBinder.setValidator(DataBinder.java:472)<br />
	at com.swayam.demo.web.controller.ItemController.initBinder(ItemController.java:63)<br />
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)<br />
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)<br />
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)<br />
	at java.lang.reflect.Method.invoke(Method.java:597)<br />
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.doInvokeMethod(HandlerMethodInvoker.java:710)<br />
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.initBinder(HandlerMethodInvoker.java:329)<br />
	at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.updateModelAttributes(HandlerMethodInvoker.java:691)<br />
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:417)<br />
	at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:402)<br />
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:771)<br />
</span></p></blockquote>
<h4>3. Use @Valid before the Form Bean</h4>
<p>This is a new feature added in release 3.x. Now the validation happens with JSR-303 Bean Validation API. You can find the jar <a href="http://repo1.maven.org/maven2/javax/validation/validation-api/1.0.0.GA/" target="_blank">here</a>. If you are using Maven, you can add the following dependency:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.validation<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>validation-api<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.0.GA<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>This is how you use it:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    @RequestMapping<span style="color: #009900;">&#40;</span>value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/checkout.htm&quot;</span>, method <span style="color: #339933;">=</span> RequestMethod.<span style="color: #006633;">POST</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> ModelAndView checkout<span style="color: #009900;">&#40;</span>
            @Valid ItemBean formBean,
            BindingResult errors<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        ModelAndView model <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<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> model<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The Form Bean is validated with the set Validator by the framework. Note that for Spring 2.5.x, you need to make the call to validator manually inside the handler method:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">...
        <span style="color: #006633;">Validator</span> validator <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ItemValidator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        validator.<span style="color: #006633;">validate</span><span style="color: #009900;">&#40;</span>formBean, errors<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>    
...</pre></div></div>

<h2>In my Validator, how do I use messages from properties file for i18n?</h2>
<p>Lets say,  my properties file is <em>Messages.properties</em> located at <em>/com/swayam/demo/web/res/</em>. The contents are:</p>
<blockquote><p>noItemsSelected=You have not selected any items</p></blockquote>
<p>I have to create an instance of <em>MessageSource</em> in my Spring conf file as shown:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">	<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;messageSource&quot;</span></span>
<span style="color: #009900;">		<span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.springframework.context.support.ResourceBundleMessageSource&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;basenames&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
				<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.swayam.demo.web.res.Messages<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/list<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/property<span style="color: #000000; font-weight: bold;">&gt;</span></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></pre></div></div>

<p>Now I can use the <em>property key</em> in my <em>validate()</em> method as shown:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> validate<span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> target, Errors errors<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        ItemBean bean <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>ItemBean<span style="color: #009900;">&#41;</span> target<span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>bean.<span style="color: #006633;">getTotalPrice</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            errors.<span style="color: #006633;">rejectValue</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;totalPrice&quot;</span>, <span style="color: #0000ff;">&quot;noItemsSelected&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<h2>How to display the Validation messages on my JSP?</h2>
<p>First, from the controller, you have to pass an instance of <em>BindingResult</em> to the JSP.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    @RequestMapping<span style="color: #009900;">&#40;</span>value <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/checkout.htm&quot;</span>, method <span style="color: #339933;">=</span> RequestMethod.<span style="color: #006633;">POST</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">public</span> ModelAndView checkout<span style="color: #009900;">&#40;</span>
            @Valid ItemBean formBean,
            BindingResult errors<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        ModelAndView model <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ModelAndView<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        model.<span style="color: #006633;">addObject</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;command&quot;</span>, formBean<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>errors.<span style="color: #006633;">hasErrors</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>
            model.<span style="color: #006633;">setViewName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Item&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            model.<span style="color: #006633;">addObject</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;errors&quot;</span>, errors<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>
            model.<span style="color: #006633;">setViewName</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Checkout&quot;</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;">return</span> model<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>And this is how the JSP looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="jsp" style="font-family:monospace;">		&lt;c:if test=&quot;${ not empty errors.allErrors }&quot;&gt; 
		&lt;div style=&quot;text-align:center; color:red;&quot;&gt;
			&lt;ul&gt;
				&lt;c:forEach var=&quot;error&quot; items=&quot;${errors.allErrors}&quot;&gt;
					&lt;li&gt;&lt;spring:message code=&quot;${error.code}&quot;  text=&quot;${error.defaultMessage}&quot;/&gt;&lt;/li&gt;
				&lt;/c:forEach&gt;
			&lt;/ul&gt;
		&lt;/div&gt;
		&lt;/c:if&gt;</pre></div></div>

<h2>How do I bind a complex object to my JSP form field?</h2>
<p>Often we have to display a complex object as text. The best example I can take is that of a Date. Its a <em>java.util.Date</em> in the model. How do I map this to a text field in my JSP? You have to extend the <em>PropertyEditorSupport</em> and override the following methods:<br />
1. <strong>getAsText()</strong>: Converts an Object to its String representation. Used for displaying a model object in the JSP.<br />
2. <strong>setAsText(String text)</strong>: Converts the text from the input field to the complex object that the model understands.<br />
A typical implementation would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> DateEditorSupport <span style="color: #000000; font-weight: bold;">extends</span> <span style="color: #003399;">PropertyEditorSupport</span> <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> Logger LOG <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>DateEditorSupport.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Format</span> formatter<span style="color: #339933;">;</span>
&nbsp;
    DateEditorSupport<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> dateFormat<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        formatter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">SimpleDateFormat</span><span style="color: #009900;">&#40;</span>dateFormat<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: #003399;">String</span> getAsText<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #003399;">String</span> date <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">Object</span> value <span style="color: #339933;">=</span> getValue<span style="color: #009900;">&#40;</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>value <span style="color: #000000; font-weight: bold;">instanceof</span> <span style="color: #003399;">Date</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            date <span style="color: #339933;">=</span> formatter.<span style="color: #006633;">format</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <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;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> java.<span style="color: #006633;">lang</span>.<span style="color: #003399;">IllegalArgumentException</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Expecting a &quot;</span>
                    <span style="color: #339933;">+</span> <span style="color: #003399;">Date</span>.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; class, got &quot;</span>
                    <span style="color: #339933;">+</span> value.<span style="color: #006633;">getClass</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getName</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;
        <span style="color: #000000; font-weight: bold;">return</span> date<span style="color: #339933;">;</span>
&nbsp;
    <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> setAsText<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> text<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
&nbsp;
            <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> formatter.<span style="color: #006633;">parseObject</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            setValue<span style="color: #009900;">&#40;</span>date<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">ParseException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            LOG.<span style="color: #006633;">fatal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error setting date for String: &quot;</span> <span style="color: #339933;">+</span> text, e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Then, in the <em>@InitBinder</em> method, you need to register this against the class:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">    @InitBinder
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> initBinder<span style="color: #009900;">&#40;</span>WebDataBinder binder, WebRequest webRequest<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        binder.<span style="color: #006633;">registerCustomEditor</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Date</span>.<span style="color: #000000; font-weight: bold;">class</span>, <span style="color: #000000; font-weight: bold;">new</span> DateEditorSupport<span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">&quot;dd/MM/yyyy&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Putting it all together</h2>
<p>You will find the sources <a href="http://puretech.paawak.com/resources/java/spring-mvc-example/SpringMVC_src.zip" >here</a>. Its an Eclipse project, using Maven. In order to get all the libraries, install <a href="http://maven.apache.org/" target="_blank">Maven</a> and run:<br />
<em>mvn eclipse:clean eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true -Dwtpversion=2.0</em><br />
You can also run <a href="http://puretech.paawak.com/resources/java/spring-mvc-example/SpringMVC.war">the war file</a> directly and go to <a href="http://localhost:8080/SpringMVC/" target="_blank">http://localhost:8080/SpringMVC/</a> to see it in action.</p>
]]></content:encoded>
			<wfw:commentRss>http://puretech.paawak.com/2010/09/05/annotation-based-spring-mvc-how-tos/feed/</wfw:commentRss>
		</item>
		<item>
		<title>[How To] Convert a Maven Project to Eclipse Web Project</title>
		<link>http://puretech.paawak.com/2010/08/28/how-to-convert-a-maven-project-to-eclipse-web-project/</link>
		<comments>http://puretech.paawak.com/2010/08/28/how-to-convert-a-maven-project-to-eclipse-web-project/#comments</comments>
		<pubDate>Sat, 28 Aug 2010 06:48:37 +0000</pubDate>
		<dc:creator>paawak</dc:creator>
		
		<category><![CDATA[java]]></category>

		<category><![CDATA[maven]]></category>

		<guid isPermaLink="false">http://puretech.paawak.com/?p=334</guid>
		<description><![CDATA[Eclipse WTP has this nice feature where it allows you to deploy, run and debug Web Applications from the IDE. You start by going to the Server View, adding the target server (like Apache Tomcat in our case) and then adding the Web Project to the server. This is how the view looks like:

But I [...]]]></description>
			<content:encoded><![CDATA[<p>Eclipse WTP has this nice feature where it allows you to deploy, run and debug Web Applications from the IDE. You start by going to the Server View, adding the target server (like Apache Tomcat in our case) and then adding the Web Project to the server. This is how the view looks like:</p>
<p><img class="aligncenter size-full wp-image-346" title="addremovewebprojects_1" src="http://puretech.paawak.com/wp-content/uploads/2010/08/addremovewebprojects_1.png" alt="addremovewebprojects_1" width="882" height="669" /></p>
<p>But I have often faced this problem: when I run <em>mvn eclipse:eclipse</em> on a Maven Web Project to convert it to an Eclipse project, it is not recognised as a Web Project by Eclipse. When I right-click on the server and select <em>Add Remove Projects</em>, it does not appear in the option box. But to me  <em>mvn eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true</em> is indispensable as it downloads and links all the sources and JavaDocs, which comes very handy. On the other hand if I cannot add my project as a Web Project in Eclipse, running and debugging becomes a big issue. So how do I reconcile these two?</p>
<h2>Solution 1: Specify the WTP Version</h2>
<p>While running <em>mvn eclipse:eclipse</em>, specify the WTP Version by passing the <em>-Dwtpversion=2.0</em> argument. The command looks like:</p>
<p><em>mvn eclipse:clean eclipse:eclipse -DdownloadSources=true -DdownloadJavadocs=true <span style="color: #0000ff;">-Dwtpversion=2.0</span></em></p>
<p>Note that as of this writing, the supported versions of WTP are  1.0, 1.5, 2.0 and R7.</p>
<h2>Solution 2: Convert it to a Facted Project</h2>
<p>Right click on the project, go to <em>Properties</em>, then select <em>Project Facets</em>.</p>
<p><img class="aligncenter size-full wp-image-349" title="converttofacetedproject" src="http://puretech.paawak.com/wp-content/uploads/2010/08/converttofacetedproject.png" alt="converttofacetedproject" width="442" height="380" /></p>
<p>Another dialog appears having the available facets.</p>
<p><img class="aligncenter size-full wp-image-350" title="selectdynamicwebproject" src="http://puretech.paawak.com/wp-content/uploads/2010/08/selectdynamicwebproject.png" alt="selectdynamicwebproject" width="638" height="560" /></p>
<p>Select <em>Dynamic Web Module</em>.</p>
<h2>Solution 3: Manual editing</h2>
<p>I like this best as it gives lot of flexibility around configuration. First you need to modify the <em>.project</em> file as shown:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;projectDescription<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>MyWebProject<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;comment<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/comment<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;projects<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/projects<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;buildSpec<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;buildCommand<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.wst.jsdt.core.javascriptValidator<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/arguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/buildCommand<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;buildCommand<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.jdt.core.javabuilder<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/arguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/buildCommand<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;buildCommand<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.wst.common.project.facet.core.builder<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/arguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/buildCommand<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;buildCommand<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.wst.validation.validationbuilder<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;arguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
			<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/arguments<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/buildCommand<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/buildSpec<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;natures<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;nature<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.jem.workbench.JavaEMFNature<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/nature<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;nature<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.wst.common.modulecore.ModuleCoreNature<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/nature<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;nature<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.wst.common.project.facet.core.nature<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/nature<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;nature<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.jdt.core.javanature<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/nature<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;nature<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.eclipse.wst.jsdt.core.jsNature<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/nature<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/natures<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/projectDescription<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Then, inside the <em>.settings</em> directory, you need to create two files called <em>org.eclipse.wst.common.component</em> and <em>org.eclipse.wst.common.project.facet.core.xml</em>.</p>
<h3>org.eclipse.wst.common.component</h3>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project-modules</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;moduleCoreId&quot;</span> <span style="color: #000066;">project-version</span>=<span style="color: #ff0000;">&quot;1.5.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;wb-module</span> <span style="color: #000066;">deploy-name</span>=<span style="color: #ff0000;">&quot;MyWebProject&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;wb-resource</span> <span style="color: #000066;">deploy-path</span>=<span style="color: #ff0000;">&quot;/&quot;</span> <span style="color: #000066;">source-path</span>=<span style="color: #ff0000;">&quot;/src/main/webapp&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;wb-resource</span> <span style="color: #000066;">deploy-path</span>=<span style="color: #ff0000;">&quot;/WEB-INF/classes&quot;</span> <span style="color: #000066;">source-path</span>=<span style="color: #ff0000;">&quot;/src/main/java&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;context-root&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;MyWebProject&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;java-output-path&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;/MyWebProject/build/classes&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/wb-module<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project-modules<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3>org.eclipse.wst.common.project.facet.core.xml</h3>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;faceted-project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;runtime</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;Apache Tomcat v7.0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fixed</span> <span style="color: #000066;">facet</span>=<span style="color: #ff0000;">&quot;jst.web&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fixed</span> <span style="color: #000066;">facet</span>=<span style="color: #ff0000;">&quot;wst.jsdt.web&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fixed</span> <span style="color: #000066;">facet</span>=<span style="color: #ff0000;">&quot;java&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;installed</span> <span style="color: #000066;">facet</span>=<span style="color: #ff0000;">&quot;java&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.6&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;installed</span> <span style="color: #000066;">facet</span>=<span style="color: #ff0000;">&quot;jst.web&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;3.0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;installed</span> <span style="color: #000066;">facet</span>=<span style="color: #ff0000;">&quot;wst.jsdt.web&quot;</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/faceted-project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://puretech.paawak.com/2010/08/28/how-to-convert-a-maven-project-to-eclipse-web-project/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Creating an EAR with Maven</title>
		<link>http://puretech.paawak.com/2010/08/22/creating-an-ear-with-maven/</link>
		<comments>http://puretech.paawak.com/2010/08/22/creating-an-ear-with-maven/#comments</comments>
		<pubDate>Sat, 21 Aug 2010 19:20:20 +0000</pubDate>
		<dc:creator>paawak</dc:creator>
		
		<category><![CDATA[java]]></category>

		<category><![CDATA[ear]]></category>

		<category><![CDATA[maven]]></category>

		<guid isPermaLink="false">http://puretech.paawak.com/?p=315</guid>
		<description><![CDATA[Maven is the build tool of choice for many people due to the simplicity and flexibility of use. The real strength of Maven is vetted when handling real complex projetcs consisting of tens of modules, each requiring elaborate build requirements.
I had faced an uphill task of Mavenising one of our EAR projects. As is the [...]]]></description>
			<content:encoded><![CDATA[<p>Maven is the build tool of choice for many people due to the simplicity and flexibility of use. The real strength of Maven is vetted when handling real complex projetcs consisting of tens of modules, each requiring elaborate build requirements.</p>
<p>I had faced an uphill task of Mavenising one of our EAR projects. As is the typical case, our project consisted of a EJB module and a WAR module, package together in a EAR module. It was a bit complex as we had to deploy the same application in JBoss and Glassfish. I will write some of that experience here.</p>
<p>Let us consider an enterprise application having a structure as show below:</p>
<p><a rel="attachment wp-att-316" href="http://puretech.paawak.com/2010/08/22/creating-an-ear-with-maven/project-structure/"><img class="aligncenter size-full wp-image-316" title="Project Structure" src="http://puretech.paawak.com/wp-content/uploads/2010/08/project-structure.png" alt="Project Structure" width="340" height="332" /></a></p>
<p>I will briefly explain what the modules stand for:</p>
<ol>
<li><strong>swayam-ear</strong>: This is the enterprise application</li>
<li><strong>swayam-ejb</strong>: Is the EJB module</li>
<li><strong>swayam-war</strong>: Is the web module</li>
<li><strong>swayam-shared</strong>: Has the ejb remote interfaces. As the name indicates, its shared by the <em>sawaym-ejb</em> and the <em>swayam-war</em> modules</li>
<li><strong>swayam-ear-builder</strong>: Used for building all the modules</li>
</ol>
<p>I am using Netbeans (6.8) and Glassfish for convinience. But you can pretty much use anything.</p>
<p>I have kept things pretty simple. This is how the Remote interface looks like (its a Stateless Session Bean):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@<span style="color: #003399;">Remote</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">interface</span> MySessionBeanRemote <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #003399;">String</span> sayHello<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And this is how I access it from the servlet:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">...
<span style="color: #006633;">MySessionBeanRemote</span> remoteBean <span style="color: #339933;">=</span> <span style="color: #003399;">InitialContext</span>.<span style="color: #006633;">doLookup</span><span style="color: #009900;">&#40;</span>MySessionBeanRemote.<span style="color: #000000; font-weight: bold;">class</span>.<span style="color: #006633;">getName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
...</pre></div></div>

<h2>Mavenising the EJB</h2>
<p>We will use the <em>maven-ejb-plugin</em> for this. This is how the pom looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0&quot;</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.swayam.eardemo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-ejb<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ejb<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-ejb<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sourceDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${basedir}/src<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sourceDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.maven.plugins<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-jar-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-ejb-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ejbVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>3.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ejbVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;archive<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;manifest<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;addClasspath<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>true<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/addClasspath<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/manifest<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/archive<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.maven.plugins<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-compiler-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javaee-api<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>6.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>provided<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.swayam.eardemo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-shared<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Note that the packaging is <em>ejb</em> and not <em>jar</em>.</p>
<h2>Mavenising the WAR</h2>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0&quot;</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.swayam.eardemo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-war<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>war<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-war<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pluginRepositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pluginRepository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven.java.net<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Java.net Maven2 Repository<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://download.java.net/maven/2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pluginRepository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pluginRepositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;finalName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-war<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/finalName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;sourceDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${basedir}/src/java<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/sourceDirectory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.maven.plugins<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-compiler-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-war-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;webResources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;resource<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${basedir}/web<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/directory<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;excludes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;exclude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>CVS/**<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/exclude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;exclude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>WEB-INF/lib/*<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/exclude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/excludes<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/resource<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/webResources<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.swayam.eardemo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-shared<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>                
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>javax.servlet<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>servlet-api<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>provided<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/scope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>    
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h2>Mavenising the EAR</h2>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0&quot;</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">         <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.swayam.eardemo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-ear<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ear<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-ear<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pluginRepositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pluginRepository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven.java.net<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Java.net Maven2 Repository<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://download.java.net/maven/2<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pluginRepository<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pluginRepositories<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;finalName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-ear<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/finalName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.apache.maven.plugins<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-compiler-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/source<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.6<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/target<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-resources-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.3<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;encoding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>UTF-8<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/encoding<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>maven-ear-plugin<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2.4.1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;modules<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;webModule<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.swayam.eardemo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-war<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bundleFileName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-war.war<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bundleFileName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;contextRoot<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/swayam-war<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/contextRoot<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/webModule<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ejbModule<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.swayam.eardemo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-ejb<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bundleFileName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-ejb.jar<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bundleFileName<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/ejbModule<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/modules<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugin<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plugins<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/build<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.swayam.eardemo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-war<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${swayam-version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>war<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.swayam.eardemo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-ejb<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>${swayam-version}<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ejb<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/type<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependencies<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;swayam-version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/swayam-version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/properties<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>The most important thing here is the <em>type</em> tag inside the <em>dependency</em> tag. Without this, it will not work.</p>
<h2>Putting it all together</h2>
<p>Its often very cumbersome to build these modules one by one manually when one of them changes. This is more so in a development environment. So, I will conclude with one pom for building all the modules at one go. This pom is present in the <em>swayam-ear-builder</em> module.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;project</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0&quot;</span> <span style="color: #000066;">xmlns:xsi</span>=<span style="color: #ff0000;">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></span>
<span style="color: #009900;">  <span style="color: #000066;">xsi:schemaLocation</span>=<span style="color: #ff0000;">&quot;http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>4.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/modelVersion<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>com.swayam.eardemo<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-ear-builder<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>pom<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/packaging<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>swayam-ear-builder<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;modules<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;module<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>../swayam-ear<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/module<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;module<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>../swayam-ejb<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/module<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;module<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>../swayam-war<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/module<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;module<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>../swayam-shared<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/module<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/modules<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/project<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Note that the path of the modules are relative to the pom.<br />
You can build the EAR project and deploy it on Glassfish or JBoss. Once successfully deployed, you can open <a href="http://localhost:8080/swayam-war/EjbInvoker">http://localhost:8080/swayam-war/EjbInvoker</a> This is how it looks like:</p>
<p><a rel="attachment wp-att-326" href="http://puretech.paawak.com/2010/08/22/creating-an-ear-with-maven/screenshot-servlet-ejbinvoker-google-chrome/"><img class="aligncenter size-full wp-image-326" title="screenshot-servlet-ejbinvoker-google-chrome" src="http://puretech.paawak.com/wp-content/uploads/2010/08/screenshot-servlet-ejbinvoker-google-chrome.png" alt="screenshot-servlet-ejbinvoker-google-chrome" width="620" height="235" /></a></p>
<h2>Resources</h2>
<ol>
<li><a href="http://puretech.paawak.com/resources/java/maven/ear-demo/swayam-ear-all-sources.zip">Sources</a></li>
<li><a href="http://puretech.paawak.com/resources/java/maven/ear-demo/swayam-ear.ear">Binaries</a></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://puretech.paawak.com/2010/08/22/creating-an-ear-with-maven/feed/</wfw:commentRss>
		</item>
		<item>
		<title>[How To] Configure GTalk on Kopete</title>
		<link>http://puretech.paawak.com/2010/08/21/how-to-configure-gtalk-on-kopete/</link>
		<comments>http://puretech.paawak.com/2010/08/21/how-to-configure-gtalk-on-kopete/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 19:40:34 +0000</pubDate>
		<dc:creator>paawak</dc:creator>
		
		<category><![CDATA[linux]]></category>

		<category><![CDATA[im]]></category>

		<guid isPermaLink="false">http://puretech.paawak.com/?p=292</guid>
		<description><![CDATA[Kopete is one of the most versatile and cool Instant Messenger for Linux. Its lot better and more secure and feature rich than Pidgin. GTalk is based on the open source XMPP Protocol, Jabber compatible.
While entering the new account details, select Jabber:

Enter your full email id.
Go to the connection tab. Check the  Override default [...]]]></description>
			<content:encoded><![CDATA[<p>Kopete is one of the most versatile and cool Instant Messenger for Linux. Its lot better and more secure and feature rich than Pidgin. GTalk is based on the open source XMPP Protocol, Jabber compatible.</p>
<p>While entering the new account details, select Jabber:</p>
<p><a rel="attachment wp-att-293" href="http://puretech.paawak.com/2010/08/21/how-to-configure-gtalk-on-kopete/kopete_gtalk_1/" target="_blank"></a><a rel="attachment wp-att-293" href="http://puretech.paawak.com/2010/08/21/how-to-configure-gtalk-on-kopete/kopete_gtalk_1/"><img class="aligncenter size-full wp-image-293" title="Select type as &quot;Jabber&quot;" src="http://puretech.paawak.com/wp-content/uploads/2010/08/kopete_gtalk_1.png" alt="Select type as &quot;Jabber&quot;" width="940" height="573" /></a></p>
<p>Enter your full email id.</p>
<p style="margin-bottom: 0in;">Go to the connection tab. Check the  <em><strong>Override default server information</strong></em> check-box and enter the server as t<em><strong>alk.google.com</strong></em>, the port as <em><strong>5223.</strong></em> Also check these check boxes:</p>
<ol>
<li>
<p style="margin-bottom: 0in;">Use protocol encryption</p>
</li>
<li>Allow plain text password authentication</li>
</ol>
<p><a rel="attachment wp-att-294" href="http://puretech.paawak.com/2010/08/21/how-to-configure-gtalk-on-kopete/kopete_gtalk_3/"><img class="aligncenter size-full wp-image-294" title="Connection details" src="http://puretech.paawak.com/wp-content/uploads/2010/08/kopete_gtalk_3.png" alt="Connection details" width="938" height="580" /></a></p>
<p>Save this information and you should be done.</p>
<p style="margin-bottom: 0in;">
]]></content:encoded>
			<wfw:commentRss>http://puretech.paawak.com/2010/08/21/how-to-configure-gtalk-on-kopete/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Tweaking MySQL on Fedora</title>
		<link>http://puretech.paawak.com/2010/08/21/tweaking-mysql-on-fedora/</link>
		<comments>http://puretech.paawak.com/2010/08/21/tweaking-mysql-on-fedora/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 19:27:36 +0000</pubDate>
		<dc:creator>paawak</dc:creator>
		
		<category><![CDATA[database]]></category>

		<category><![CDATA[linux]]></category>

		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://puretech.paawak.com/?p=290</guid>
		<description><![CDATA[MySQL is installed on Fedora and most Linuxes by default. Its just about some tweaking before you can use it. I am detailing some of the rather useful commands.
To Install MySQL and start it
mysql_install_db
mysqld_safe &#38;
Make MySQL case insensitive
This is useful when the DB Script is also expected to run on Windows server.
vi /etc/my.cnf

[mysqld]
lower_case_table_names=1
To change the [...]]]></description>
			<content:encoded><![CDATA[<p>MySQL is installed on Fedora and most Linuxes by default. Its just about some tweaking before you can use it. I am detailing some of the rather useful commands.</p>
<h2>To Install MySQL and start it</h2>
<p><span style="color: #000080;">mysql_install_db<br />
mysqld_safe &amp;</span></p>
<h2>Make MySQL case insensitive</h2>
<p>This is useful when the DB Script is also expected to run on Windows server.</p>
<p>vi /etc/my.cnf<br />
<span style="color: #000080;"><br />
[mysqld]<br />
lower_case_table_names=1</span></p>
<h2>To change the root password</h2>
<p><strong><span style="color: #000080;">mysql&gt;</span></strong></p>
<p>update user set password=password(&#8221;newPassword&#8221;)  where user=&#8217;root&#8217;;<br />
Query OK, 2 rows affected (0.00 sec)<br />
Rows matched: 2  Changed: 2  Warnings: 0</p>
<p>grant all on *.* to &#8216;root&#8217;@'192.168.%&#8217; identified by &#8216;newPassword&#8217;;</p>
<p>FLUSH PRIVILEGES;</p>
<h2>Adding a user</h2>
<p><strong><span style="color: #000080;">mysql&gt;</span></strong></p>
<p>insert into user (host, user, password) values(&#8217;localhost&#8217;,'newUser&#8217;,password(&#8217;xx123&#8242;));</p>
<p>insert into  host(host,db,Select_priv, Insert_priv, Update_priv, Delete_priv, Create_priv, Drop_priv)  values(&#8217;localhost&#8217;,'dbName&#8217;,'Y&#8217;,'Y&#8217;,'Y&#8217;,'Y&#8217;,'Y&#8217;,'Y&#8217;);</p>
<p>grant all on dbName.* to &#8216;newUser&#8217;@'localhost&#8217; identified by &#8216;xx123&#8242;;</p>
<p>FLUSH PRIVILEGES;</p>
]]></content:encoded>
			<wfw:commentRss>http://puretech.paawak.com/2010/08/21/tweaking-mysql-on-fedora/feed/</wfw:commentRss>
		</item>
		<item>
		<title>[How To] Enable VPN on Linux by PPTP</title>
		<link>http://puretech.paawak.com/2010/08/21/how-to-enable-vpn-on-linux-by-pptp/</link>
		<comments>http://puretech.paawak.com/2010/08/21/how-to-enable-vpn-on-linux-by-pptp/#comments</comments>
		<pubDate>Fri, 20 Aug 2010 19:11:40 +0000</pubDate>
		<dc:creator>paawak</dc:creator>
		
		<category><![CDATA[linux]]></category>

		<category><![CDATA[pptp]]></category>

		<category><![CDATA[vpn]]></category>

		<guid isPermaLink="false">http://puretech.paawak.com/?p=288</guid>
		<description><![CDATA[PPTP is an wonderful utility to enable VPN on a Linux box. Its secure and compatible with Windows network. I first heard about PPTP from (this wonderful guy called) Nikolaj. I have learnt how to set it up from him.
I am detailing the steps here:
1&#62; Install PPTP

yum install pptp
2&#62; vi /etc/ppp/options.pptp 

###############################################################################
# $Id: options.pptp,v 1.2 [...]]]></description>
			<content:encoded><![CDATA[<p>PPTP is an wonderful utility to enable VPN on a Linux box. Its secure and compatible with Windows network. I first heard about PPTP from (this wonderful guy called) Nikolaj. I have learnt how to set it up from him.</p>
<p>I am detailing the steps here:</p>
<h2>1&gt; Install PPTP</h2>
<p><!-- 		@page { margin: 0.79in } 		P { margin-bottom: 0.08in } --></p>
<p style="margin-bottom: 0in; font-style: normal; font-weight: normal;"><span style="font-family: Liberation Serif,Times New Roman,serif;">yum install pptp</span></p>
<h2 style="margin-bottom: 0in;"><span style="color: #4700b8;"><span style="color: #000000;">2&gt; vi /etc/ppp/options.pptp </span></span></h2>
<p style="margin-bottom: 0in;"><!-- 		@page { margin: 0.79in } 		P { margin-bottom: 0.08in } --></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">###############################################################################</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># $Id: options.pptp,v 1.2 2005/08/20 13:16:38 quozl Exp $</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">#</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># Sample PPTP PPP options file /etc/ppp/options.pptp</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># Options used by PPP when a connection is made by a PPTP client.</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># This file can be referred to by an /etc/ppp/peers file for the tunnel.</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># Changes are effective on the next connection.  See &#8220;man pppd&#8221;.</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">#</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># You are expected to change this file to suit your system.  As</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># packaged, it requires PPP 2.4.2 or later from http://ppp.samba.org/</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># and the kernel MPPE module available from the CVS repository also on</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># http://ppp.samba.org/, which is packaged for DKMS as kernel_ppp_mppe.</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">###############################################################################</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"><br />
</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># Lock the port</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">lock</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># Authentication</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># We don&#8217;t need the tunnel server to authenticate itself</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">noauth</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">persist </span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">debug</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># We won&#8217;t do EAP, CHAP, or MSCHAP, but we will accept MSCHAP-V2</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">refuse-eap</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">refuse-chap</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">refuse-mschap</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"><br />
</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># Compression</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># Turn off compression protocols we know won&#8217;t be used</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">nobsdcomp</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">nodeflate</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"><br />
</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># Encryption</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># (There have been multiple versions of PPP with encryption support,</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># choose with of the following sections you will use.  Note that MPPE</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># requires the use of MSCHAP-V2 during authentication)</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"><br />
</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># http://ppp.samba.org/ the PPP project version of PPP by Paul Mackarras</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># ppp-2.4.2 or later with MPPE only, kernel module ppp_mppe.o</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># {{{</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># Require MPPE 128-bit encryption</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">#require-mppe-128</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># }}}</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"><br />
</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># http://polbox.com/h/hs001/ fork from PPP project by Jan Dubiec</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># ppp-2.4.2 or later with MPPE and MPPC, kernel module ppp_mppe_mppc.o</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># {{{</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># Require MPPE 128-bit encryption</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">#mppe required,stateless</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"># }}}</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;"><br />
</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">lcp-echo-failure 36 </span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">lcp-echo-interval 5</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">lcp-max-failure 0</span></p>
<h2 style="margin-bottom: 0in; font-style: normal; font-weight: normal;"><span style="font-family: Liberation Serif,Times New Roman,serif;">3&gt; vi /etc/ppp/chap-secrets</span></h2>
<p style="margin-bottom: 0in; font-style: normal; font-weight: normal;"><!-- 		@page { margin: 0.79in } 		P { margin-bottom: 0.08in } --></p>
<p style="margin-bottom: 0in;"><span style="color: #0000ff;"># Secrets for authentication using CHAP</span></p>
<p style="margin-bottom: 0in;"><span style="color: #0000ff;"># client                 	server       	secret                 	     		IP addresses</span></p>
<p style="margin-bottom: 0in;"><span style="color: #0000ff;"><br />
</span></p>
<p style="margin-bottom: 0in;"><span style="color: #0000ff;">VPNUserName      PPTP       VPNPassword         *</span></p>
<h2 style="margin-bottom: 0in;"><span style="color: #4700b8;"><span style="color: #000000;">4&gt; Create a file called /etc/ppp/peers/my-company-vpn</span></span></h2>
<p style="margin-bottom: 0in;"><!-- 		@page { margin: 0.79in } 		P { margin-bottom: 0.08in } --></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">#pty &#8220;pptp my-company.com &#8211;nolaunchpppd&#8221;</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">name VPNUserName</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">remotename PPTP</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">require-mppe-128</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">file /etc/ppp/options.pptp</span></p>
<p style="margin-bottom: 0in;"><span style="color: #333399;">ipparam my-company-vpn</span></p>
<h2 style="margin-bottom: 0in;"><span style="color: #4700b8;"><span style="color: #000000;">5&gt; Then on the prompt:</span></span></h2>
<p style="margin-bottom: 0in;"><!-- 		@page { margin: 0.79in } 		P { margin-bottom: 0.08in } --></p>
<p style="margin-bottom: 0in; padding-left: 30px;">pptp my-company.com call <span style="color: #4700b8;"><span style="color: #000000;">my-company-vpn</span></span></p>
<h2 style="margin-bottom: 0in;"><span style="color: #4700b8;"><span style="color: #000000;">6&gt; After 10/15 seconds, on the prompt:</span></span></h2>
<p style="margin-bottom: 0in; padding-left: 30px;"><span style="color: #4700b8;"><span style="color: #000000;">route -n</span></span></p>
<p style="margin-bottom: 0in;"><span style="color: #4700b8;"><span style="color: #000000;">You should see something like:</span></span></p>
<p style="margin-bottom: 0in;"><!-- 		@page { margin: 0.79in } 		P { margin-bottom: 0.08in } --></p>
<p style="margin-bottom: 0in; padding-left: 30px;"><span style="color: #333399;">Kernel IP routing table </span></p>
<p style="margin-bottom: 0in; padding-left: 30px;"><span style="color: #333399;">Destination     Gateway         Genmask         Flags Metric Ref    Use Iface </span></p>
<p style="margin-bottom: 0in; padding-left: 30px;"><span style="color: #333399;"><span style="color: #ff0000;">192.168.1.162</span><strong> </strong></span><span style="color: #333399;"> 0.0.0.0         255.255.255.255 UH    0      0        0 <em><strong>ppp0</strong></em></span></p>
<p style="margin-bottom: 0in; padding-left: 30px;"><span style="color: #333399;">87.61.21.102    192.168.1.1     255.255.255.255 UGH   0      0        0 eth0 </span></p>
<p style="margin-bottom: 0in; padding-left: 30px;"><span style="color: #333399;">192.168.1.0     0.0.0.0         255.255.255.0   U     1      0        0 eth0 </span></p>
<p style="margin-bottom: 0in;">If you see <em><strong>ppp0</strong></em>, it means you have successfully connected to the VPN.</p>
<h2 style="margin-bottom: 0in;"><span style="color: #4700b8;"><span style="color: #000000;">7&gt;</span></span> Add required routes</h2>
<p style="margin-bottom: 0in;"><!-- 		@page { margin: 0.79in } 		P { margin-bottom: 0.08in } --></p>
<p style="margin-bottom: 0in; padding-left: 30px;">route add -host 192.168.1.30 gw <span style="color: #ff0000;">192.168.1.162</span></p>
<p>Where <em>192.168.1.30</em> is my internal company IP.</p>
<h2>8&gt;    	 	 	 	 	After that, to resolve the domains by names</h2>
<p>vi /etc/resolv.conf</p>
<p style="margin-bottom: 0in; padding-left: 30px;"><span style="color: #333399;">#to use when connected to the VPN my-company.com</span></p>
<p style="margin-bottom: 0in; padding-left: 30px;"><span style="color: #333399;">domain my-office.my-company.com</span></p>
<p style="margin-bottom: 0in; padding-left: 30px;"><span style="color: #333399;">#this, is the most important line: courtesy: Nikolaj</span></p>
<p style="margin-bottom: 0in; padding-left: 30px;"><span style="color: #333399;">search my-office.my-company.com</span></p>
<p style="margin-bottom: 0in; padding-left: 30px;"><span style="color: #333399;">#nameserver 192.168.1.1</span></p>
<p style="margin-bottom: 0in; padding-left: 30px;"><span style="color: #333399;">nameserver 192.168.1.4</span></p>
<p style="margin-bottom: 0in; padding-left: 30px;"><span style="color: #333399;">nameserver 192.168.1.5</span></p>
<h2 style="margin-bottom: 0in;">Further reading:</h2>
<ul>
<li><a title="Nikolaj's original post on PPTP" href="http://blog.efef.dk/?p=92">Nikolaj&#8217;s original post</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://puretech.paawak.com/2010/08/21/how-to-enable-vpn-on-linux-by-pptp/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>

