<?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>Hibernate &#8211; My Shitty Code</title>
	<atom:link href="https://myshittycode.com/tag/hibernate/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>Hibernate &#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>MS SQL Server + Hibernate 5: Incorrect syntax near &#8216;@P0&#8217;</title>
		<link>https://myshittycode.com/2017/06/20/ms-sql-server-hibernate-5-incorrect-syntax-near-p0/</link>
					<comments>https://myshittycode.com/2017/06/20/ms-sql-server-hibernate-5-incorrect-syntax-near-p0/#comments</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Tue, 20 Jun 2017 16:07:49 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[Hibernate]]></category>
		<category><![CDATA[MS SQL Server]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=1048</guid>

					<description><![CDATA[<p>PROBLEM When upgrading to Hibernate 5, the following exception is thrown:- SOLUTION Change the MS SQL Server dialect from this&#8230; &#8230; to this &#8230;</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2017/06/20/ms-sql-server-hibernate-5-incorrect-syntax-near-p0/">MS SQL Server + Hibernate 5: Incorrect syntax near &#8216;@P0&#8217;</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 upgrading to Hibernate 5, the following exception is thrown:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
Caused by: java.sql.SQLException: Incorrect syntax near &#039;@P0&#039;.
	at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372) ~&#x5B;jtds-1.3.1.jar:1.3.1]
	at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2988) ~&#x5B;jtds-1.3.1.jar:1.3.1]
	at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2421) ~&#x5B;jtds-1.3.1.jar:1.3.1]
</pre></div>


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



<p>Change the MS SQL Server dialect from this&#8230;</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
org.hibernate.dialect.SQLServerDialect
</pre></div>


<p>&#8230; to this &#8230;</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
org.hibernate.dialect.SQLServer2012Dialect
</pre></div><p>The post <a rel="nofollow" href="https://myshittycode.com/2017/06/20/ms-sql-server-hibernate-5-incorrect-syntax-near-p0/">MS SQL Server + Hibernate 5: Incorrect syntax near &#8216;@P0&#8217;</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2017/06/20/ms-sql-server-hibernate-5-incorrect-syntax-near-p0/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1048</post-id>	</item>
		<item>
		<title>Developing against H2 Database: Lesson Learned</title>
		<link>https://myshittycode.com/2014/01/09/developing-against-h2-database-lesson-learned/</link>
					<comments>https://myshittycode.com/2014/01/09/developing-against-h2-database-lesson-learned/#respond</comments>
		
		<dc:creator><![CDATA[Shitty Author]]></dc:creator>
		<pubDate>Thu, 09 Jan 2014 21:33:53 +0000</pubDate>
				<category><![CDATA[Programming Language]]></category>
		<category><![CDATA[H2]]></category>
		<category><![CDATA[Hibernate]]></category>
		<guid isPermaLink="false">http://myshittycode.com/?p=310</guid>

					<description><![CDATA[<p>While my production database is MS SQL Server, I have been using H2 database for rapid local development. I wrote my DDL scripts as &#8220;Transact-SQL&#8221; as possible so that I don&#8217;t need to maintain multiple DDL scripts for each database. So far, it has been working out great. My web development runs against H2&#8217;s file-based [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2014/01/09/developing-against-h2-database-lesson-learned/">Developing against H2 Database: Lesson Learned</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>While my production database is MS SQL Server, I have been using H2 database for rapid local development. I wrote my DDL scripts as &#8220;Transact-SQL&#8221; as possible so that I don&#8217;t need to maintain multiple DDL scripts for each database. So far, it has been working out great. My web development runs against H2&#8217;s file-based database because I want my data to persist until I manually wipe them off.</p>



<p>That said, I also realized that H2 does behavior a little differently from MS SQL Server. For example, I have the following Hibernate&#8217;s HQL query:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
public Collection&lt;Request&gt; getRequestsWithMatchingStatus(Collection&lt;String&gt; statusNames) {
    return sessionFactory.getCurrentSession()
            .createQuery(&quot;from Request r where r.status.name in (:statusNames)&quot;)
            .setParameterList(&quot;statusNames&quot;, statusNames)
            .list();
}
</pre></div>


<p>If <code>statusNames</code> is an empty collection, H2 will work just fine. However, MS SQL Server will throw the following exception:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
Caused by: java.sql.SQLException: Incorrect syntax near &#039;)&#039;.
	at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368)
	at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2816)
	at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2254)
</pre></div>


<p>My test cases didn&#8217;t catch this error because they run against H2&#8217;s in-memory database.</p>



<p>Of course, the fix is as easy as performing a check on the collection size first:-</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: java; title: ; notranslate">
public Collection&lt;Request&gt; getRequestsWithMatchingStatus(Collection&lt;String&gt; statusNames) {
    if (statusNames.isEmpty()) {
        return new ArrayList&lt;Request&gt;();
    }&lt;/code&gt;
&lt;code&gt;
&lt;/code&gt;
&lt;code&gt;    return sessionFactory.getCurrentSession()
            .createQuery(&quot;from Request r where r.status.name in (:statusNames)&quot;)
            .setParameterList(&quot;statusNames&quot;, statusNames)
            .list();
}
</pre></div>


<p>That said, H2 database allows me to do local development really fast, but I also need to make sure I test it against MS SQL Server from time to time before deploying into production.</p>
<p>The post <a rel="nofollow" href="https://myshittycode.com/2014/01/09/developing-against-h2-database-lesson-learned/">Developing against H2 Database: Lesson Learned</a> appeared first on <a rel="nofollow" href="https://myshittycode.com">My Shitty Code</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://myshittycode.com/2014/01/09/developing-against-h2-database-lesson-learned/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">310</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-02-18 05:36:37 by W3 Total Cache
-->