<?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>OcpSoft &#187; Hibernate</title> <atom:link href="http://ocpsoft.com/cat/java/hibernate/feed/" rel="self" type="application/rss+xml" /><link>http://ocpsoft.com</link> <description>&#34;Simple Software&#34;</description> <lastBuildDate>Tue, 27 Jul 2010 15:07:50 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0</generator> <item><title>Getting Started Quickly with Hibernate Annotations</title><link>http://ocpsoft.com/java/getting-started-quickly-with-hibernate-annotations/</link> <comments>http://ocpsoft.com/java/getting-started-quickly-with-hibernate-annotations/#comments</comments> <pubDate>Mon, 26 Jan 2009 14:08:04 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[Hibernate]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[Annotations]]></category> <category><![CDATA[ORM]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=5</guid> <description><![CDATA[Tutorial Chapter 1 &#8211; Step By Step Getting started with Hibernate can be tricky, but here&#8217;s a step-by-step tutorial that explains exactly what needs to be done to enable your application for hibernate. This chapter covers very basic mapping and persistence. Download the following archives: (or use this Maven POM) Hibernate Core (3.3.0_CR1) Hibernate Annotations [...]]]></description> <content:encoded><![CDATA[<h2>Tutorial Chapter 1 &#8211; Step By Step</h2><p>Getting started with Hibernate can be tricky, but here&#8217;s a step-by-step tutorial that explains exactly what needs to be done to enable your application for hibernate. This chapter covers very basic mapping and persistence.</p><h3>Download the following archives: (or use <a
href="#comment-259">this Maven POM</a>)</h3><ul><li>Hibernate Core <a
href="http://sourceforge.net/project/showfiles.php?group_id=40712&amp;package_id=127784&amp;release_id=595668" target="_blank">(3.3.0_CR1)</a></li><li>Hibernate Annotations <a
href="http://sourceforge.net/project/showfiles.php?group_id=40712&amp;package_id=139933" target="_blank">(3.4.0_CR1)</a></li><li>HSQLDB (<a
href="http://sourceforge.net/project/showfiles.php?group_id=23316&amp;package_id=16653&amp;release_id=339171" target="_blank">1.8.0.10</a>), a lightweight in-memory RDBMS (or use the database of your choice)</li></ul><p><span
id="more-5"></span></p><h3><a
name="instructions"></a>Instructions:</h3><ul><li>Extract the archives and copy necessary JAR files into your project (or if you are using Maven, this is already taken care for you -> <a
href="#comment-259">example POM</a>):<br
/> <em>It may seem like quite a few jars, it might not, but this is the minimal set to have a working Hibernate Annotations 3.4.0 application. All of the following files should be contained in your downloads.<br
/> </em></li></ul><pre style="padding-left: 90px;"><strong>Hibernate Annotations</strong>
hibernate-annotations.jar
hibernate-commons-annotations.jar
jta.jar (<a href="http://en.wikipedia.org/wiki/Java_Transaction_API" target="_blank">JTA, javax.transaction</a>)

<strong>Hibernate Core</strong>
hibernate-core.jar (or hibernate3.jar)
ejb3-persistence.jar (<a href="http://java.sun.com/developer/technicalArticles/J2EE/jpa/" target="_blank">javax.persistence</a>)
antlr.jar (<a href="http://antlr.org/" target="_blank">2.7.6</a>)
commons-collections.jar (<a href="http://commons.apache.org/collections/" target="_blank">3.1</a>)
dom4j.jar (<a href="http://www.dom4j.org/" target="_blank">1.6.1</a>)
slf4j-api.jar (<a href="http://www.slf4j.org/" target="_blank">1.4.2</a>)
cglib.jar (<a href="http://cglib.sourceforge.net/" target="_blank">v2</a>)
asm.jar (<a href="http://asm.objectweb.org/" target="_blank">v3</a>)

