iForeRunner
 
   
 
  JAVA  
   
   
   
 
  Java|J 2EE| Spring FrameWork  
 
  Hibernate example for Java with oracle.

Jars required:

  1. hibernate-annotations-3.4.0.GA/lib/slf4j-api.jar
     

  2. hibernate-annotations-3.4.0.GA/lib/dom4j.jar
     

  3. hibernate-annotations-3.4.0.GA/lib/hibernate-commons-annotations.jar
     

  4. hibernate-annotations-3.4.0.GA/lib/hibernate-core.jar
     

  5. hibernate-annotations-3.4.0.GA/lib/test/slf4j-log4j12.jar
     

  6. hibernate-annotations-3.4.0.GA/lib/test/antlr.jar
     

  7. hibernate-annotations-3.4.0.GA/lib/test/asm.jar
     

  8. hibernate-annotations-3.4.0.GA/lib/test/asm-attrs.jar
     

  9. hibernate-annotations-3.4.0.GA/lib/test/commons-collections.jar
     

  10. hibernate-annotations-3.4.0.GA/lib/test/javassist.jar
     

  11. hibernate-annotations-3.4.0.GA/lib/test/jta.jar
     

  12. hibernate-annotations-3.4.0.GA/lib/test/junit.jar
     

  13. hibernate-annotations-3.4.0.GA/lib/test/log4j.jar
     

  14. app/oracle/product/10.2.0/server/jdbc/lib/ojdbc14.jar
     

  15. app/oracle/product/10.2.0/server/jdbc/lib/ojdbc14_g.jar

1-13 we have downloaded from hibernate.org, 14 and 15 are there in oracle home directory, Imentioned the path so add all the above in class path

Note: Please download both hibernate core and annotations. from www.hibernate.org

 

Firrst Example: There is no order of sequence I followed these number are just count for the number of things we need todo.

1) Hibernate.cfg.xml file


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

<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >

<hibernate-configuration>

<session-factory>

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

<property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:xe</property>

<property name="hibernate.connection.username">hr</property>

<property name="hibernate.connection.password">hr</property>

<property name="hibernate.connection.pool_size">11</property>

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

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

<property name="hibernate.hbm2ddl.auto">update</property>

<!-- Mapping files -->

<mapping resource="message.xml"/>

</session-factory>

</hibernate-configuration>


2) Bean configuration file "message.xml"


<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC

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

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

<hibernate-mapping>

<class name="org.hibernate.Message" table="Messages">

<id name="siNo" type="int" column="SINO" >

<generator class="assigned"/>

</id>

<property name="message">

<column name="MESSAGE"/>

</property>

</class>

</hibernate-mapping>


3) Bean file:


package org.hibernate;

public class Message {

private int siNo;

private String Message;

public String getMessage() {

return Message;

}

public void setMessage(String message) {

Message = message;

}

public int getSiNo() {

return siNo;

}

public void setSiNo(int siNo) {

this.siNo = siNo;

}

}


4) Run the commit using a java file

 

 

package org.hibernate;

import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

// Darur Venugopal

public class HibDemo {

public static void main(String[] args) {

Session session = null;

try {

// This step will read hibernate.cfg.xml

// and prepare hibernate for useSessionFactory sf = new Configuration()

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

System.out.println("111111111111111");

session = sessionFactory.openSession();

System.out.println("ABCD");

// Create new instance of Contact and set

Message message = new Message();

//message.setSiNo(2);

message.setMessage("HELLO WORLD");

session.save(message);

System.out.println("Done");

} catch (Exception e) {

System.out.println(e.getMessage());

} finally {

// Actual contact insertion will happen at this step

 session.flush();

 session.close();

// Above two lines are must

}

}

}

Hibernate's first example for Java with oracle.

 
     
 
 
 
http://www.iforerunner.com/