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

<channel>
	<title>Joda-Time &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/joda-time/feed/" rel="self" type="application/rss+xml" />
	<link>https://myshittycode.com</link>
	<description>Embracing the Messiness in Search of Epic Solutions</description>
	<lastBuildDate>Fri, 06 Jan 2023 17:19:30 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.1</generator>

<image>
	<url>https://myshittycode.com/wp-content/uploads/2022/04/cropped-icon-32x32.png</url>
	<title>Joda-Time &#8211; My Shitty Code</title>
	<link>https://myshittycode.com</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">205304208</site>	<item>
		<title>Spring MVC: Handling Joda Data Types as JSON</title>
		<link>https://myshittycode.com/2015/03/28/spring-mvc-handling-joda-data-types-as-json/</link>
					<comments>https://myshittycode.com/2015/03/28/spring-mvc-handling-joda-data-types-as-json/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Sat, 28 Mar 2015 23:49:18 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Joda-Time]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[Spring]]></category>
		<category><![CDATA[Spring MVC]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=681</guid>

					<description><![CDATA[<p>PROBLEM Let&#8217;s assume we have the following bean that contains Joda&#8217;s LocalDate and LocalDateTime objects:- This simple Spring MVC rest controller creates this bean and returns the JSON data back to the client:- By default, the generated JSON looks like this:- How do we nicely format these values and still retain the correct data types [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2015/03/28/spring-mvc-handling-joda-data-types-as-json/">Spring MVC: Handling Joda Data Types as JSON</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>Let&#8217;s assume we have the following bean that contains Joda&#8217;s <b>LocalDate</b> and <b>LocalDateTime</b> objects:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
public class MyBean {
    private LocalDate date;
    private LocalDateTime dateTime;

    public LocalDate getDate() {
        return date;
    }

    public void setDate(LocalDate date) {
        this.date = date;
    }

    public LocalDateTime getDateTime() {
        return dateTime;
    }

    public void setDateTime(LocalDateTime dateTime) {
        this.dateTime = dateTime;
    }
}
</pre></div>


<p>This simple Spring MVC rest controller creates this bean and returns the JSON data back to the client:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
@RequestMapping(value = &quot;/joda&quot;, method = RequestMethod.GET)
public ResponseEntity joda() {
    MyBean myBean = new MyBean();
    myBean.setDate(LocalDate.now());
    myBean.setDateTime(LocalDateTime.now());

    return new ResponseEntity&lt;mybean&gt;(myBean, HttpStatus.OK);
}
</pre></div>


<p>By default, the generated JSON looks like this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
{
   &quot;date&quot;:
   &#x5B;
       2015,
       3,
       28
   ],
   &quot;dateTime&quot;:
   &#x5B;
       2015,
       3,
       28,
       18,
       12,
       58,
       992
   ]
}
</pre></div>


<p>How do we nicely format these values and still retain the correct data types (<b>LocalDate</b> and <b>LocalDateTime</b>) in <b>MyBean</b> instead of writing our custom formatter and store the values as <b>String</b>?</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>First, add a dependency for <b>jackson-datatype-joda</b>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [16,17,18,19,20]; title: ; notranslate">
&lt;dependency&gt;
    &lt;groupid&gt;com.fasterxml.jackson.core&lt;/groupid&gt;
    &lt;artifactid&gt;jackson-core&lt;/artifactid&gt;
    &lt;version&gt;2.5.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupid&gt;com.fasterxml.jackson.core&lt;/groupid&gt;
    &lt;artifactid&gt;jackson-databind&lt;/artifactid&gt;
    &lt;version&gt;2.5.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupid&gt;com.fasterxml.jackson.core&lt;/groupid&gt;
    &lt;artifactid&gt;jackson-annotations&lt;/artifactid&gt;
    &lt;version&gt;2.5.1&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupid&gt;com.fasterxml.jackson.datatype&lt;/groupid&gt;
    &lt;artifactid&gt;jackson-datatype-joda&lt;/artifactid&gt;
    &lt;version&gt;2.5.1&lt;/version&gt;
&lt;/dependency&gt;
</pre></div>


<p>Next, instruct <b>MappingJackson2HttpMessageConverter</b> to accept a custom <b>ObjectMapper</b>.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;bean id=&quot;objectMapper&quot; class=&quot;org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean&quot; p:indentoutput=&quot;true&quot; p:simpledateformat=&quot;yyyy-MM-dd&#039;T&#039;HH:mm:ss.SSSZ&quot;&gt;
&lt;/bean&gt;

&lt;bean class=&quot;org.springframework.beans.factory.config.MethodInvokingFactoryBean&quot; p:targetobject-ref=&quot;objectMapper&quot; p:targetmethod=&quot;registerModule&quot;&gt;
    &lt;property name=&quot;arguments&quot;&gt;
        &lt;list&gt;
            &lt;bean class=&quot;com.fasterxml.jackson.datatype.joda.JodaModule&quot;&gt;
        &lt;/bean&gt;&lt;/list&gt;
    &lt;/property&gt;
&lt;/bean&gt;

&lt;mvc:annotation-driven&gt;
    &lt;mvc:message-converters&gt;
        &lt;bean class=&quot;org.springframework.http.converter.StringHttpMessageConverter&quot;&gt;
        &lt;bean class=&quot;org.springframework.http.converter.ResourceHttpMessageConverter&quot;&gt;
        &lt;bean class=&quot;org.springframework.http.converter.json.MappingJackson2HttpMessageConverter&quot;&gt;
            &lt;property name=&quot;objectMapper&quot; ref=&quot;objectMapper&quot;&gt;
        &lt;/property&gt;&lt;/bean&gt;
    &lt;/bean&gt;&lt;/bean&gt;&lt;/mvc:message-converters&gt;
&lt;/mvc:annotation-driven&gt;
</pre></div>


<p>The generated JSON output now looks like this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
{
   &quot;date&quot;: &quot;2015-03-28&quot;,
   &quot;dateTime&quot;: &quot;2015-03-28T18:11:16.348&quot;
}
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2015/03/28/spring-mvc-handling-joda-data-types-as-json/">Spring MVC: Handling Joda Data Types as JSON</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2015/03/28/spring-mvc-handling-joda-data-types-as-json/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">681</post-id>	</item>
		<item>
		<title>Jadira Usertype: Under JDK 6 it may not be possible to handle DST transitions correctly</title>
		<link>https://myshittycode.com/2013/12/26/jadira-usertype-under-jdk-6-it-may-not-be-possible-to-handle-dst-transitions-correctly/</link>
					<comments>https://myshittycode.com/2013/12/26/jadira-usertype-under-jdk-6-it-may-not-be-possible-to-handle-dst-transitions-correctly/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Thu, 26 Dec 2013 14:51:54 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Jadira]]></category>
		<category><![CDATA[Joda-Time]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=277</guid>

					<description><![CDATA[<p>PROBLEM When saving a Hibernate entity that contains Joda Time object, Jadira UserType throws the following warning:- This warning occurs when using this version:- SOLUTION Upgrade Jadira Usertype to the latest version, and the problem goes away:-</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2013/12/26/jadira-usertype-under-jdk-6-it-may-not-be-possible-to-handle-dst-transitions-correctly/">Jadira Usertype: Under JDK 6 it may not be possible to handle DST transitions correctly</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">PROBLEM</h2>



<p>When saving a Hibernate entity that contains Joda Time object, Jadira UserType throws the following warning:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
&#x5B;WARN ] &#x5B;JavaTimeZoneWorkaroundHelper] &#x5B;&amp;lt;clinit&gt;:40] - Under JDK 6 it may not be possible
to handle DST transitions correctly
&#x5B;ERROR] &#x5B;JavaTimeZoneWorkaroundHelper] &#x5B;&amp;lt;clinit&gt;:42] - Running under a Zone that uses
daylight saving time. To avoid incorrect datetimes being stored during DST transition,
either update to JDK 7 or use a Timezone for the JDK without Daylight Saving Time
</pre></div>


<p>This warning occurs when using this version:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;dependency&gt;
    &lt;groupId&gt;org.jadira.usertype&lt;/groupId&gt;
    &lt;artifactId&gt;usertype.core&lt;/artifactId&gt;
    &lt;version&gt;3.0.0.GA&lt;/version&gt;
&lt;/dependency&gt;
</pre></div>


<h2 class="wp-block-heading">SOLUTION</h2>



<p>Upgrade Jadira Usertype to the latest version, and the problem goes away:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;dependency&gt;
    &lt;groupId&gt;org.jadira.usertype&lt;/groupId&gt;
    &lt;artifactId&gt;usertype.core&lt;/artifactId&gt;
    &lt;version&gt;3.1.0.CR10&lt;/version&gt;
&lt;/dependency&gt;
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2013/12/26/jadira-usertype-under-jdk-6-it-may-not-be-possible-to-handle-dst-transitions-correctly/">Jadira Usertype: Under JDK 6 it may not be possible to handle DST transitions correctly</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2013/12/26/jadira-usertype-under-jdk-6-it-may-not-be-possible-to-handle-dst-transitions-correctly/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">277</post-id>	</item>
		<item>
		<title>Hibernate + Joda Time: Auto Registering Type</title>
		<link>https://myshittycode.com/2013/12/25/hibernate-joda-time-auto-registering-type/</link>
					<comments>https://myshittycode.com/2013/12/25/hibernate-joda-time-auto-registering-type/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Thu, 26 Dec 2013 01:30:19 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Jadira]]></category>
		<category><![CDATA[Joda-Time]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=274</guid>

					<description><![CDATA[<p>OVERVIEW Jadira Usertype is required when using Joda Time with Hibernate 4. The Joda Time objects are annotated accordingly in the Hibernate entity, and they looks something like this:- PROBLEM This solution works, but it is rather tedious because I can never remember the actual @Type to write, thus I always end up copying and [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2013/12/25/hibernate-joda-time-auto-registering-type/">Hibernate + Joda Time: Auto Registering Type</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">OVERVIEW</h2>



<p><a href="http://jadira.sourceforge.net/usertype-userguide.html" target="_blank" rel="noopener">Jadira Usertype</a> is required when using Joda Time with Hibernate 4. The Joda Time objects are annotated accordingly in the Hibernate entity, and they looks something like this:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; highlight: [16,20]; title: ; notranslate">
@Entity
@Table(name = &quot;person&quot;)
public class Person implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = &quot;personId&quot;)
    private Long id;

    @Column
    private String name;

    @Column
    @Type(type = &quot;org.jadira.usertype.dateandtime.joda.PersistentLocalDate&quot;)
    private LocalDate birthDate;

    @Column
    @Type(type = &quot;org.jadira.usertype.dateandtime.joda.PersistentLocalDateTime&quot;)
    private LocalDateTime createdDatetime;

    // getters and setters
}
</pre></div>


<h2 class="wp-block-heading">PROBLEM</h2>



<p>This solution works, but it is rather tedious because I can never remember the actual <code>@Type</code> to write, thus I always end up copying and pasting it from past project code.</p>



<p>Further, that additional annotations clutter up my otherwise beautiful code.</p>



<h2 class="wp-block-heading">SOLUTION</h2>



<p>Jadira Usertype provides a clean way to auto register these Joda Time object types.</p>



<p>In the Hibernate configuration, add the following line:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [8]; title: ; notranslate">
&lt;bean id=&quot;sessionFactory&quot; class=&quot;org.springframework.orm.hibernate4.LocalSessionFactoryBean&quot;&gt;
    &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;/&gt;

    &lt;property name=&quot;hibernateProperties&quot;&gt;
        &lt;props&gt;
            &lt;prop key=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.SQLServerDialect&lt;/prop&gt;
            &lt;prop key=&quot;hibernate.show_sql&quot;&gt;false&lt;/prop&gt;
            &lt;prop key=&quot;jadira.usertype.autoRegisterUserTypes&quot;&gt;true&lt;/prop&gt;
        &lt;/props&gt;
    &lt;/property&gt;
    &lt;property name=&quot;packagesToScan&quot;&gt;
        &lt;list&gt;
            &lt;value&gt;com.choonchernlim.project.entity&lt;/value&gt;
        &lt;/list&gt;
    &lt;/property&gt;
&lt;/bean&gt;
</pre></div>


<p>Now all <code>@Type</code> annotations can be safely removed from the Hibernate entity:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
@Entity
@Table(name = &quot;person&quot;)
public class Person implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = &quot;personId&quot;)
    private Long id;

    @Column
    private String name;

    @Column
    private LocalDate birthDate;

    @Column
    private LocalDateTime createdDatetime;

    // getters and setters
}
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2013/12/25/hibernate-joda-time-auto-registering-type/">Hibernate + Joda Time: Auto Registering Type</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2013/12/25/hibernate-joda-time-auto-registering-type/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">274</post-id>	</item>
		<item>
		<title>Hibernate: Migrating from XML-Based Configuration to Annotation-Based Configuration</title>
		<link>https://myshittycode.com/2013/10/21/hibernate-migrating-from-xml-based-configuration-to-annotation-based-configuration/</link>
					<comments>https://myshittycode.com/2013/10/21/hibernate-migrating-from-xml-based-configuration-to-annotation-based-configuration/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Mon, 21 Oct 2013 19:30:54 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Joda-Time]]></category>
		<category><![CDATA[Maven]]></category>
		<category><![CDATA[Spring]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=153</guid>

					<description><![CDATA[<p>Overview At some point of time, as your project scope grows, the Hibernate mapping XML files are going to get to a point where it becomes very difficult to maintain. This is where the annotation-based configuration comes in. It took me a few years to convince myself that annotation-based configuration is the way to go. [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2013/10/21/hibernate-migrating-from-xml-based-configuration-to-annotation-based-configuration/">Hibernate: Migrating from XML-Based Configuration to Annotation-Based Configuration</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<h2 class="wp-block-heading">Overview</h2>



<p>At some point of time, as your project scope grows, the Hibernate mapping XML files are going to get to a point where it becomes very difficult to maintain. This is where the annotation-based configuration comes in. It took me a few years to convince myself that annotation-based configuration is the way to go. Change is hard, yet necessary.</p>



<p>This tutorial covers the following:-</p>



<ul class="wp-block-list">
<li>Upgrade Hibernate from 3.x to 4.x.</li>



<li>Configure Spring-Hibernate integration to replace XML-based configuration with annotation-based configuration.</li>



<li>Configure Joda-Time to work properly in Hibernate 4.</li>



<li>Activate <b>@Transactional</b> instead of relying on AOP-based transaction.</li>
</ul>



<h2 class="wp-block-heading">Maven Dependencies</h2>



<p>Change the Hibernate dependency version from 3.x to 4.x. On top of that, add <b>org.jadira.usertype:usertype.core:[version]</b> dependency to get <a href="http://www.joda.org/joda-time/" target="_blank" rel="noopener">Joda-Time</a> to work properly with Hibernate 4.</p>



<h3 class="wp-block-heading">Hibernate 3.x</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;dependency&gt;
    &lt;groupid&gt;org.hibernate&lt;/groupid&gt;
    &lt;artifactid&gt;hibernate-core&lt;/artifactid&gt;
    &lt;version&gt;3.6.9.Final&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupid&gt;joda-time&lt;/groupid&gt;
    &lt;artifactid&gt;joda-time&lt;/artifactid&gt;
    &lt;version&gt;2.3&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
	&lt;groupid&gt;joda-time&lt;/groupid&gt;
	&lt;artifactid&gt;joda-time-hibernate&lt;/artifactid&gt;
	&lt;version&gt;1.3&lt;/version&gt;
&lt;/dependency&gt;
</pre></div>


<h3 class="wp-block-heading">Hibernate 4.x</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [4,16,17,18,19,20]; title: ; notranslate">
&lt;dependency&gt;
    &lt;groupid&gt;org.hibernate&lt;/groupid&gt;
    &lt;artifactid&gt;hibernate-core&lt;/artifactid&gt;
    &lt;version&gt;4.2.6.Final&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupid&gt;joda-time&lt;/groupid&gt;
    &lt;artifactid&gt;joda-time&lt;/artifactid&gt;
    &lt;version&gt;2.3&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
	&lt;groupid&gt;joda-time&lt;/groupid&gt;
	&lt;artifactid&gt;joda-time-hibernate&lt;/artifactid&gt;
	&lt;version&gt;1.3&lt;/version&gt;
&lt;/dependency&gt;
&lt;dependency&gt;
    &lt;groupid&gt;org.jadira.usertype&lt;/groupid&gt;
    &lt;artifactid&gt;usertype.core&lt;/artifactid&gt;
    &lt;version&gt;3.1.0.CR8&lt;/version&gt;
&lt;/dependency&gt;
</pre></div>


<p>By the way, if you don&#8217;t know what Joda-Time is or haven&#8217;t use it by now, then you should be spanked by the Date God. With that added dependency, the <b>@Type</b> for date field will look a little different ( <a href="http://stackoverflow.com/questions/8974747/hibernate-4-and-joda-time" target="_blank" rel="noopener">see here for more info</a> ):-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; highlight: [7]; title: ; notranslate">
@Entity
@Table(name = &quot;MrMeow&quot;)
public class MrMeow implements Ideable, Serializable {
	...

	@Column
	@Type(type = &quot;org.jadira.usertype.dateandtime.joda.PersistentLocalDate&quot;)
	private LocalDate whenMeowed;
}
</pre></div>


<h2 class="wp-block-heading">Spring Integration</h2>



<h3 class="wp-block-heading">Hibernate 3.x</h3>



<p>This configuration uses Hibernate XML mapping files to configure mappings between the domain objects and the database tables. Further, AOP is used to configure the transaction.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;bean id=&quot;sessionFactory&quot; class=&quot;org.springframework.orm.hibernate3.LocalSessionFactoryBean&quot;&gt;
    &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;&gt;
    &lt;property name=&quot;configLocation&quot; value=&quot;classpath:hibernate.cfg.xml&quot;&gt;
    &lt;/property&gt;&lt;/property&gt;
&lt;/bean&gt;

&lt;bean id=&quot;transactionManager&quot; class=&quot;org.springframework.orm.hibernate3.HibernateTransactionManager&quot;&gt;
    &lt;property name=&quot;sessionFactory&quot; ref=&quot;sessionFactory&quot;&gt;
&lt;/property&gt;&lt;/bean&gt;

&lt;tx:advice id=&quot;txAdvice&quot;&gt;
    &lt;tx:attributes&gt;
        &lt;tx:method name=&quot;*&quot; propagation=&quot;REQUIRED&quot;&gt;
    &lt;/tx:method&gt;&lt;/tx:attributes&gt;
&lt;/tx:advice&gt;

&lt;aop:config proxy-target-class=&quot;true&quot;&gt;
    &lt;aop:advisor pointcut=&quot;execution(* myproject..*.*(..))&quot; advice-ref=&quot;txAdvice&quot;&gt;
&lt;/aop:advisor&gt;&lt;/aop:config&gt;
</pre></div>


<h3 class="wp-block-heading">Hibernate 4.x</h3>



<p>This configuration scans <b>myproject.domain</b> package, which contains all Hibernate annotated domain classes. Further, it activates <b>@Transactional</b> so that you can place it in your code to manage the transaction.</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;tx:annotation-driven transaction-manager=&quot;transactionManager&quot;/&gt;
    
&lt;bean id=&quot;transactionManager&quot; class=&quot;org.springframework.orm.hibernate4.HibernateTransactionManager&quot;&gt;
    &lt;property name=&quot;sessionFactory&quot; ref=&quot;sessionFactory&quot;/&gt;
&lt;/bean&gt;

&lt;bean id=&quot;sessionFactory&quot; class=&quot;org.springframework.orm.hibernate4.LocalSessionFactoryBean&quot;&gt;
    &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;/&gt;

    &lt;property name=&quot;hibernateProperties&quot;&gt;
        &lt;props&gt;
			&lt;!-- Configure your Hibernate dialect  --&gt;
            &lt;prop key=&quot;hibernate.dialect&quot;&gt;org.hibernate.dialect.SQLServerDialect&lt;/prop&gt;
            &lt;prop key=&quot;hibernate.show_sql&quot;&gt;false&lt;/prop&gt;
        &lt;/props&gt;
    &lt;/property&gt;
    &lt;property name=&quot;packagesToScan&quot;&gt;
        &lt;list&gt;
			&lt;!-- Configure your domain package --&gt;
            &lt;value&gt;myproject.domain&lt;/value&gt;
        &lt;/list&gt;
    &lt;/property&gt;
&lt;/bean&gt;
</pre></div>


<h2 class="wp-block-heading">web.xml</h2>



<p>Change filter class for <b>OpenSessionInViewFilter</b>.</p>



<h3 class="wp-block-heading">Hibernate 3.x</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
&lt;filter&gt;
    &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt;
	&lt;filter-class&gt;org.springframework.orm.hibernate3.support.OpenSessionInViewFilter&lt;/filter-class&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
    &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt;
    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
</pre></div>


<h3 class="wp-block-heading">Hibernate 4.x</h3>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; highlight: [3]; title: ; notranslate">
&lt;filter&gt;
    &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt;
    &lt;filter-class&gt;org.springframework.orm.hibernate4.support.OpenSessionInViewFilter&lt;/filter-class&gt;
&lt;/filter&gt;
&lt;filter-mapping&gt;
    &lt;filter-name&gt;hibernateFilter&lt;/filter-name&gt;
    &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
</pre></div>


<h2 class="wp-block-heading">Domain Classes</h2>



<p>Finally, time to annotate your Hibernate domain classes!</p>



<h2 class="wp-block-heading">Helpful Note</h2>



<p>Please make sure you don&#8217;t have JPA 1.x in your classpath in the first place. Read <a title="java.lang.NoSuchMethodError:&nbsp;javax/persistence/OneToMany.orphanRemoval()Z" href="http://myshittycode.com/2013/10/04/java-lang-nosuchmethoderror-javaxpersistenceonetomany-orphanremovalz/">java.lang.NoSuchMethodError: javax/persistence/OneToMany.orphanRemoval()Z</a> for more information.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2013/10/21/hibernate-migrating-from-xml-based-configuration-to-annotation-based-configuration/">Hibernate: Migrating from XML-Based Configuration to Annotation-Based Configuration</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2013/10/21/hibernate-migrating-from-xml-based-configuration-to-annotation-based-configuration/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">153</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 
Lazy Loading (feed)
Database Caching using Disk

Served from: myshittycode.com @ 2026-03-03 06:27:20 by W3 Total Cache
-->