<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
    <channel>
        <title>Using Hibernate with CUBRID</title>
        <link>http://www.cubrid.org/?mid=cubrid_hibernate_tutorial</link>
        <description>Using Hibernate with CUBRID</description>
        <language>en</language>
        <pubDate>Tue, 12 Oct 2010 20:10:25 -0800</pubDate>
        <lastBuildDate>Tue, 26 Jun 2012 05:37:42 -0800</lastBuildDate>
        <generator>XpressEngine 1.4.4.1</generator>
                        										        <item>
            <title>Using Hibernate w...</title>
            <dc:creator>CUBRID</dc:creator>
            <link>http://www.cubrid.org/cubrid_hibernate_tutorial</link>
            <guid isPermaLink="true">http://www.cubrid.org/cubrid_hibernate_tutorial</guid>
                                    <description><![CDATA[<h1 style="margin-top: 10px; margin-right: 0px; margin-bottom: 23px; margin-left: 0px; ">Using Hibernate with CUBRID</h1><br />

<div><div>
<span class="Apple-style-span" style="font-family: monospace; white-space: pre-wrap; font-size: medium; ">⇐</span><span style="background-color: rgb(237, 242, 194); "><span style="background-color: rgb(255, 255, 255); color: rgb(128, 128, 128); "><a href="/cubrid_hibernate" target="_self">HIBERNATE for CUBRID</a></span></span></div><div><br /></div><div>Before we start, note that CUBRID will be accessed from Hibernate using the JDBC driver. If you have a firm knowledge of JDBC and Hibernate you will notice that there isn't any difference in accessing CUBRID as with any other JDBC-enabled database systems. However, configuring Hibernate to work with CUBRID requires some different steps, as for the moment CUBRID support is not officially supported by Hibernate out of the box and we have a custom packaged Hibernate distribution with the added support.</div><div><br /></div><h3>Required Software</h3><div><br /></div><div>To successfully replicate the following examples, it is necessary to install the following prerequisites:</div><div><br /></div><div><ul><li>HIBERNATE</li><li>HIBERNATE Library for CUBRID</li><li>SLF4J Library</li></ul></div><div><span class="Apple-style-span" style="font-size: 15px; font-weight: bold; "><br /></span></div><div><span class="Apple-style-span" style="font-size: 15px; font-weight: bold; ">Downloads</span></div><div><ul><li>Download Hibernate.</li><li>Download Hibernate Library for CUBRID.</li><li>Download SLF4J Library.</li></ul></div><div><br /></div><h3>Examples</h3><div><br /></div><div><span class="Apple-style-span" style="font-size: 15px; font-weight: bold; ">Setting up the Directory Structure and Environment</span></div><div><br /></div><div><br /></div><div><b>Create a Workspace</b></div><div><br /></div><div>In this tutorial you will learn how to configure a simple project that works with CUBRID and Hibernate to access a sample database. Let's start by creating a directory structure:</div><div><br /></div><div><div editor_component="code_highlighter" code_type="Bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">$mkdir tutorial<br />$mkdir tutorial/lib<br />$mkdir tutorial/src</div><br /></div><div><b>Unpack Hibernate</b></div><div><br /></div><div>Once you have downloaded the Hibernate, unpack it into a temporary directory and copy the following libraries to tutorial/lib:</div><div><br /></div><div><ul><li>antlr-2.7.6.jar</li><li>cglib-2.2.jar</li><li>commons-collections-3.1.jar</li><li>dom4j-1.6.1.jar</li><li>javassist-3.9.0.GA.jar</li><li>jta-1.1.jar</li></ul></div><div><br /></div><div><b>Unpack a SLF4J</b></div></div><div><br /></div><div><div>Once you have downloaded the SLF4J Library, unpack it into a temporary directory and copy the following libraries to tutorial/lib:</div><div><br /></div><div><ul><li>slf4j-api-1.6.0.jar</li><li>slf4j-simple-1.6.0.jar</li></ul></div><div><br /></div><div><b>Copy CUBRID JDBC and Hibernate Library for CUBRID</b></div><div><br /></div><div>Next, we will need to add two CUBRID specific libraries to tutorial/lib:</div><div><br /></div><div><ul><li>JDBC-8.3.0.0337-cubrid.jar: This is CUBRID's JDBC Driver, which comes together with CUBRID. It is located at $CUBRID/jdbc directory, the place where you have installed the CUBRID Server. Or you can download it from JDBC &amp; Java Programming with CUBRID.</li><li>hibernate-core-3.6.5-Final.jar: This is a custom built Hibernate Library for CUBRID.</li></ul></div><div>The directory structure of your project should look now like this:</div><div><br /></div><div><img editor_component="image_link" src="http://www.cubrid.org/files/attach/images/tutorials/hibernate/hibernate_directory_structure.jpg" width="220" height="274"/><br /></div><div><br /></div><div><b>Scenario</b></div><div><br /></div><div>Let's suppose the following scenario. We have a table where we keep a list of countries. Each country is identified by its 3-letters code, has a name, continent and a capital. We will use this table to insert/select/update/delete records using Hibernate.</div><div><br /></div><h3><b>Create a Database Table</b></h3><div><b><br /></b></div><div>Let's start the implementation by opening up CUBRID Manager and creating a new database named tutorial. In this database we will create this table named country. DDL should look like this:</div><div><br /></div><div><div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">CREATE TABLE country (<br /><span class="Apple-tab-span" style="white-space:pre">	</span>code VARCHAR(3) PRIMARY KEY NOT NULL UNIQUE,<br /><span class="Apple-tab-span" style="white-space:pre">	</span>`name` VARCHAR(40),<br /><span class="Apple-tab-span" style="white-space:pre">	</span>capital VARCHAR(30),<br /><span class="Apple-tab-span" style="white-space:pre">	</span>continent VARCHAR(10)<br />);</div><p><br /></p><p><img editor_component="image_link" src="http://www.cubrid.org/files/attach/images/tutorials/hibernate/hibernate_cubrid_database.jpg" width="500" height="425"/><br /></p><p><br /></p></div><div>Let's also insert a couple of records into the database for a start:</div><div><br /></div><div><div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">INSERT INTO country &nbsp;VALUES ('ROU', 'Romania', 'Bucharest', 'Europe');<br />INSERT INTO country &nbsp;VALUES ('CAN', 'Canada', 'Ottawa', 'America');</div><br /><h3><b>Create a POJO Mapping Class</b></h3></div><div><br /></div><div>Let's create the directory layout for java packages.</div><div><br /></div><div><div editor_component="code_highlighter" code_type="Bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">$mkdir -p tutorial/src/com/cubrid/hibernate<br />$cd tutorial/src/com/cubrid/hibernate</div></div><div><br /></div><div>Here we create the Java class com.cubrid.hibernate.Country in Country.java.</div><div><br /></div><div><div editor_component="code_highlighter" code_type="Java" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">package com.cubrid.hibernate;<br />import java.util.Date;<br /><br />public class Country {<br /><span class="Apple-tab-span" style="white-space:pre">	</span>private String code;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>private String name;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>private String continent;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>private String capital;<br /><br /><span class="Apple-tab-span" style="white-space:pre">	</span>public String getCode() {<br /><span class="Apple-tab-span" style="white-space:pre">		</span>return code;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>}<br /><br /><span class="Apple-tab-span" style="white-space:pre">	</span>public void setCode(String code) {<br /><span class="Apple-tab-span" style="white-space:pre">		</span>this.code = code;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>}<br /><br /><span class="Apple-tab-span" style="white-space:pre">	</span>public String getName() {<br /><span class="Apple-tab-span" style="white-space:pre">		</span>return name;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>}<br /><br /><span class="Apple-tab-span" style="white-space:pre">	</span>public void setName(String name) {<br /><span class="Apple-tab-span" style="white-space:pre">		</span>this.name = name;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>}<br /><span class="Apple-tab-span" style="white-space:pre">	</span><br /><span class="Apple-tab-span" style="white-space:pre">	</span>public String getContinent() {<br /><span class="Apple-tab-span" style="white-space:pre">		</span>return continent;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>}<br /><br /><span class="Apple-tab-span" style="white-space:pre">	</span>public void setContinent(String continent) {<br /><span class="Apple-tab-span" style="white-space:pre">		</span>this.continent = continent;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>}<br /><br /><span class="Apple-tab-span" style="white-space:pre">	</span>public String getCapital() {<br /><span class="Apple-tab-span" style="white-space:pre">		</span>return capital;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>}<br /><br /><span class="Apple-tab-span" style="white-space:pre">	</span>public void setCapital(String capital) {<br /><span class="Apple-tab-span" style="white-space:pre">		</span>this.capital = capital;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>}<br />}</div><p><br /></p><p></p><h3><b>Create a HibernateUtil Class</b></h3><div><b><br /></b></div><div>Next we need to create the HibernateUtil class that provide support for session management in HibernateUtil.java file.</div><div><br /></div><div><div editor_component="code_highlighter" code_type="Java" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">package com.cubrid.hibernate;<br />import org.hibernate.SessionFactory;<br />import org.hibernate.Session;<br />import org.hibernate.Transaction;<br />import org.hibernate.cfg.Configuration;<br /><br />public class HibernateUtil {<br /><span class="Apple-tab-span" style="white-space:pre">	</span>private static org.hibernate.SessionFactory sessionFactory;<br /><br /><span class="Apple-tab-span" style="white-space:pre">	</span>public static SessionFactory getSessionFactory() {<br /><span class="Apple-tab-span" style="white-space:pre">		</span>if (sessionFactory == null) {<br /><span class="Apple-tab-span" style="white-space:pre">			</span>&nbsp;initSessionFactory();<br /><span class="Apple-tab-span" style="white-space:pre">		</span>}<br /><span class="Apple-tab-span" style="white-space:pre">		</span>return sessionFactory;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>}<br /><br /><span class="Apple-tab-span" style="white-space:pre">	</span>private static synchronized void initSessionFactory() {<br /><span class="Apple-tab-span" style="white-space:pre">		</span>sessionFactory = new Configuration().configure().buildSessionFactory();<br /><span class="Apple-tab-span" style="white-space:pre">	</span>}<br /><br /><span class="Apple-tab-span" style="white-space:pre">	</span>public static Session getSession() {<br /><span class="Apple-tab-span" style="white-space:pre">		</span>return getSessionFactory().openSession();<br /><span class="Apple-tab-span" style="white-space:pre">	</span>}<br />}</div><div><br /></div></div><h3><b>Create a Tutorial Class</b></h3><div><b><br /></b></div><div><br /></div><div>Let's write now the skeleton of our application. Let's call it Tutorial in class Tutorial.java. Here we will write later all the operations done using Hibernate.</div><div><br /></div><div><div editor_component="code_highlighter" code_type="Java" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">package com.cubrid.hibernate;<br /><br />public class Tutorial {<br /><span class="Apple-tab-span" style="white-space:pre">	</span>public static void main(String[] args) {<br /><span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println("Tutorial is starting");<br /><span class="Apple-tab-span" style="white-space:pre">	</span>}<br />}</div><p><br /></p></div><h3><b>Create an Ant Build Script</b></h3><div><b><br /></b></div><div><br /></div><div>In order to compile and run our tutorial we will use ant. The build.xml script from the root of our directory structure should look like this:</div><div><br /></div><div><div editor_component="code_highlighter" code_type="Xml" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">&lt;?xml version="1.0" encoding="utf-8" ?&gt;<br />&lt;project name="hibernate-tutorial" default="run"&gt;<br /><br /><span class="Apple-tab-span" style="white-space:pre">	</span>&lt;property name="sourcedir" value="src"/&gt;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>&lt;property name="targetdir" value="bin"/&gt;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>&lt;property name="libdir" value="lib"/&gt;<br /><br /><span class="Apple-tab-span" style="white-space:pre">	</span>&lt;path id="libraries"&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;fileset dir="${libdir}"&gt;<br /><span class="Apple-tab-span" style="white-space:pre">			</span>&lt;include name="*.jar"/&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;/fileset&gt;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>&lt;/path&gt;<br /><br /><span class="Apple-tab-span" style="white-space:pre">	</span>&lt;target name="clean"&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;delete dir="${targetdir}"/&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;mkdir dir="${targetdir}"/&gt;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>&lt;/target&gt;<br /><br /><span class="Apple-tab-span" style="white-space:pre">	</span>&lt;target name="compile" depends="clean, copy-resources"&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;javac srcdir="${sourcedir}"&nbsp;destdir="${targetdir}"&nbsp;classpathref="libraries"&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;/javac&gt;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>&lt;/target&gt;<br /><br /><span class="Apple-tab-span" style="white-space:pre">	</span>&lt;target name="copy-resources"&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;copy todir="${targetdir}"&gt;<br /><span class="Apple-tab-span" style="white-space:pre">			</span>&lt;fileset dir="${sourcedir}"&gt;<br /><span class="Apple-tab-span" style="white-space:pre">				</span>&lt;exclude name="**/*.java"/&gt;<br /><span class="Apple-tab-span" style="white-space:pre">			</span>&lt;/fileset&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;/copy&gt;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>&lt;/target&gt;<br /><br /><span class="Apple-tab-span" style="white-space:pre">	</span>&lt;target name="run" depends="compile"&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;java fork="true" classname="com.cubrid.hibernate.Tutorial" classpathref="libraries"&gt;<br /><span class="Apple-tab-span" style="white-space:pre">			</span>&lt;classpath path="${targetdir}"/&gt;<br /><span class="Apple-tab-span" style="white-space:pre">			</span>&lt;arg value="${action}"/&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;/java&gt;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>&lt;/target&gt;<br />&lt;/project&gt;</div><br />You can run ant now and if everything is set up right, the output would be:</div><div><br /></div><div><div editor_component="code_highlighter" code_type="Bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">Buildfile: build.xml<br />clean:<br />&nbsp;&nbsp;[delete] Deleting directory /home/cristiroma/workspace/tutorial/bin<br />&nbsp;&nbsp;[mkdir] Created dir: /home/cristiroma/workspace/tutorial/bin<br /><br />copy-resources:<br />&nbsp;&nbsp;&nbsp;[copy] Copied 3 empty directories to 2 empty directories under /home/cristiroma/workspace/tutorial/bin<br /><br />compile:<br />&nbsp;&nbsp;&nbsp;[javac] Compiling 3 source files to /home/cristiroma/workspace/tutorial/bin<br /><br />run:<br />&nbsp;&nbsp;&nbsp;[java] Tutorial is starting<br /><br />BUILD SUCCESSFUL<br />Total time: 0 seconds</div><p><b style="font-size: 15px; "><br /></b></p><p><b style="font-size: 15px; ">Configuring Hibernate</b></p></div><div>We will configure Hibernate using hibernate.cfg.xml file. The content of the file is presented below. The file has to be placed in the src folder. As you can see, in bold are highlighted the CUBRID specific settings. The database JDBC connection URL differs from other databases as well as JDBC driver, and Hibernate dialect classes.</div><div><br /></div><div><div editor_component="code_highlighter" code_type="Xml" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">&lt;?xml version='1.0' encoding='utf-8'?&gt;<br />&lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&gt;<br />&lt;hibernate-configuration&gt;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>&lt;session-factory&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;property name="connection.url"&gt;jdbc:cubrid:127.0.0.1:30000:tutorial:::&lt;/property&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;property name="connection.username"&gt;dba&lt;/property&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;property name="connection.password"&gt;test&lt;/property&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;property name="connection.driver_class"&gt;cubrid.jdbc.driver.CUBRIDDriver&lt;/property&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;property name="dialect"&gt;org.hibernate.dialect.CUBRIDDialect&lt;/property&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;property name="show_sql"&gt;true&lt;/property&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;property name="format_sql"&gt;true&lt;/property&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&nbsp;&lt;!-- JDBC connection pool (use the built-in) --&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;property name="connection.pool_size"&gt;1&lt;/property&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;property name="current_session_context_class"&gt;thread&lt;/property&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&nbsp;&lt;!-- Mapping files will go here.... --&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;mapping resource="com/cubrid/hibernate/country.hbm.xml"/&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;/session-factory&gt;<br />&lt;/hibernate-configuration&gt;</div><br />It's time now to write the mapping for our country table. As you already know we have to create a country.hbm.xml file in tutorial/src/com/cubrid/hibernate directory, as is highlighted in the configuration above. The content of the file is:</div></div><div><span class="Apple-style-span" style="font-size: 15px; font-weight: 800; "><br /></span></div><div><div editor_component="code_highlighter" code_type="Xml" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">&lt;?xml version="1.0" encoding="UTF-8"?&gt;<br />&lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;<br />&lt;hibernate-mapping&gt;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>&lt;class name="com.cubrid.hibernate.Country" table="country"&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;id name="code" column="code" type="java.lang.String" length="3" /&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;property name="name" type="java.lang.String" length="40"/&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;property name="capital" type="java.lang.String" length="30"/&gt;<br /><span class="Apple-tab-span" style="white-space:pre">		</span>&lt;property name="continent" type="java.lang.String" length="10"/&gt;<br /><span class="Apple-tab-span" style="white-space:pre">	</span>&lt;/class&gt;<br />&lt;/hibernate-mapping&gt;</div><br /><br /></div><div><span class="Apple-style-span" style="font-size: 15px; font-weight: bold; "><b>Common Operations</b></span></div><div><b><br /></b></div><div>Now that all configuration is complete, here is the directory layout of our project:</div><div><br /></div><div><br /></div><div><img editor_component="image_link" src="http://www.cubrid.org/files/attach/images/tutorials/hibernate/hibernate_directory_structure2.jpg" width="258" height="421"/><br /></div><div><br /></div><div>In this chapter let's see how we accomplish few simple operations using Hibernate. Open again the Tutorial.java class and write a query.</div><div><br /></div><div><div editor_component="code_highlighter" code_type="Java" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">package com.cubrid.hibernate;<br /><br />import org.hibernate.Session;<br />import java.util.List;<br />import java.util.Iterator;<br />import com.cubrid.hibernate.HibernateUtil;<br /><br />public class Tutorial {<br /><span class="Apple-tab-span" style="white-space:pre">	</span>public static void main(String[] args) {<br /><span class="Apple-tab-span" style="white-space:pre">		</span>System.out.println("Tutorial is starting");<br /><br /><span class="Apple-tab-span" style="white-space:pre">		</span>// Selecting rows<br /><span class="Apple-tab-span" style="white-space:pre">		</span>Session session = HibernateUtil.getSession();<br /><span class="Apple-tab-span" style="white-space:pre">		</span>List&lt;Country&gt; countries = session.createCriteria(Country.class).list();<br /><span class="Apple-tab-span" style="white-space:pre">		</span>Iterator&lt;Country&gt; it = countries.iterator();<br /><br /><span class="Apple-tab-span" style="white-space:pre">		</span>while(it.hasNext()) {<br /><span class="Apple-tab-span" style="white-space:pre">			</span>Country country = it.next();<br /><span class="Apple-tab-span" style="white-space:pre">			</span>System.out.println(country.getCode());<br /><span class="Apple-tab-span" style="white-space:pre">		</span>}<br /><span class="Apple-tab-span" style="white-space:pre">	</span>}<br />}</div><br />Running this code using ant will result in the following output.</div><div><br /></div><div><div editor_component="code_highlighter" code_type="Bash" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">Buildfile: build.xml<br />clean:&nbsp;<br />&nbsp; [delete] Deleting directory /home/cristiroma/workspace/tutorial/bin<br />&nbsp;&nbsp;[mkdir] Created dir: /home/cristiroma/workspace/tutorial/bin<br /><br />copy-resources:<br />&nbsp;&nbsp;[copy] Copying 2 files to /home/cristiroma/workspace/tutorial/bin&nbsp;<br /><br />compile:&nbsp;<br />&nbsp;&nbsp;[javac] Compiling 3 source files to /home/cristiroma/workspace/tutorial/bin&nbsp;<br />&nbsp;&nbsp;[javac] Note: /home/cristiroma/workspace/tutorial/src/com/cubrid/hibernate/Tutorial.java uses unchecked or unsafe operations.&nbsp;<br />&nbsp;&nbsp;[javac] Note: Recompile with -Xlint:unchecked for details.&nbsp;<br /><br />run:<br />&nbsp;&nbsp;[java] Tutorial is starting&nbsp;<br />&nbsp;&nbsp;[java] 11 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.6.5-Final&nbsp;<br />&nbsp;&nbsp;[java] 12 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found&nbsp;<br />&nbsp;&nbsp;[java] 15 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist&nbsp;<br />&nbsp;&nbsp;[java] 19 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling&nbsp;<br />&nbsp;&nbsp;[java] 93 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml&nbsp;<br />&nbsp;&nbsp;[java] 93 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml&nbsp;<br />&nbsp; [java] 154 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource : com/cubrid/hibernate/Country.hbm.xml&nbsp;<br />...<br />&nbsp;&nbsp;[java] 392 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory&nbsp;<br />&nbsp;&nbsp;[java] 538 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured<br />&nbsp;&nbsp;[java] Hibernate:&nbsp;<br />&nbsp;&nbsp;[java] &nbsp; &nbsp; select&nbsp;<br />&nbsp;&nbsp;[java] &nbsp; &nbsp; &nbsp; &nbsp; this_.code as code0_0_,&nbsp;<br />&nbsp;&nbsp;[java] &nbsp; &nbsp; &nbsp; &nbsp; this_.name as name0_0_,&nbsp;<br />&nbsp;&nbsp;[java] &nbsp; &nbsp; &nbsp; &nbsp; this_.capital as capital0_0_,<br />&nbsp;&nbsp;[java] &nbsp; &nbsp; &nbsp; &nbsp; this_.continent as continent0_0_&nbsp;&nbsp;<br />&nbsp;&nbsp;[java] &nbsp; &nbsp; from&nbsp;<br />&nbsp;&nbsp;[java] &nbsp; &nbsp; &nbsp; &nbsp; country this_&nbsp;<br />&nbsp;&nbsp;[java] ROU&nbsp;<br />&nbsp; [java] CAN</div></div><div><br /><div>As you can see, Hibernate is connecting to the tutorial database and selects the two records from country table. Deleting records from the table is also straightforward:</div></div><div><br /></div><div><div editor_component="code_highlighter" code_type="Java" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(modules/editor/components/code_highlighter/code.png) no-repeat top right;">//Deleting rows<br />session.beginTransaction();<br />session.createQuery("delete from Country").executeUpdate();<br />session.getTransaction().commit();</div><br />The rest of the possibilities enabled by Hibernate, including advanced queries, joins etc. can be accomplished in Hibernate using CUBRID as database the same way you would do with other RDBMS.</div><div><span class="Apple-style-span" style="font-size: 15px; font-weight: bold; "><b><br /></b></span></div><div><span class="Apple-style-span" style="font-size: 15px; font-weight: bold; "><b>Getting Help</b></span></div><div><br /></div><div>If you have any difficulties, you can post your questions to java forum for CUBRID at <a href="/?mid=forum&amp;category=195533&amp;act=dispForumContent" target="_self">http://forum.cubrid.org</a>.</div></div><div><br /></div>]]></description>
                        <pubDate>Sat, 16 Jul 2011 18:11:38 -0800</pubDate>
                                </item>
            </channel>
</rss>
