<?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>Java Interview Questions &#187; Hibernate Tutorial</title>
	<atom:link href="http://www.bestjavainterviewquestions.com/category/java-important-notes/hibernate-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.bestjavainterviewquestions.com</link>
	<description>Java Interview Questions &#124; Core Java Interview Questions &#124; Advanced Java Interview Questions &#124; EJB Interview Questions &#124; J2EE Interview Questions &#124; Hibernate Interview Questions</description>
	<lastBuildDate>Fri, 03 Feb 2012 10:23:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Hibernate Basics</title>
		<link>http://www.bestjavainterviewquestions.com/310/</link>
		<comments>http://www.bestjavainterviewquestions.com/310/#comments</comments>
		<pubDate>Sat, 20 Sep 2008 17:21:46 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Hibernate Tutorial]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=310</guid>
		<description><![CDATA[Introduction Hibernate is a solution for object relational mapping and a persistence management solution or persistent layer. This is probably not understandable for anybody learning Hibernate. What you can imagine is probably that you have your application with some functions (business logic) and you want to save data in a database. When you use Java [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Introduction</strong></p>
<p>Hibernate is a solution for object relational mapping and a persistence management solution or persistent layer. This is probably not understandable for anybody learning Hibernate.</p>
<p>What you can imagine is probably that you have your application with some functions (business logic) and you want to save data in a database. When you use Java all the business logic normally works with objects of different class types. Your database tables are not at all objects.</p>
<p>Hibernate provides a solution to map database tables to a class. It copies one row of the database data to a class. In the other direction it supports to save objects to the database. In this process the object is transformed to one or more tables.</p>
<p>Saving data to a storage is called persistence. And the copying of tables to objects and vice versa is called object relational mapping.<br />
<strong>Create a Java Project</strong></p>
<p>Using Eclipse press the keys Ctrl+n (Strg+n) to create a new project. Select a Java project. We will call it FirstHibernateExample.<br />
Prepare the project for Hibernate using<br />
MyEclipse</p>
<p>When you are using MyEclipse, right click on your project in the package explorer and choose Add Hibernate capabilities.</p>
<p>Select First Hibernate Program&#8212;&gt;In that select  New&#8211;&gt;and then Just follow the instructions</p>
<p>Continue the wizard and create a new hibernate.cfg.xml in the src directory.</p>
<p>In the last step you can create a Hibernate SessionFactory. I prefer to create my own. You can find it below.</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>September 20, 2008 -- <a href="http://www.bestjavainterviewquestions.com/difference-between-sessionsave-and-sessionsaveorupdate-2/" title="Difference between session.save() and session.saveOrUpdate()">Difference between session.save() and session.saveOrUpdate()</a> (0)</li><li>July 7, 2008 -- <a href="http://www.bestjavainterviewquestions.com/jsp-standard-questions/" title="JSP Standard Questions">JSP Standard Questions</a> (0)</li><li>December 17, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-is-port-number/" title="What is Port Number">What is Port Number</a> (0)</li><li>October 5, 2008 -- <a href="http://www.bestjavainterviewquestions.com/difference-between-get-and-head-request-methods/" title="Difference between GET and HEAD request methods">Difference between GET and HEAD request methods</a> (0)</li><li>December 12, 2008 -- <a href="http://www.bestjavainterviewquestions.com/url-connection-class/" title="URL Connection Class">URL Connection Class</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/310/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Connecting our Class(say x) to DataBase using Hibernate</title>
		<link>http://www.bestjavainterviewquestions.com/connecting-our-classsay-x-to-database-using-hibernate/</link>
		<comments>http://www.bestjavainterviewquestions.com/connecting-our-classsay-x-to-database-using-hibernate/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 18:03:59 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Hibernate Tutorial]]></category>
		<category><![CDATA[configuring the class to database]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=329</guid>
		<description><![CDATA[In this file the mapping from our class Honey to the database table honey is configured. Create a Test Client Create a Java Class ?TestClient? in the package ?de.laliluna.example?. Add the following source code. It includes methods to create entries in the database, to update and to list them. /** * Test application for example [...]]]></description>
			<content:encoded><![CDATA[<p>In this file the mapping from our class Honey to the database table honey is configured.</p>
<p><strong>Create a Test Client</strong></p>
<p>Create a Java Class ?TestClient? in the package ?de.laliluna.example?.</p>
<p>Add the following source code. It includes methods to create entries in the database, to update and to list them.<br />
<em><br />
<font size="4"><br />
/**<br />
 * Test application for example<br />
 * @author Sebastian Hennebrueder<br />
 * created Jan 16, 2006<br />
 * copyright 2006 by http://www.laliluna.de<br />
 */</p>
<p>package de.laliluna.example;</p>
<p>import java.util.Iterator;<br />
import java.util.List;</p>
<p>import org.apache.log4j.Logger;<br />
import org.hibernate.HibernateException;<br />
import org.hibernate.Session;<br />
import org.hibernate.Transaction;</p>
<p>import de.laliluna.hibernate.InitSessionFactory;</p>
<p>public class TestExample {</p>
<p>	private static Logger log =Logger.getLogger(TestExample.class);<br />
	/**<br />
	 * @param args<br />
	 */<br />
	public static void main(String[] args) {<br />
		Honey forestHoney = new Honey();<br />
		forestHoney.setName(&#8220;forest honey&#8221;);<br />
		forestHoney.setTaste(&#8220;very sweet&#8221;);<br />
		Honey countryHoney = new Honey();<br />
		countryHoney.setName(&#8220;country honey&#8221;);<br />
		countryHoney.setTaste(&#8220;tasty&#8221;);<br />
		createHoney(forestHoney);<br />
		createHoney(countryHoney);<br />
		// our instances have a primary key now:<br />
		log.debug(forestHoney);<br />
		log.debug(countryHoney);<br />
		listHoney();<br />
		deleteHoney(forestHoney);<br />
		listHoney();</p>
<p>	}</p>
<p>	private static void listHoney() {<br />
		Transaction tx = null;<br />
		Session session = InitSessionFactory.getInstance().getCurrentSession();<br />
		try {<br />
			tx = session.beginTransaction();<br />
			List honeys = session.createQuery(&#8220;select h from Honey as h&#8221;)<br />
					.list();<br />
			for (Iterator iter = honeys.iterator(); iter.hasNext();) {<br />
				Honey element = (Honey) iter.next();<br />
				log.debug(element);<br />
			}<br />
			tx.commit();<br />
		} catch (HibernateException e) {<br />
			e.printStackTrace();<br />
			if (tx != null &#038;&#038; tx.isActive())<br />
				tx.rollback();</p>
<p>		}<br />
	}</p>
<p>	private static void deleteHoney(Honey honey) {<br />
		Transaction tx = null;<br />
		Session session = InitSessionFactory.getInstance().getCurrentSession();<br />
		try {<br />
			tx = session.beginTransaction();<br />
			session.delete(honey);<br />
			tx.commit();<br />
		} catch (HibernateException e) {<br />
			e.printStackTrace();<br />
			if (tx != null &#038;&#038; tx.isActive())<br />
				tx.rollback();<br />
		}<br />
	}</p>
<p>	private static void createHoney(Honey honey) {<br />
		Transaction tx = null;<br />
		Session session = InitSessionFactory.getInstance().getCurrentSession();<br />
		try {<br />
			tx = session.beginTransaction();<br />
			session.save(honey);<br />
			tx.commit();<br />
		} catch (HibernateException e) {<br />
			e.printStackTrace();<br />
			if (tx != null &#038;&#038; tx.isActive())<br />
				tx.rollback();<br />
		}<br />
	}<br />
}<br />
</font></em></p>
<p>Congratulations. You have finished your first steps in the Hibernate world.</p>
<p>We wanted to give you a fast entry in the Hibernate world. There are many more complex topics and better implementation to use Hibernate. For example, the opening and closing of the session in each method is not a good practice. A session can be reused during which saves a lot of time.</p>
<p>When you want to learn more about best practices have a look at our seminars or other tutorials.<br />
Copyright and disclaimer</p>
<p>This tutorial is copyright of Sebastian Hennebrueder, laliluna.de. You may download a tutorial for your own personal use but not redistribute it. You must not remove or modify this copyright notice.</p>
<p>The tutorial is provided as is. I do not give any warranty or guaranty any fitness for a particular purpose. In no event shall I be liable to any party for direct, indirect, special, incidental, or consequential damages, including lost profits, arising out of the use of this tutorial, even if I has been advised of the possibility of such damage.</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>December 10, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-is-thread-synchronization/" title="What is Thread Synchronization">What is Thread Synchronization</a> (1)</li><li>September 17, 2008 -- <a href="http://www.bestjavainterviewquestions.com/mapping-files-in-hibernate/" title="Mapping Files in Hibernate">Mapping Files in Hibernate</a> (0)</li><li>August 24, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-session-bean-and-entity-bean/" title="Define Session Bean and Entity Bean">Define Session Bean and Entity Bean</a> (0)</li><li>April 6, 2009 -- <a href="http://www.bestjavainterviewquestions.com/exception-handling-and-their-uses/" title="Exception Handling and their uses">Exception Handling and their uses</a> (2)</li><li>October 16, 2008 -- <a href="http://www.bestjavainterviewquestions.com/dynamic-and-static-web-page/" title="Dynamic and Static Web Page">Dynamic and Static Web Page</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/connecting-our-classsay-x-to-database-using-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mapping Files in Hibernate</title>
		<link>http://www.bestjavainterviewquestions.com/mapping-files-in-hibernate/</link>
		<comments>http://www.bestjavainterviewquestions.com/mapping-files-in-hibernate/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 17:56:25 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Hibernate Tutorial]]></category>
		<category><![CDATA[mapping files in hibernate]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=326</guid>
		<description><![CDATA[Create the mapping files Create a new file named ?hibernate.cfg.xml? in your root directory if it is not already created. Insert the following in your hibernate file. Do not forget to change the username and the password to suit your database configuration. PostgreSQL Version: jdbc:postgresql://localhost/firsthibernate postgres org.postgresql.Driver org.hibernate.dialect.PostgreSQLDialect p org.hibernate.transaction.JDBCTransactionFactory thread true Random PostsSeptember 20, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Create the mapping files</strong></p>
<p>Create a new file named ?hibernate.cfg.xml? in your root directory if it is not already created.</p>
<p>Insert the following in your hibernate file. Do not forget to change the username and the password to suit your database configuration.</p>
<p><em><font size="4">PostgreSQL Version:</p>
<p><?xml version='1.0' encoding='UTF-8'?><br />
<!DOCTYPE hibernate-configuration PUBLIC<br />
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"<br />
          "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><br />
<hibernate-configuration><br />
<session-factory></p>
<property name="connection.url">jdbc:postgresql://localhost/firsthibernate</property>
<property name="connection.username">postgres</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="connection.password">p</property>
<property name="transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
    <!--  thread is the short name for<br />
      org.hibernate.context.ThreadLocalSessionContext<br />
      and let Hibernate bind the session automatically to the thread<br />
    --></p>
<property name="current_session_context_class">thread</property>
    <!-- this will show us all sql statements --></p>
<property name="hibernate.show_sql">true</property>
	<!-- mapping files --></p>
<mapping resource="de/laliluna/example/Honey.hbm.xml" />
</session-factory><br />
</hibernate-configuration></font></em></p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>October 5, 2008 -- <a href="http://www.bestjavainterviewquestions.com/difference-between-errortag-and-message-tag/" title="Difference between errortag and message tag">Difference between errortag and message tag</a> (0)</li><li>November 6, 2008 -- <a href="http://www.bestjavainterviewquestions.com/load-factor/" title="Load Factor">Load Factor</a> (0)</li><li>September 20, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-session-hijacking-3/" title="Define Session Hijacking">Define Session Hijacking</a> (0)</li><li>October 27, 2008 -- <a href="http://www.bestjavainterviewquestions.com/final-variables-and-methods/" title="Final Variables and Methods">Final Variables and Methods</a> (0)</li><li>August 24, 2008 -- <a href="http://www.bestjavainterviewquestions.com/software-architecture-of-ejb/" title="Software  Architecture of EJB">Software  Architecture of EJB</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/mapping-files-in-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Example of Hibernate</title>
		<link>http://www.bestjavainterviewquestions.com/example-of-hibernate/</link>
		<comments>http://www.bestjavainterviewquestions.com/example-of-hibernate/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 17:54:25 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Hibernate Tutorial]]></category>
		<category><![CDATA[create a class in hibernate]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=324</guid>
		<description><![CDATA[Create the class Create a new class named ?Honey? in the package ?de.laliluna.example?. Add three fields id, name and taste and generate (Context menu -> Source -> Generate Getter and Setter) or type the getters and setters for the fields. Then create an empty constructor. package de.laliluna.example; /** * @author &#8230;&#8230;&#8230;&#8230;.. * */ public class [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Create the class</strong></p>
<p>Create a new class named ?Honey? in the package ?de.laliluna.example?. Add three fields id, name and taste and generate (Context menu -> Source -> Generate Getter and Setter) or type the getters and setters for the fields. Then create an empty constructor.</p>
<p><em><br />
<font size="4">package de.laliluna.example;</p>
<p>/**<br />
 * @author &#8230;&#8230;&#8230;&#8230;..<br />
 *<br />
 */<br />
public class Honey {<br />
	private Integer id;<br />
	private String name;<br />
	private String taste;</p>
<p>	public Honey(){</p>
<p>	}</p>
<p>	/**<br />
	 * @return Returns the id.<br />
	 */<br />
	public Integer getId() {<br />
		return id;<br />
	}<br />
	/**<br />
	 * @param id The id to set.<br />
	 */<br />
	public void setId(Integer id) {<br />
		this.id = id;<br />
	}<br />
	/**<br />
	 * @return Returns the name.<br />
	 */<br />
	public String getName() {<br />
		return name;<br />
	}<br />
	/**<br />
	 * @param name The name to set.<br />
	 */<br />
	public void setName(String name) {<br />
		this.name = name;<br />
	}<br />
	/**<br />
	 * @return Returns the taste.<br />
	 */<br />
	public String getTaste() {<br />
		return taste;<br />
	}<br />
	/**<br />
	 * @param taste The taste to set.<br />
	 */<br />
	public void setTaste(String taste) {<br />
		this.taste = taste;<br />
	}<br />
}<br />
</font><br />
</em></p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>July 24, 2008 -- <a href="http://www.bestjavainterviewquestions.com/jsf-important-questions/" title="JSF Important Questions">JSF Important Questions</a> (2)</li><li>August 24, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-local-interfaces/" title="Define Local Interfaces">Define Local Interfaces</a> (0)</li><li>August 15, 2008 -- <a href="http://www.bestjavainterviewquestions.com/life-cycle-of-thread/" title="Life Cycle of Thread">Life Cycle of Thread</a> (0)</li><li>June 17, 2008 -- <a href="http://www.bestjavainterviewquestions.com/awt-interview-questions-2/" title="AWT Interview Questions">AWT Interview Questions</a> (0)</li><li>August 10, 2008 -- <a href="http://www.bestjavainterviewquestions.com/example-programs-of-lifecycle-of-servlets/" title="Example programs of lifecycle of servlets">Example programs of lifecycle of servlets</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/example-of-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using MySql DataBase in Hibernate</title>
		<link>http://www.bestjavainterviewquestions.com/using-mysql-database-in-hibernate/</link>
		<comments>http://www.bestjavainterviewquestions.com/using-mysql-database-in-hibernate/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 17:48:55 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Hibernate Tutorial]]></category>
		<category><![CDATA[using mysql in hibernate]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=322</guid>
		<description><![CDATA[Create a database with MySql or PostgreSQL or anything you like. Call it ?firsthibernate?. Using PostgreSql use the following script to create your table: CREATE TABLE &#8220;public&#8221;.&#8221;honey&#8221; ( id SERIAL, name text, taste text, PRIMARY KEY(id) ); Using MySql use the following script: CREATE TABLE `honey` ( `id` int(11) NOT NULL auto_increment, `name` varchar(250) default [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Create a database with MySql or PostgreSQL or anything you like. Call it ?firsthibernate?.</strong></p>
<p>Using PostgreSql use the following script to create your table:</p>
<p><em><font size="4">CREATE TABLE &#8220;public&#8221;.&#8221;honey&#8221; (<br />
  id SERIAL,<br />
  name text,<br />
  taste text,<br />
  PRIMARY KEY(id)<br />
);</font></em></p>
<p><strong>Using MySql use the following script:</strong></p>
<p><em><font size="3">CREATE TABLE `honey` (<br />
  `id` int(11) NOT NULL auto_increment,<br />
  `name` varchar(250) default NULL,<br />
  `taste` varchar(250) default NULL,<br />
  PRIMARY KEY  (`id`)<br />
) ENGINE=MyISAM CHARSET=latin1</font></em></p>
<p><strong>MySQL Version:</strong></p>
<p><em><font size="3"><?xml version="1.0" encoding="UTF-8"?><br />
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" ><br />
<hibernate-mapping><br />
 <class name="de.laliluna.example.Honey" table="honey"><br />
 <id name="id" column="id" type="java.lang.Integer"><br />
 <generator class="increment"/><br />
 </id></p>
<property name="name" column="name" type="java.lang.String" />
<property name="taste" column="taste" type="java.lang.String" />
 </class><br />
</hibernate-mapping></font></em></p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>June 8, 2008 -- <a href="http://www.bestjavainterviewquestions.com/applet-interview-question/" title="Applet Interview Question">Applet Interview Question</a> (4)</li><li>August 24, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-business-logic-and-business-method/" title="Define business logic and business method">Define business logic and business method</a> (0)</li><li>May 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/core-java-interview-questions/" title="Core Java Interview Questions">Core Java Interview Questions</a> (1)</li><li>February 15, 2009 -- <a href="http://www.bestjavainterviewquestions.com/providing-current-date-and-time-using-stateless-session-bean/" title="Providing current date and time using Stateless session bean">Providing current date and time using Stateless session bean</a> (0)</li><li>September 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/difference-between-print-and-println-methods-in-java/" title="Difference between Print() and Println() Methods in Java">Difference between Print() and Println() Methods in Java</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/using-mysql-database-in-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding a DataBase Driver in Hibernate</title>
		<link>http://www.bestjavainterviewquestions.com/adding-a-database-driver-in-hibernate/</link>
		<comments>http://www.bestjavainterviewquestions.com/adding-a-database-driver-in-hibernate/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 17:46:13 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Hibernate Tutorial]]></category>
		<category><![CDATA[database driver in hibernate]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=320</guid>
		<description><![CDATA[Add the database driver Even Hibernate needs a database driver to access a database. Open the project properties, click on ?Java Build Path?, select ?Add External Jars? and add your database driver. When you use PostgreSQL you can find your database driver on http://jdbc.postgresql.org when you use MySQL have a look here http://www.mysql.de/products/connector/j Create database [...]]]></description>
			<content:encoded><![CDATA[<p><strong><font size="4">Add the database driver</font></strong></p>
<p>Even Hibernate needs a database driver to access a database. Open the project properties, click on ?Java Build Path?, select ?Add External Jars? and add your database driver. When you use PostgreSQL you can find your database driver on http://jdbc.postgresql.org when you use MySQL have a look here http://www.mysql.de/products/connector/j<br />
Create database and tables.</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>July 7, 2008 -- <a href="http://www.bestjavainterviewquestions.com/jsp-standard-questions/" title="JSP Standard Questions">JSP Standard Questions</a> (0)</li><li>September 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-is-default-package-in-java/" title="What is Default Package in Java">What is Default Package in Java</a> (0)</li><li>July 15, 2008 -- <a href="http://www.bestjavainterviewquestions.com/springs-interview-questions/" title="Springs Interview Questions">Springs Interview Questions</a> (0)</li><li>July 7, 2008 -- <a href="http://www.bestjavainterviewquestions.com/jsp-interview-questions-2/" title="JSP Interview Questions">JSP Interview Questions</a> (1)</li><li>September 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-are-command-line-arguments-in-java/" title="What are Command Line Arguments in Java">What are Command Line Arguments in Java</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/adding-a-database-driver-in-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Configuring the Log4J for Hibernate</title>
		<link>http://www.bestjavainterviewquestions.com/configuring-the-log4j-for-hibernate/</link>
		<comments>http://www.bestjavainterviewquestions.com/configuring-the-log4j-for-hibernate/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 17:43:38 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Hibernate Tutorial]]></category>
		<category><![CDATA[configure log4j in hibernate]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=317</guid>
		<description><![CDATA[Configuring Log4J As you can see above we added the log4j library. This library does like a configuration file in the source directory or it welcomes you with the following error. log4j:WARN No appenders could be found for logger (TestClient). log4j:WARN Please initialize the log4j system properly. Create a file named log4j.properties in the root [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Configuring Log4J</strong></p>
<p>As you can see above we added the log4j library. This library does like a configuration file in the source directory or it welcomes you with the following error.</p>
<p>log4j:WARN No appenders could be found for logger (TestClient).<br />
log4j:WARN Please initialize the log4j system properly.</p>
<p>Create a file named log4j.properties in the root directory and insert the following:</p>
<p>### direct log messages to stdout ###<br />
log4j.appender.stdout=org.apache.log4j.ConsoleAppender<br />
log4j.appender.stdout.Target=System.out<br />
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout<br />
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L &#8211; %m%n</p>
<p>### set log levels &#8211; for more verbose logging change &#8216;info&#8217; to &#8216;debug&#8217; ###</p>
<p>log4j.rootLogger=debug, stdout</p>
<p>log4j.logger.org.hibernate=info<br />
#log4j.logger.org.hibernate=debug</p>
<p>### log HQL query parser activity<br />
#log4j.logger.org.hibernate.hql.ast.AST=debug</p>
<p>### log just the SQL<br />
log4j.logger.org.hibernate.SQL=debug</p>
<p>### log JDBC bind parameters ###<br />
log4j.logger.org.hibernate.type=info</p>
<p>### log schema export/update ###<br />
log4j.logger.org.hibernate.tool.hbm2ddl=info</p>
<p>### log HQL parse trees<br />
#log4j.logger.org.hibernate.hql=debug</p>
<p>### log cache activity ###<br />
log4j.logger.org.hibernate.cache=info</p>
<p>### log transaction activity<br />
#log4j.logger.org.hibernate.transaction=debug</p>
<p>### log JDBC resource acquisition<br />
#log4j.logger.org.hibernate.jdbc=debug</p>
<p>### enable the following line if you want to track down connection ###<br />
### leakages when using DriverManagerConnectionProvider ###<br />
#log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>August 24, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-are-synchronized-methods-and-synchronized-statements/" title="What are synchronized methods and synchronized statements?">What are synchronized methods and synchronized statements?</a> (0)</li><li>December 16, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-array-in-java/" title="Define Array in Java">Define Array in Java</a> (1)</li><li>August 9, 2008 -- <a href="http://www.bestjavainterviewquestions.com/some-important-methos-in-applets/" title="Some important methos in Applets">Some important methos in Applets</a> (0)</li><li>June 23, 2008 -- <a href="http://www.bestjavainterviewquestions.com/19/" title="Java Basic Questions">Java Basic Questions</a> (1)</li><li>August 24, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-local-interfaces/" title="Define Local Interfaces">Define Local Interfaces</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/configuring-the-log4j-for-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a SessionFactory Using Hibernate</title>
		<link>http://www.bestjavainterviewquestions.com/creating-a-sessionfactory-using-hibernate/</link>
		<comments>http://www.bestjavainterviewquestions.com/creating-a-sessionfactory-using-hibernate/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 17:32:22 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Hibernate Tutorial]]></category>
		<category><![CDATA[session factory in hibernate]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=314</guid>
		<description><![CDATA[Create a SessionFactory A session factory is important for Hibernate. It implements a design pattern, that ensures that only one instance of the session is used per thread. You should only get your Hibernate session from this factory. Create a class named HibernateSessionFactory in the package de.laliluna.hibernate and add the source code below. /** * [...]]]></description>
			<content:encoded><![CDATA[<p><strong><font size="4">Create a SessionFactory</font></strong></p>
<p>A session factory is important for Hibernate. It implements a design pattern, that ensures that only one instance of the session is used per thread. You should only get your Hibernate session from this factory.</p>
<p>Create a class named HibernateSessionFactory in the package de.laliluna.hibernate and add the source code below.</p>
<p>/**</p>
<p>*</p>
<p>* @author</p>
<p>* created Feb 22, 2006</p>
<p>*/</p>
<p>package de.laliluna.hibernate;</p>
<p>import javax.naming.InitialContext;</p>
<p>import org.apache.log4j.Logger;</p>
<p>import org.hibernate.HibernateException;</p>
<p>import org.hibernate.Session;</p>
<p>import org.hibernate.SessionFactory;</p>
<p>import org.hibernate.cfg.Configuration;</p>
<p>import org.hibernate.cfg.Environment;</p>
<p>/**</p>
<p>* @author says This class guaranties that only one single SessionFactory</p>
<p>*         is instanciated and that the configuration is done thread safe as</p>
<p>*         singleton. Actually it only wraps the Hibernate SessionFactory.</p>
<p>*         When a JNDI name is configured the session is bound to to JNDI,</p>
<p>*         else it is only saved locally.</p>
<p>*         You are free to use any kind of JTA or Thread transactionFactories.</p>
<p>*/</p>
<p>public class InitSessionFactory {</p>
<p>/**</p>
<p>* Default constructor.</p>
<p>*/</p>
<p>private InitSessionFactory() {</p>
<p>}</p>
<p>/**</p>
<p>* Location of hibernate.cfg.xml file. NOTICE: Location should be on the</p>
<p>* classpath as Hibernate uses #resourceAsStream style lookup for its</p>
<p>* configuration file. That is place the config file in a Java package &#8211; the</p>
<p>* default location is the default Java package.&lt;br&gt;</p>
<p>* &lt;br&gt;</p>
<p>* Examples: &lt;br&gt;</p>
<p>* &lt;code&gt;CONFIG_FILE_LOCATION = &#8220;/hibernate.conf.xml&#8221;.</p>
<p>* CONFIG_FILE_LOCATION = &#8220;/com/foo/bar/myhiberstuff.conf.xml&#8221;.&lt;/code&gt;</p>
<p>*/</p>
<p>private static String CONFIG_FILE_LOCATION = &#8220;/hibernate.cfg.xml&#8221;;</p>
<p>/** The single instance of hibernate configuration */</p>
<p>private static final Configuration cfg = new Configuration();</p>
<p>/** The single instance of hibernate SessionFactory */</p>
<p>private static org.hibernate.SessionFactory sessionFactory;</p>
<p>/**</p>
<p>* initialises the configuration if not yet done and returns the current</p>
<p>* instance</p>
<p>*</p>
<p>* @return</p>
<p>*/</p>
<p>public static SessionFactory getInstance() {</p>
<p>if (sessionFactory == null)</p>
<p>initSessionFactory();</p>
<p>return sessionFactory;</p>
<p>}</p>
<p>/**</p>
<p>* Returns the ThreadLocal Session instance. Lazy initialize the</p>
<p>* &lt;code&gt;SessionFactory&lt;/code&gt; if needed.</p>
<p>*</p>
<p>* @return Session</p>
<p>* @throws HibernateException</p>
<p>*/</p>
<p>public Session openSession() {</p>
<p>return sessionFactory.getCurrentSession();</p>
<p>}</p>
<p>/**</p>
<p>* The behaviour of this method depends on the session context you have</p>
<p>* configured. This factory is intended to be used with a hibernate.cfg.xml</p>
<p>* including the following property &lt;property</p>
<p>* name=&#8221;current_session_context_class&#8221;&gt;thread&lt;/property&gt; This would return</p>
<p>* the current open session or if this does not exist, will create a new</p>
<p>* session</p>
<p>*</p>
<p>* @return</p>
<p>*/</p>
<p>public Session getCurrentSession() {</p>
<p>return sessionFactory.getCurrentSession();</p>
<p>}</p>
<p>/**</p>
<p>* initializes the sessionfactory in a safe way even if more than one thread</p>
<p>* tries to build a sessionFactory</p>
<p>*/</p>
<p>private static synchronized void initSessionFactory() {</p>
<p>/*</p>
<p>* [laliluna] check again for null because sessionFactory may have been</p>
<p>* initialized between the last check and now</p>
<p>*</p>
<p>*/</p>
<p>Logger log = Logger.getLogger(InitSessionFactory.class);</p>
<p>if (sessionFactory == null) {</p>
<p>try {</p>
<p>cfg.configure(CONFIG_FILE_LOCATION);</p>
<p>String sessionFactoryJndiName = cfg</p>
<p>.getProperty(Environment.SESSION_FACTORY_NAME);</p>
<p>if (sessionFactoryJndiName != null) {</p>
<p>cfg.buildSessionFactory();</p>
<p>log.debug(&#8220;get a jndi session factory&#8221;);</p>
<p>sessionFactory = (SessionFactory) (new InitialContext())</p>
<p>.lookup(sessionFactoryJndiName);</p>
<p>} else{</p>
<p>log.debug(&#8220;classic factory&#8221;);</p>
<p>sessionFactory = cfg.buildSessionFactory();</p>
<p>}</p>
<p>} catch (Exception e) {</p>
<p>System.err</p>
<p>.println(&#8220;%%%% Error Creating HibernateSessionFactory %%%%&#8221;);</p>
<p>e.printStackTrace();</p>
<p>throw new HibernateException(</p>
<p>&#8220;Could not initialize the Hibernate configuration&#8221;);</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p>public static void close(){</p>
<p>if (sessionFactory != null)</p>
<p>sessionFactory.close();</p>
<p>sessionFactory = null;</p>
<p>}</p>
<p>}</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>September 22, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-is-jre-explain/" title="What is JRE ? Explain">What is JRE ? Explain</a> (1)</li><li>July 15, 2008 -- <a href="http://www.bestjavainterviewquestions.com/springs-interview-questions-2/" title="Springs Interview Questions">Springs Interview Questions</a> (7)</li><li>July 7, 2008 -- <a href="http://www.bestjavainterviewquestions.com/ejb-interview-questions/" title="EJB INTERVIEW QUESTIONS">EJB INTERVIEW QUESTIONS</a> (1)</li><li>September 2, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-session/" title="Define Session">Define Session</a> (0)</li><li>August 15, 2008 -- <a href="http://www.bestjavainterviewquestions.com/what-is-thread-how-to-use-threads/" title="What is Thread, How to Use Threads?">What is Thread, How to Use Threads?</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/creating-a-sessionfactory-using-hibernate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hibernate Introduction</title>
		<link>http://www.bestjavainterviewquestions.com/hibernate-introduction/</link>
		<comments>http://www.bestjavainterviewquestions.com/hibernate-introduction/#comments</comments>
		<pubDate>Wed, 17 Sep 2008 17:24:35 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Hibernate Tutorial]]></category>
		<category><![CDATA[hibernate introduction]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=311</guid>
		<description><![CDATA[Short introduction Hibernate is a solution for object relational mapping and a persistence management solution or persistent layer. This is probably not understandable for anybody learning Hibernate. What you can imagine is probably that you have your application with some functions (business logic) and you want to save data in a database. When you use [...]]]></description>
			<content:encoded><![CDATA[<p><strong><span style="font-size: small;">Short introduction</span></strong></p>
<p>Hibernate is a solution for object relational mapping and a persistence management solution or persistent layer. This is probably not understandable for anybody learning Hibernate.</p>
<p>What you can imagine is probably that you have your application with some functions (business logic) and you want to save data in a database. When you use Java all the business logic normally works with objects of different class types. Your database tables are not at all objects.</p>
<p>Hibernate provides a solution to map database tables to a class. It copies one row of the database data to a class. In the other direction it supports to save objects to the database. In this process the object is transformed to one or more tables.</p>
<p>Saving data to a storage is called persistence. And the copying of tables to objects and vice versa is called object relational mapping.</p>
<p><strong><span style="font-size: small;">Create a Java Project</span></strong></p>
<p>Using Eclipse press the keys Ctrl+n (Strg+n) to create a new project. Select a Java project. We will call it FirstHibernateExample.</p>
<p><strong><span style="font-size: small;">Prepare the project for Hibernate using MyEclipse</span></strong></p>
<p>When you are using MyEclipse, right click on your project in the package explorer and choose Add Hibernate capabilities.</p>
<p>&#8211;&gt;Select the Package Explorer in the Left Side of  the EclipseIDE and in that<br />
&#8211;&gt;Select FirstHibernateProgram<br />
|<br />
|___Select MyEclipse<br />
|<br />
|__Add Hibernate Capabilities</p>
<p>After that</p>
<p>Continue the wizard and create a new hibernate.cfg.xml in the src directory.</p>
<p>In the last step you can create a Hibernate SessionFactory. I prefer to create my own. You can find it below.<br />
Prepare the project for Hibernate for anybody</p>
<p>When you do not use MyEclipse download Hibernate from the website <a href="http://www.hibernate.org/">http://www.hibernate.org/</a></p>
<p>Extract the file. Hibernate comes with a long list of libraries. You do not need all of them. There is a REAME file in the lib directory explaining what is required. Open your project properties, select ?Java Build Path?, click on ?Add External Jars? and add the libaries shown below to your project path.<br />
&#8212;&gt;xml-apis.jar<br />
&#8212;&gt;asm.jar<br />
&#8212;&gt;asm-attrs.jar<br />
&#8212;&gt;c3p0-0.9.0.jar<br />
&#8212;&gt;cglib-2.1.3.jar<br />
&#8212;&gt;commons-logging-1.0.4.jar<br />
&#8212;&gt;concurrent-1.3.2.jar<br />
&#8212;&gt;dom4j-1.6.1.jar<br />
&#8212;&gt;ehcache-1.1.jar<br />
&#8212;&gt;hibernate3.jar<br />
&#8212;&gt;jacc-1_0-fr.jar<br />
&#8212;&gt;jaxen-1.1-beta-7.jar<br />
&#8212;&gt;jdbc2_0-stdext.jar<br />
&#8212;&gt;jta.jar<br />
&#8212;&gt;log4j-1.2.11.jar<br />
&#8212;&gt;antlr-2.7.6rc1.jar</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>October 5, 2008 -- <a href="http://www.bestjavainterviewquestions.com/difference-between-errortag-and-message-tag/" title="Difference between errortag and message tag">Difference between errortag and message tag</a> (0)</li><li>October 5, 2008 -- <a href="http://www.bestjavainterviewquestions.com/explain-executequery-method/" title="Explain executeQuery Method">Explain executeQuery Method</a> (0)</li><li>September 20, 2008 -- <a href="http://www.bestjavainterviewquestions.com/310/" title="Hibernate Basics">Hibernate Basics</a> (0)</li><li>August 24, 2008 -- <a href="http://www.bestjavainterviewquestions.com/define-servletoutputstream-object-in-jsp/" title="Define ServletOutputStream object in Jsp">Define ServletOutputStream object in Jsp</a> (0)</li><li>July 21, 2010 -- <a href="http://www.bestjavainterviewquestions.com/explain-briefly-about-serialization/" title="Explain Briefly about Serialization">Explain Briefly about Serialization</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/hibernate-introduction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Converting Hibernate with Eclipse Integration</title>
		<link>http://www.bestjavainterviewquestions.com/converting-hibernate-with-eclipse-integration/</link>
		<comments>http://www.bestjavainterviewquestions.com/converting-hibernate-with-eclipse-integration/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 03:05:21 +0000</pubDate>
		<dc:creator>Ramakrishna</dc:creator>
				<category><![CDATA[Hibernate Tutorial]]></category>
		<category><![CDATA[Hibernate and Eclipse Integration]]></category>

		<guid isPermaLink="false">http://www.bestjavainterviewquestions.com/?p=308</guid>
		<description><![CDATA[Recently I had the experience of converting a Java project in Eclipse 3.1 to a plugin and RCP. Previously working Hibernate activity failed with errors indicating that the hibernate.cfg.xml file could not be located. This led me on massive flailing with plugin classpaths, to no avail, since this was not the problem. The Eclipse class [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had the experience of converting a Java project in Eclipse 3.1 to a plugin and RCP. Previously working Hibernate activity failed with errors indicating that the hibernate.cfg.xml file could not be located. This led me on massive flailing with plugin classpaths, to no avail, since this was not the problem.</p>
<p>The Eclipse class loading mechanism and problems with integrating with third party libraries has been the subject of much discussion, and previous solutions have involved using Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader()) in the appropriate places, which are not obvious to me nor to most naive users. However, Eclipse 3.1 has a &#8220;buddy loading&#8221; facility that solves this problem nearly trivially. Credit for telling me the solution belongs to snpesnpe, but is presented here to help with this problem.</p>
<p>The issue is that the Hibernate libraries cannot know, in advance, where a user will install relevant code, particularly the hibernate.cfg.xml and related hbm.xml files. Placing the hibernate.cfg.xml file at the root of the classpath is the usual solution, but since the Hibernate libraries don&#8217;t even know that your plugin exists, there is no way for them to know what classpath to explore for this file. Enter the buddy system.</p>
<p>In the MANIFEST.MF file of the Hibernate plugin (which NEEDS the buddy loading), such as org.hibernate.eclipse, add a line:</p>
<p>Eclipse-BuddyPolicy:registered</p>
<p>and in the MANIFEST.MF file of your plugin project or RCP project, add the line:</p>
<p>Eclipse-RegisterBuddy:org.hibernate.eclipse</p>
<p>Important to notice the syntax &#8211; our plugin is willing to be seen by the hibernate library, using Eclipse-RegisterBuddy, and Hibernate is registering itself with Eclipse-BuddyPolicy. While this is stated clearly in the Eclipse Help (in retrospect!) it is critical to get the syntax precisely correct.</p>
<p>Finally, if you are using HibernateUtil as your main entry point into Hibernate, then in your plugin start method add the line:</p>
<p>Class.forName(&#8220;myPlugin.HibernateUtil&#8221;); //full class name should go here</p>
<p>This works &#8211; assumption is that hibernate.cfg.xml is in the src directory of your plugin and this is in the classpath.</p>
<h2  class="related_post_title">Random Posts</h2><ul class="related_post"><li>November 8, 2008 -- <a href="http://www.bestjavainterviewquestions.com/which-of-the-collection-classes-will-not-allow-duplicate-values/" title="Which of the Collection Classes will not allow duplicate values">Which of the Collection Classes will not allow duplicate values</a> (1)</li><li>September 23, 2008 -- <a href="http://www.bestjavainterviewquestions.com/naming-conventions-in-java/" title="Naming Conventions in Java">Naming Conventions in Java</a> (0)</li><li>July 7, 2008 -- <a href="http://www.bestjavainterviewquestions.com/servlet-interview-questions/" title="Servlet Interview Questions">Servlet Interview Questions</a> (1)</li><li>October 4, 2008 -- <a href="http://www.bestjavainterviewquestions.com/how-application-creates-a-connection/" title="How application creates a connection">How application creates a connection</a> (0)</li><li>October 3, 2008 -- <a href="http://www.bestjavainterviewquestions.com/jsp-scriplets-and-templets/" title="Jsp Scriplets and Templets">Jsp Scriplets and Templets</a> (0)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.bestjavainterviewquestions.com/converting-hibernate-with-eclipse-integration/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

