Example: Insertion of a record into employee table of oracle database using java
hibernate application
1. hibernate3.jar [available in hibernate-home directory]
2. Dependent jar files of hibernate3.jar [available in hibernate-home\lib folder] 1. dom4j-version.jar 2. cglib-version.jar 3. commons-collections- version.jar 4. commons-logging-version.jar 5. jta.jar 6. asm.jar 7. antlr- version.jar 3. classes12.jar [to support oracle type 4 driver for oracle9i] (available in oraclehome\ora92\jdbc\lib\) or ojdbc driver 6 or 7 depending up on version.
2. Employee.java [persistent class/POJO] (All gets and sets will be written or
added here)
3. Employee.hbm.xml [Hibernate Mapping file]
4. Client.java [Java application using hibernate API & setup to interact with Database software.]
Step1: Create table in oracle database software as follows:
create table employee ( eid number primary key, firstname varchar2(20),
lastname varchar2(20), email varchar2(20) ); Step2: Create hibernate configuration file as follows:[Note: For reference open file from Hibernatehome\etc\hibernate.cfg.xml
hibernate.cfg.xml <!DOCTYPE hibernate-configuration PUBLIC "-
//Employee.java public class Employee implements java.io.Serializable { int no;
String fname, lname, email; public void setNo(int no) { this.no = no; } public int getNo() { return no; } public void setFname(String fname) { this.fname = fname; } public String getFname() { return fname; } public void setLname(String lname) { this.lname = lname; } public String getLname(){ return lname; } public void setEmail(String email){ this.email = email; } public String getEmail(){ return email; } }//class Step 4: Develop Hibernate Mapping file (Employee.hbm.xml) Note: Take reference file from Hibernate-home\eg\*.hbm.xml Employee.hbm.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="Employee" table="employee"> <id name="no" column = "eid"/> <!-- Singular primary key field --> <property name="fname" column="firstname"/> <property name="lname" column = "lastname"/> <property name = "email" column = "email"/> </class> </hibernate-mapping>
Note: To see these SQL queries as log messages use hibernate.show_sql
property in hibernate configuration file as shown below.
<property name = hibernate.show_sql>true</property>
Step 5: Develop hibernate based java application with persistence logic
operations. [Insertion of record] Client.java import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.Transaction; public class TestClient { public static void main(String[] args) throws Exception { //locate and read configuration file Configuration conf = new Configuration(); conf = conf.configure(); //if we name configuration file differently then we need to specify it as a argument //conf = conf.configure(oracle.cfg.xml); //creation of session factory object based on configuration file details SessionFactory factory = conf.buildSessionFactory();
//get session object Session ses = factory.openSession(); Transaction tx =
ses.beginTransaction(); //object creation with data Employee e1 = new Employee(); //Transient state e1.setNo(1); e1.setFname("Vara"); e1.setLname("Prasad"); e1.setEmail("kanakavaraprasad@gmail.com"); //Note: Even after storing data, the object e1 is in transient state. //record insertion ses.save(e1); //now the object e1 is in persistent state. tx.commit(); ses.close(); //closes statement and connection objects //Note: Now the object e1 is in transient state. factory.close(); //destroys entire connection pool
Learn Python Programming for Beginners: Best Step-by-Step Guide for Coding with Python, Great for Kids and Adults. Includes Practical Exercises on Data Analysis, Machine Learning and More.