Category Archives: hibernate

Configure Hibernate for Jdeveloper


Here is the way to configure hibernate to work on JDeveloper

First thing first

As you know the three main things in setting up any hibernate is to have

  • Reference to hibernate3.jar library in classpath
  • Having hibernate.cfg.xml file (can be done programmatic way as well)
  • The mapping file (eg. Simple.hbm.xml)

To make this work for JDeveloper, download the latest version of hibernate3

Refer the downloaded location in the projects library and classpath by Adding a Library(Add Library)  – Create New – Add Entry


Provide the Library Name as ‘Hibernate’ (can be of your choice) and add the following libraries

  • hibernate3.jar
  • antlr-2.7.6.jar
  • commons-collections-3.1.jar
  • dom4j-1.6.1.jar
  • javassist-3.4.GA.jar
  • jta-1.1.jar
  • slf4j-api-1.5.2.jar

Have the hibernate.cfg.xml file in your current working directory


<?xml version='1.0' encoding='utf-8'?>

<!--<span class="hiddenSpellError" pre=""-->DOCTYPE hibernate-configuration PUBLIC

"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>

<!-- Database connection settings -->

<property

name="connection.driver_class">oracle.jdbc.driver.OracleDriver

<property

name="connection.url">jdbc:oracle:thin:@localhost:1521:XE

<property name="connection.username">system</property>

<property name="connection.password">fusion</property>

<!-- <span class="hiddenSpellError" pre="">JDBC</span> connection pool (use the built-in) -->

<property name="connection.pool_size">1</property>

<!-- SQL dialect -->

<property

name="dialect">org.hibernate.dialect.OracleDialect</property>

<!-- Enable Hibernate's automatic session context management

-->

<property

name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->

<property

name="cache.provider_class">org.hibernate.cache.NoCacheProvider

</property>

<!-- Echo all executed SQL to <span class="hiddenSpellError" pre="to ">stdout</span> -->

<property name="show_sql">true</property>

<!-- Drop and re-create the database schema on startup -->

<property name="hbm2ddl.auto">create</property>

<mapping resource="Simple.hbm.xml"/>

</session-factory>

</hibernate-configuration>

The configuration file is for Oracle driver.. this can be changed


<property

name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>

<property

name="connection.url">jdbc:oracle:thin:@localhost:1521:XE

<property name="connection.username">system</property>

<property name="connection.password">fusion</property>

You have to specify the mapping file also


<mapping resource="Simple.hbm.xml"/>

the highlighted libraries are added from slf4j to overcome

As the final step, represent the db table of yours as a simple pojo class (Simple.java) like


public class Simple {

private Long id;

private String title;

private Date date;

public Simple() {}

public Long getId() {

return id;

}

private void setId(Long id) {

this.id = id;

}

public Date getDate() {

return date;

}

public void setDate(Date date) {

this.date = date;

}

public String getTitle() {

return title;

}

public void setTitle(String title) {

this.title = title;

}

private Set participants = new HashSet();

public Set getParticipants() {

return participants;

}

public void setParticipants(Set participants) {

this.participants = participants;

}

}

where id, title and date represents the column name of the table in the db

Have a corresponding mapping file in the same qualified path (Simple.hbm.xml)


<?xml version="1.0"?>

<!--<span class="hiddenSpellError" pre=""-->DOCTYPE hibernate-mapping PUBLIC

"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>

<class table="EVENTS">

<id name="id" column="EVENT_ID">

<generator/>

</id>

<property name="date" column="EVENT_DATE"/>

<property name="title"/>

</class>

</hibernate-mapping>

Have a configFile.java where you instantiate the Configuration, Session and get involved in the transaction


import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

public class ConfigFile {

static Session session;

public ConfigFile() {

super();

}

public static void main(String[] args){

Configuration cfg = new Configuration().configure();

SessionFactory sessions = cfg.buildSessionFactory();

Session sess = sessions.openSession();

session = sess;

Simple s = new Simple();

s.setTitle("hi");

session.save(s);

}

}

Run the configFile.java.. that’s it. 🙂

Advertisements