<strong>HSQLDB
</strong>hsqldb.jar</pre><ul><li>Add the JARs to your project&#8217;s classpath.</li><li>Copy and paste the following source files into your project. We&#8217;re going to use some canned configurations in order to get all of this working.</li></ul><div
class="featured"><center>Need some <strong>/pretty /urls</strong> in your JSF web-app? Try <a
href="http://ocpsoft.com/prettyfaces/" title="SEO | Dynamic Parameters | Bookmarks | Navigation | JSF | JSF2 | URLRewrite Filter | Open Source">PrettyFaces</a>: URL-rewriting for Java EE and JSF. (Free and <strong>open-source</strong>!)</center></div><h4>log4j.properties</h4><p>(Optional) Put this file on your classpath, or in your source folder. Hibernate uses log4j to print output, so if we want to see detailed results, you should set this up.</p><div
class="wp_syntax"><div
class="code"><pre class="config" style="font-family:monospace;">log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
&nbsp;
log4j.rootLogger=warn, stdout
log4j.logger.org.hibernate=info
log4j.logger.org.hibernate.type=info
log4j.logger.org.hibernate.tool.hbm2ddl=debug
log4j.logger.org.hibernate.transaction=debug</pre></div></div><h4 style="text-align: center;">&#8212;-</h4><h4>MockObject.java</h4><p>It&#8217;s time to create our first object. Notice that this is a simple POJO with two fields: id and version. The <em>id</em> field is a required field, but before you complain, there is a philosophy of database design that dictates every entity table should have its own <em>id</em> column; some disagree with this, but I&#8217;ve found it a very convenient practice. Try it out before you pass judgment.</p><p>In order to map an object, a few things always need to be done:</p><ul><li>The class must be annotated with <em>@javax.persistence.Entity</em></li><li>The class must have a <em>@javax.persistence.Id </em>column</li><li>The class must have a default (or undefined) constructor method. This means it must have a constructor that has no arguments.</li><li>The <em>@Version</em> column is how Hibernate performs <a
href="http://en.wikipedia.org/wiki/Optimistic_concurrency_control" target="_blank">optimistic locking</a>. If a record has been modified by another transaction, your transaction&#8217;s version will be out of date, and an exception will be thrown.</li><li>By default, all fields in your class will be persisted to the database if possible. If there is a field that should not be recorded in the database, mark that field with the <em>@Transient</em> annotation.</li><li>To add new columns in the database, simply add new fields in your object.</li></ul><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Serializable</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Column</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Entity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.GeneratedValue</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.GenerationType</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Id</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Table</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Version</span><span style="color: #339933;">;</span>
&nbsp;
@<span style="color: #003399;">Entity</span>
@Table<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;mock_objects&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> MockObject <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span>
<span style="color: #009900;">&#123;</span>
    @Id
    @GeneratedValue<span style="color: #009900;">&#40;</span>strategy <span style="color: #339933;">=</span> GenerationType.<span style="color: #006633;">AUTO</span><span style="color: #009900;">&#41;</span>
    @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;id&quot;</span>, updatable <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span>, nullable <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> id <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    @Version
    @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;version&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> version <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">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;">int</span> getVersion<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> version<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><h4 style="text-align: center;">&#8212;-</h4><h4>HibernateUtil.java</h4><p>HibernateUtil is a convenience class that automates configuration, and provides your application access to the active Session. This is where you tell Hibernate about your <em>MockObject</em> class, and where you get a handle to an object that will save, update, delete and etc. your objects.</p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.SessionFactory</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.cfg.AnnotationConfiguration</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HibernateUtil
