Hibernate Working Flow
1. Filer structure should be like Above
2.hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/test
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">charan</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml" />
</session-factory>
</hibernate-configuration>
3.contact.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="hibernate.Contact" table="emp">
<id name="Eno" column="eno">
<generator />
</id>
<property name="Ename">
<column name="ename" />
</property>
<property name="Sal">
<column name="sal" />
</property>
</class>
</hibernate-mapping>
4.Contact.java
package hibernate;
public class Contact {
private String ename;
private int eno;
private int sal;
private long id;
public String getEname() {
return ename;
}
public int getSal() {
return sal;
}
public int getEno() {
return eno;
}
public void setSal(String string) {
sal = Integer.parseInt(string);
}
public void setEname(String string) {
ename = string;
}
public void setEno(int string) {
eno = string;
}
}
5. FirstExample.java
package hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class FirstExample {
public static void main(String[] args) {
Session session = null;
try{
System.out.println("Hello");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
System.out.println("Inserting Record");
Transaction tx = session.beginTransaction();
Contact contact = new Contact();
contact.setEname("karupu");
contact.setSal("5000");
contact.setEno(20);
session.save(contact);
tx.commit();
System.out.println("Done");
}catch(Exception e){
e.printStackTrace();
}finally{
if (session != null) {
session.flush();
session.close();
}
}
}
}
1. Filer structure should be like Above
2.hibernate.cfg.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/test
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">charan</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="contact.hbm.xml" />
</session-factory>
</hibernate-configuration>
3.contact.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="hibernate.Contact" table="emp">
<id name="Eno" column="eno">
<generator />
</id>
<property name="Ename">
<column name="ename" />
</property>
<property name="Sal">
<column name="sal" />
</property>
</class>
</hibernate-mapping>
4.Contact.java
package hibernate;
public class Contact {
private String ename;
private int eno;
private int sal;
private long id;
public String getEname() {
return ename;
}
public int getSal() {
return sal;
}
public int getEno() {
return eno;
}
public void setSal(String string) {
sal = Integer.parseInt(string);
}
public void setEname(String string) {
ename = string;
}
public void setEno(int string) {
eno = string;
}
}
5. FirstExample.java
package hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class FirstExample {
public static void main(String[] args) {
Session session = null;
try{
System.out.println("Hello");
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
System.out.println("Inserting Record");
Transaction tx = session.beginTransaction();
Contact contact = new Contact();
contact.setEname("karupu");
contact.setSal("5000");
contact.setEno(20);
session.save(contact);
tx.commit();
System.out.println("Done");
}catch(Exception e){
e.printStackTrace();
}finally{
if (session != null) {
session.flush();
session.close();
}
}
}
}