Hibernate Hello World Program (Hibernate Insert Query)
Mates, here is the first program in hibernate like saving an object into the database (don’t think we are inserting a record into the database ? that is the case in JDBC, in hibernate we are just saving an object into database, means inserting only) hope you got my contention, as of now am giving this as normal console-based java application, actually it’s bit tedious to set the classpath every time for all the jar files but you must know this too.
From the next example, I will give all the applications in the Eclipse
As I told you earlier, these are the files we require to shape a hibernate program.
- Product.java (My POJO class)
- Product.hbm.xml (XML mapping file )
- hibernate.cfg.xml (XML configuration file)
- ClientForSave.java (java file to write our hibernate logic)
Product.java:
private int productId; private String proName; private double price; public void setProductId(int productId) { this.productId = productId; } public int getProductId() { return productId; } public void setProName(String proName) { this.proName = proName; } public String getProName() { return proName; } public void setPrice(double price) { this.price = price; } public double getPrice() { return price; }
Product.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="Product" table="PRODUCTS"> <id name="productId" column="pid" > <generator class="assigned" /> </id> <property name="proName" column="pname" /> <property name="price"/> </class> </hibernate-mapping>
In this mapping file, my Product class is linked with PRODUCTS table in the database, and next is the id element, means in the database table what column we need to take as primary key column, that property name we need to live here, actually, I have been given my property name productId which will mapped with PID column in the table.
And proName is mapped with pname column of the PRODUCTS table, see I have not specified any column for the property price, this means that our property name in the POJO class and the column name in the table both are same.
Remember: the first 3 lines are the DTD for the mapping file, as a programmer no need to remember but we need to be very careful while you are copying this DTD, program may not be executed if you write DTD wrong we have separate DTD for Mapping XML and Configuration XML files.
hibernate.cfg.xml:
<?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> <!-- Related to the connection START --> <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver </property> <property name="connection.url">jdbc:oracle:thin:@www.java4s.com:1521:XE</property> <property name="connection.user">user</property> <property name="connection.password">password</property> <!-- Related to the connection END --> <!-- Related to hibernate properties START --> <property name="show_sql">true </property> <property name="dialet">org.hibernate.dialect.OracleDialect </property> <property name="hbm2ddl.auto">update </property> <!-- Related to hibernate properties END --> <!-- Related to mapping START --> <mapping resource="product.hbm.xml" /> <!-- Related to the mapping END --> </session-factory> </hibernate-configuration>
In this configuration file, I have been given my Oracle database connection properties, if you are using MySql then just specify your database related details it depends on you.
ClientForSave.java
import org.hibernate.*; import org.hibernate.cfg.*; public class ClientForSave { public static void main(String[] args) { Configuration cfg = new Configuration(); cfg.configure("hibernate.cfg.xml"); SessionFactory factory = cfg.buildSessionFactory(); Session session = factory.openSession(); Product p=new Product(); p.setProductId(101); p.setProName("iPhone"); p.setPrice(25000); Transaction tx = session.beginTransaction();
session.save(p);
System.out.println("Object saved successfully.....!!"); tx.commit(); session.close(); factory.close(); } }
Now compile all .java files and run ClientForSave.java and check the output
Note:
- Make sure all .class, .java, .xml files are exist in the same folder
- Before you compile and run this application, ensure you set the classpath for all 12 jars files, this is tedious like what I told you earlier, we can avoid this process from the next example with Eclipse, a real-time tool?
- except for select operation, all other operations must be in the Transaction Scope
post a comment