<span style="color: #009900;">&#123;</span>
    <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> SessionFactory sessionFactory<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">static</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">try</span>
        <span style="color: #009900;">&#123;</span>
            AnnotationConfiguration config <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> AnnotationConfiguration<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            config.<span style="color: #006633;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hibernate.dialect&quot;</span>, <span style="color: #0000ff;">&quot;org.hibernate.dialect.HSQLDialect&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            config.<span style="color: #006633;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hibernate.connection.driver_class&quot;</span>, <span style="color: #0000ff;">&quot;org.hsqldb.jdbcDriver&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            config.<span style="color: #006633;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hibernate.connection.url&quot;</span>, <span style="color: #0000ff;">&quot;jdbc:hsqldb:mem:demodb&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            config.<span style="color: #006633;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hibernate.connection.username&quot;</span>, <span style="color: #0000ff;">&quot;sa&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            config.<span style="color: #006633;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hibernate.connection.password&quot;</span>, <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            config.<span style="color: #006633;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hibernate.connection.pool_size&quot;</span>, <span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            config.<span style="color: #006633;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hibernate.connection.autocommit&quot;</span>, <span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            config.<span style="color: #006633;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hibernate.cache.provider_class&quot;</span>, <span style="color: #0000ff;">&quot;org.hibernate.cache.NoCacheProvider&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            config.<span style="color: #006633;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hibernate.hbm2ddl.auto&quot;</span>, <span style="color: #0000ff;">&quot;create-drop&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            config.<span style="color: #006633;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hibernate.show_sql&quot;</span>, <span style="color: #0000ff;">&quot;true&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            config.<span style="color: #006633;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hibernate.transaction.factory_class&quot;</span>, <span style="color: #0000ff;">&quot;org.hibernate.transaction.JDBCTransactionFactory&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            config.<span style="color: #006633;">setProperty</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hibernate.current_session_context_class&quot;</span>, <span style="color: #0000ff;">&quot;thread&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// Add your mapped classes here:</span>
            config.<span style="color: #006633;">addAnnotatedClass</span><span style="color: #009900;">&#40;</span>MockObject.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            sessionFactory <span style="color: #339933;">=</span> config.<span style="color: #006633;">buildSessionFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Throwable</span> ex<span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">System</span>.<span style="color: #006633;">err</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Initial SessionFactory creation failed.&quot;</span> <span style="color: #339933;">+</span> ex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">ExceptionInInitializerError</span><span style="color: #009900;">&#40;</span>ex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> SessionFactory getSessionFactory<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> sessionFactory<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><h4 style="text-align: center;">&#8212;-</h4><h4>HibernateDemo.java</h4><p>Our driver class is very simple; it does the following things:</p><ul><li>Get a handle to Hibernate Session</li><li>Create and persist two new <em>MockObjects</em></li><li>Print out the generated IDs<em><br
/> </em></li></ul><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.Transaction</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.classic.Session</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HibernateDemo
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        Session session <span style="color: #339933;">=</span> HibernateUtil.<span style="color: #006633;">getSessionFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getCurrentSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Transaction transaction <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>
&nbsp;
        MockObject object0 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MockObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        MockObject object1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MockObject<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>object0<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        session.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>object1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        transaction.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</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><span style="color: #0000ff;">&quot;Object 0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <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;Generated ID is: &quot;</span> <span style="color: #339933;">+</span> object0.<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: #339933;">;</span>
        <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;Generated Version is: &quot;</span> <span style="color: #339933;">+</span> object0.<span style="color: #006633;">getVersion</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: #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;Object 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <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;Generated ID is: &quot;</span> <span style="color: #339933;">+</span> object1.<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: #339933;">;</span>
        <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;Generated Version is: &quot;</span> <span style="color: #339933;">+</span> object1.<span style="color: #006633;">getVersion</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>
