<?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/tags/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>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:09:27 by W3 Total Cache -->