Monday, September 3, 2012

Hibernate Simple Example

Download distribution  file

http://sourceforge.net/projects/hibernate/files/hibernate3/3.6.10.Final/hibernate-distribution-3.6.10.Final-dist.zip/download

Extract the zip file



Create new java project

then add the  lib folder copy the all the jar







Member.java

package com.jsl.hibernate.demo;
public class Member {

  private int mid;
  private String name;
  private String email;
  private String city;
  
  public void setMid(int mid) {
   this.mid = mid;
  }
  public int getMid() {
   return mid;
  }
  public void setName(String name) {
   this.name = name;
  }
  public String getName() {
   return name;
  }
  public void setEmail(String email) {
   this.email = email;
  }
  public String getEmail() {
   return email;
  }
  public void setCity(String city) {
   this.city = city;
  }
  public String getCity() {
   return city;
  }
}

hibernate.cfg.xml




  
   
        oracle.jdbc.driver.OracleDriver
        jdbc:oracle:thin:@localhost:1521:xe
        java
        java

        
        1

        
        org.hibernate.dialect.OracleDialect


        
        true

        
        create

        

    

 

member.hbm.xml




  
    
     
    
    
    
    
  

Manager.java

package com.jsl.hibernate.demo;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class Manager {
  public static void main(String[] args) {
   Member member=new Member();
   member.setMid(1001);
   member.setName("Krish");
   member.setEmail("krish@gmail.com");
   member.setCity("Bangalore");
   
   SessionFactory factory=new Configuration().configure().buildSessionFactory();
   Session session=factory.openSession();
   org.hibernate.Transaction tx=session.beginTransaction();
   session.save(member);
   tx.commit();
   session.close();
  }
}
if run the manger.java the output will be
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

Hibernate: insert into MEMBER (NAME, EMAIL, CITY, MID) values (?, ?, ?, ?)

No comments:

Post a Comment

Spring Boot 3 : JWT with SecurityFilterChain, AuthorizeHttpRequests, RequestMatchers

pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"...