<span style="color: #009900;">&#125;</span></pre></div></div><p>And here we are. When you run HibernateDemo, you should have a fully functional Hibernate enabled HSQL database with one table. Play around with adding new fields to your object.</p><p>Your output should look something like this:</p><pre style="padding-left: 60px;">Object 0
Generated ID is: 1
Generated Version is: 0
Object 1
Generated ID is: 2
Generated Version is: 0</pre><h4 style="text-align: center;">&#8212;-</h4><h4>Considering the <a
href="http://en.wikipedia.org/wiki/Pattern_language#Balancing_of_forces" target="_blank">Forces</a>:</h4><p>I&#8217;d like to note, here, that we&#8217;re doing a lot of configuration in the code itself. This is OK if you are relatively sure that you won&#8217;t need to be making changes to your data model or back-end configuration very frequently. However, if you know that this is going to happen, I strongly suggest using <a
href="http://www.hibernate.org/hib_docs/v3/reference/en/html/tutorial.html#tutorial-firstapp-configuration" target="_blank">XML configuration files</a> instead, and you should also consider using traditional Hibernate with XML object-relational mappings. If you build all configuration into your code, every time you change the configuration, you&#8217;ll have to recompile / rebuild parts of your system. It&#8217;s much easier just to edit a file. However, unless you&#8217;re sure, just cross that bridge when you come to it.</p><h4>A few more things to remember:</h4><p>Take your time. Don&#8217;t give up. Ask questions. <em>READ the forums and documentation.</em></p><p>Hibernate is BIG. It&#8217;s a very powerful, very complex tool. It took me a long time to get even a simple object relationship working the way I wanted to. The learning curve is not small, and you will probably be frustrated for a while. I have, however, come to the point where I see so much benefit, that I feel it was completely worth my time to learn.</p><p>Configuration is <strong>everything</strong>. Hibernate can do anything you want: You can use it to tie in to existing database schema; you can use it to run plain SQL queries or stored procedures.</p><p>Good luck.</p><h3>References:</h3><ol><li><a
href="http://www.hibernate.org/hib_docs/v3/reference/en/html/tutorial.html" target="_blank">Hibernate&#8217;s Standard Tutorial</a></li><li><a
href="http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/" target="_blank">Hibernate Annotations Reference</a></li></ol><p>This article is part of a series: <a
href="http://ocpsoft.com/java/guide-to-hibernate-annotations/" target="_self">Guide to Hibernate Annotations</a></p> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/java/getting-started-quickly-with-hibernate-annotations/feed/</wfw:commentRss> <slash:comments>6</slash:comments> </item> <item><title>Hibernate: Use a Base Class to Map Common Fields</title><link>http://ocpsoft.com/java/hibernate-use-a-base-class-to-map-common-fields/</link> <comments>http://ocpsoft.com/java/hibernate-use-a-base-class-to-map-common-fields/#comments</comments> <pubDate>Sun, 17 Aug 2008 23:38:47 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[Hibernate]]></category> <category><![CDATA[Java]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=8</guid> <description><![CDATA[Tutorial Chapter 2 &#8211; Easier Development and Maintenance Tired of wiring in an id, version, and timestamp field into all of your Hibernate objects? There&#8217;s an easy way to solve this pain once and for all of your classes. Avoid code-repetition: today&#8217;s article focuses on using Hibernate Annotations to map common fields into one mapped [...]]]></description> <content:encoded><![CDATA[<h2>Tutorial Chapter 2 &#8211; Easier Development and Maintenance</h2><p>Tired of wiring in an <em>id</em>, <em>version</em>, and <em>timestamp</em> field into all of your Hibernate objects? There&#8217;s an easy way to solve this pain once and for all of your classes. Avoid code-repetition: today&#8217;s article focuses on using Hibernate Annotations to map common fields into one mapped superclass.</p><p>If you have not done so already, and need to get a bare bones hibernate application up and running, <a
href="http://ocpsoft.com/java/getting-started-quickly-with-hibernate-annotations/" target="_self">this guide</a> should get you up and running in a few minutes.</p><p><span
id="more-8"></span></p><h3>Basic Concepts:</h3><p>You&#8217;re using Hibernate, or at least getting your feet wet at this point, so let&#8217;s assume that you&#8217;ve started to notice a pattern. You need  in all of your objects:</p><ul><li>an <em>id </em>column</li><li>a <em>version</em> column</li><li>perhaps a <em>timestamp</em> column</li></ul><p>Lets say that you are also aware of the <a
href="http://www.onjava.com/pub/a/onjava/2006/09/13/dont-let-hibernate-steal-your-identity.html" target="_blank"><em>hashcode() </em>and <em>equals() </em>issues</a> that come with using objects in collections, so you want to define some basic functionality to handle that situation as well.</p><p>It would be pretty nice if we could do all this in one place, keeping updates quick and painless. Well, using the EJB-3/Hibernate <em><a
href="http://www.hibernate.org/hib_docs/ejb3-api/javax/persistence/MappedSuperclass.html" target="_blank">@MappedSuperclass</a> </em>annotation, we can. Mapped superclass tells Hibernate that you want all mappings defined in the base class to apply and be included in all classes which extend from it.</p><h3>Instructions:</h3><p>Let&#8217;s take a look at an example base-class that will meet our above needs.</p><ul><li>Copy the following code examples into your HIbernate Annotations enabled project</li></ul><h4>PersistentObject.java</h4><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.io.Serializable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.util.Date</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Column</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.GeneratedValue</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.GenerationType</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Id</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.MappedSuperclass</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Temporal</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.TemporalType</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Version</span><span style="color: #339933;">;</span>
&nbsp;
@MappedSuperclass
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> PersistentObject <span style="color: #000000; font-weight: bold;">implements</span> <span style="color: #003399;">Serializable</span>
<span style="color: #009900;">&#123;</span>
    @Id
    @GeneratedValue<span style="color: #009900;">&#40;</span>strategy <span style="color: #339933;">=</span> GenerationType.<span style="color: #006633;">AUTO</span><span style="color: #009900;">&#41;</span>
    @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;id&quot;</span>, updatable <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span>, nullable <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Long</span> id <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
    @Version
    @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;version&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> version <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    @Temporal<span style="color: #009900;">&#40;</span>TemporalType.<span style="color: #006633;">TIMESTAMP</span><span style="color: #009900;">&#41;</span>
    @Column<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;last_update&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">Date</span> lastUpdate<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> copy<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> PersistentObject source<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> source.<span style="color: #006633;">id</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">version</span> <span style="color: #339933;">=</span> source.<span style="color: #006633;">version</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">lastUpdate</span> <span style="color: #339933;">=</span> source.<span style="color: #006633;">lastUpdate</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: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Object</span> obj<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span> <span style="color: #339933;">==</span> obj<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>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>obj <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>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>obj <span style="color: #000000; font-weight: bold;">instanceof</span> PersistentObject<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000000; font-weight: bold;">final</span> PersistentObject other <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>PersistentObject<span style="color: #009900;">&#41;</span> obj<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> other.<span style="color: #006633;">id</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span> <span style="color: #339933;">!=</span> other.<span style="color: #006633;">id</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>
        <span style="color: #009900;">&#125;</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;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">boolean</span> getBooleanValue<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Boolean</span> value<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;">Boolean</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">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> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">id</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unused&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> setId<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">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;">int</span> getVersion<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">version</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    @SuppressWarnings<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unused&quot;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> setVersion<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> version<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;">version</span> <span style="color: #339933;">=</span> version<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;">Date</span> getLastUpdate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">lastUpdate</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setLastUpdate<span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">Date</span> lastUpdate<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;">lastUpdate</span> <span style="color: #339933;">=</span> lastUpdate<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><h4 style="text-align: center;">—-</h4><h3>Extending the Base Class:</h3><p>Using the MockObject class from Chapter 1, now extend PersistentObject. We can remove the fields that are now mapped in PersistentObject and add new fields to fit our business needs. Notice that MockObject does not define fields or methods for <em>id</em>, <em>version</em>, <em>equals()</em>, <em>hashcode()</em>, or <em>timestamp</em>; this behavior is now contained within our mapped superclass.</p><p>From now on, all you need to do to incorporate this behavior into new classes is to extend PersistentObject.</p><h4>MockObject.java</h4><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Column</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Entity</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Table</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.persistence.Transient</span><span style="color: #339933;">;</span>
