1

package com.crudoperations;


import org.hibernate.Session;

import org.hibernate.SessionFactory;

import org.hibernate.Transaction;

import org.hibernate.cfg.Configuration;

import org.hibernate.service.ServiceRegistry;

import org.hibernate.service.ServiceRegistryBuilder;


import com.beans.Student;


public class CRUDMain {

public static void main(String[] args) {

    Configuration cfg = new Configuration();

    cfg.configure("hibernate.cfg.xml");

    ServiceRegistryBuilder service = new ServiceRegistryBuilder();

    ServiceRegistry sRegitry = service.applySettings(cfg.getProperties()).buildServiceRegistry();

    SessionFactory sf = cfg.buildSessionFactory(sRegitry);


    Session session = sf.openSession();

    Transaction tx = session.beginTransaction();


    Student stud = new Student();

    stud.setId(101);

    stud.setSname("abc");

    stud.setEmail("abc@gmail.com");

    stud.setMarks(89);


    //      System.out.println("invoking save() method.");

    //      int pk = (Integer) session.save(stud);      

    //      System.out.println("PK:"+pk);


    System.out.println("invoking saveOrUpdate() method.");

    session.saveOrUpdate(stud);

    tx.commit();

}

};


package com.beans;


public class Student {

private int id;

private String sname;

private String email;

private int marks;


public Student() { }


public int getId() {

    return id;

}


public void setId(int id) {

    this.id = id;

}


public String getSname() {

    return sname;

}


public void setSname(String sname) {

    this.sname = sname;

}


public String getEmail() {

    return email;

}


public void setEmail(String email) {

    this.email = email;

}


public int getMarks() {

    return marks;

}


public void setMarks(int marks) {

    this.marks = marks;

}   

}

|

    2

    0

    |
      0

      |