Hibernate MySQL Insert Data Into Database

hibernate
  • Turgay Can
  • Tarih

    03 Jun, 2012
  • Yorum

    0
  • Görüntüleme

    2249
  • İndirme

    0

Hibernate MySQL Insert Data Into Database

Öncelikle hibernate konfigürasyon dosyamızı oluşturalım.
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
/hibernateExamples</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password"></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="employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Daha sonra basit bir Java Bean sınıfı ekleyelim içinde setter ve getter metodları olan.
Employee.java

package kp.hibernate;

public class Employee {

  private int empId;
  private String empName;
  private int empSal;

  public int getEmpId() {
    return empId;
  }
  public void setEmpId(int empId) {
    this.empId = empId;
  }
  public String getEmpName() {
    return empName;
  }
  public void setEmpName(String empName) {
    this.empName = empName;
  }
  public int getEmpSal() {
    return empSal;
  }
  public void setEmpSal(int empSal) {
    this.empSal = empSal;
  }

}

Şimdi Emplye.java sınıfımız ile Database tablomuzu eşleştirecek konfigürasyon doyamızı hazırlayalım.
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="developerhelpway.hibernate.Employee" table="employee">
<id name="empId" type="int" column="emp_id" >
<generator class="increment"/>
</id>
<property name="empName">
<column name="emp_name" />
</property>
<property name="empSal">
<column name="emp_sal" />
</property>
</class>
</hibernate-mapping>

Kaydetmek için, çalıştıracağımız main class.

package kp.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class CreateHibernateExample {

  /**
   * @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub

    Session sess = null;
    Transaction tran = null;
    try{
      SessionFactory sessFact = new Configuration()
 .configure().buildSessionFactory();
      sess = sessFact.openSession();
      tran = sess.beginTransaction();
      Employee emp = new Employee();
      emp.setEmpName("Birendra Kumar");
      emp.setEmpSal(12000);
      sess.save(emp);
      tran.commit();
    }
    catch(Exception ex){
      ex.printStackTrace();
    }
    finally{
      sess.close();
    }
  }

}

0 Yorum..

Yorum yapmak için "Giriş yapın" yada "Misafir üye" olarak yorum yapabilirsiniz.

Yorum Yap