&nbsp;
@<span style="color: #003399;">Entity</span>
@Table<span style="color: #009900;">&#40;</span>name <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;mock_objects&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> MockObject <span style="color: #000000; font-weight: bold;">extends</span> PersistentObject
<span style="color: #009900;">&#123;</span>
    @<span style="color: #000000; font-weight: bold;">Transient</span>
    <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>3621010469526215357L<span style="color: #339933;">;</span>
&nbsp;
    @Column
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> textField<span style="color: #339933;">;</span>
&nbsp;
    @Column
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">long</span> numberField<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">String</span> getTextField<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> textField<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> setTextField<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> textField<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;">textField</span> <span style="color: #339933;">=</span> textField<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;">long</span> getNumberField<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> numberField<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> setNumberField<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">long</span> numberField<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;">numberField</span> <span style="color: #339933;">=</span> numberField<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div><h4 style="text-align: center;">—-</h4><p>To prove it, we&#8217;ll re-run our demo application from the quick-start guide. Notice that the results are the same. I&#8217;ve added a little logic to show that our new columns work as well.</p><h4>HibernateDemo.java</h4><p>Our driver class does the following things:</p><ul><li>Get a handle to Hibernate Session</li><li>Create and persist two new MockObjects</li><li>Assign values into the textField of each object</li><li>Print out the generated IDs and set fields</li></ul><p><em>For referenced HibernateUtil,java, please see <a
href="http://ocpsoft.com/java/getting-started-quickly-with-hibernate-annotations/" target="_self">Chapter 1</a>.<br
/> </em></p><div
class="wp_syntax"><div
class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.Transaction</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">org.hibernate.classic.Session</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HibernateDemo
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        Session session <span style="color: #339933;">=</span> HibernateUtil.<span style="color: #006633;">getSessionFactory</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getCurrentSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Transaction transaction <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>
&nbsp;
        MockObject object0 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MockObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        object0.<span style="color: #006633;">setTextField</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am object 0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        MockObject object1 <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MockObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        object1.<span style="color: #006633;">setTextField</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am object 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        session.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>object0<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        session.<span style="color: #006633;">save</span><span style="color: #009900;">&#40;</span>object1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        transaction.<span style="color: #006633;">commit</span><span style="color: #009900;">&#40;</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><span style="color: #0000ff;">&quot;Object 0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>object0.<span style="color: #006633;">getTextField</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;">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;Generated ID is: &quot;</span> <span style="color: #339933;">+</span> object0.<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: #339933;">;</span>
        <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;Generated Version is: &quot;</span> <span style="color: #339933;">+</span> object0.<span style="color: #006633;">getVersion</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: #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;Object 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>object1.<span style="color: #006633;">getTextField</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;">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;Generated ID is: &quot;</span> <span style="color: #339933;">+</span> object1.<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: #339933;">;</span>
        <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;Generated Version is: &quot;</span> <span style="color: #339933;">+</span> object1.<span style="color: #006633;">getVersion</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>
<span style="color: #009900;">&#125;</span></pre></div></div><p>Which results in the following output:</p><pre style="padding-left: 30px;">Object 0
I am object 0
Generated ID is: 1
Generated Version is: 0

Object 1
I am object 1
Generated ID is: 2
Generated Version is: 0</pre><h4 style="text-align: center;">—-</h4><h4>Considering the Forces:</h4><p>It may be worth mentioning that using a base class for all of your objects can be a blessing and a curse. If you begin to reference objects using the more generic PersistentObject type, it is possible that you could find yourself constrained to the behavior of this one class. When this happens, consider defining PersistentObject as an <em>Interface</em> and then implement that interface with any number of <em>@MappedSuperclass</em> objects.</p><p>There are benefits to referencing objects by their generic type, as well. One example, which we&#8217;ll take a look at this when we dive into the <a
href="http://www.hibernate.org/328.html" target="_blank"><em>Dao</em></a> pattern, makes it very easy to perform a wide array of operations on your Hibernate objects.</p><p>Congratulations. You should now have a mapped superclass to contain your common functionality.</p><h3>References:</h3><ol><li><a
href="http://www.hibernate.org/hib_docs/v3/reference/en/html/tutorial.html" target="_blank">Hibernate&#8217;s Standard Tutorial</a></li><li><a
href="http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/" target="_blank">Hibernate Annotations Reference</a></li></ol><p>This article is part of a series: <a
href="http://ocpsoft.com/java/guide-to-hibernate-annotations/" target="_self">Guide to Hibernate Annotations</a></p> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/java/hibernate-use-a-base-class-to-map-common-fields/feed/</wfw:commentRss> <slash:comments>12</slash:comments> </item> <item><title>Guide to Hibernate Annotations</title><link>http://ocpsoft.com/java/guide-to-hibernate-annotations/</link> <comments>http://ocpsoft.com/java/guide-to-hibernate-annotations/#comments</comments> <pubDate>Wed, 23 Jul 2008 04:37:57 +0000</pubDate> <dc:creator>Lincoln</dc:creator> <category><![CDATA[Hibernate]]></category> <category><![CDATA[Java]]></category> <category><![CDATA[ORM]]></category><guid
isPermaLink="false">http://ocpsoft.com/?p=4</guid> <description><![CDATA[Is there light at the end of the tunnel? So I&#8217;m sure I&#8217;m not alone here, when I say that Hibernate can be frustrating, difficult to learn, and may even feel like to use it means to give up on some design principles that have been proven by many. When you first begin, it feels [...]]]></description> <content:encoded><![CDATA[<h2>Is there light at the end of the tunnel?</h2><p>So I&#8217;m sure I&#8217;m not alone here, when I say that Hibernate can be frustrating, difficult to learn, and may even feel like to use it means to give up on some design principles that have been proven by many.</p><p>When you first begin, it feels like there are holes, inconsistencies, and problems that send you running home to the familiar behavior of plain SQL. But, I don&#8217;t think it&#8217;s time to give up on Hibernate yet.</p><p>Keep in mind that I am a relatively new Hibernate user, I consider myself somewhat experienced with software development, design patterns, principles, and best practices. If this sounds like you, read on&#8230;</p><p><span
id="more-4"></span></p><h2>Objective:</h2><p>I&#8217;m beginning a series of articles that will:</p><ol><li>document my journey with Hibernate</li><li>be a guide for those of you who are still on your journey</li><li>give me a way to remember why I&#8217;ve made the decisions that I&#8217;ve made; <em>what was I thinking</em>??</li></ol><h2>Topics:</h2><p>Please look forward to the following topics that will start out this series.</p><ul><li>Getting started quickly. (<a
href="http://ocpsoft.com/java/getting-started-quickly-with-hibernate-annotations/" target="_self">read it</a>)</li><li>Using a base class to map common fields. (<a
href="http://ocpsoft.com/java/hibernate-use-a-base-class-to-map-common-fields/" target="_self">read it</a>)</li><li>Criteria returning duplicate results in OneToMany relationships.</li><li>The Abstract Dao.</li><li>Keeping transactional behavior in a lazy world.</li></ul><p>Also, feel free to comment on any of these topics, or request new topics. Remember to check the following resources; a lot of common problems can be solved by reading the hibernate FAQ.</p><h2>Assumptions:</h2><p>For the purpose of these examples, please assume the following versions:</p><ul><li><a
title="Java SDK 6.0" href="https://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/ViewProductDetail-Start?ProductRef=java_ee_sdk-5_05-oth-JPR@CDS-CDS_Developer" target="_blank">Java SDK 6.0</a></li><li><a
href="http://www.hibernate.org/6.html" target="_blank">Hibernate Annotations 3.3.1.GA</a></li><li><a
href="http://www.hibernate.org/6.html" target="_blank">Hibernate 3.2.6</a></li><li><a
href="http://dev.mysql.com/downloads/mysql/5.0.html#downloads" target="_blank">MySql 5.0</a></li></ul><h3>Resources:</h3><ol><li><a
title="Hibernate FAQ" href="http://www.hibernate.org/117.html" target="_blank">Hibernate FAQ</a></li><li><a
title="Hibernate Forums" href="http://forum.hibernate.org/" target="_blank">Hibernate Forums</a></li><li><a
title="Java Persistence API" href="http://java.sun.com/javaee/5/docs/api/javax/persistence/package-summary.html" target="_blank">Java Persistence API</a></li></ol> ]]></content:encoded> <wfw:commentRss>http://ocpsoft.com/java/guide-to-hibernate-annotations/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
<!-- Served from: ocpsoft.com @ 2010-07-29 17:11:26 by W3 Total Cache -->