Studyon Minte9.com
Hibernate




Example (insert)



INSERT INTO employee VALUES ("Zara", "Ali", 1000);


* src/testexample/Testexample.java

	%java
		package testexample;

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

		public class Testexample {
		    
		    private static SessionFactory factory;
		    
		    public static void main(String args[]) {
			
			try { // DB connect
			    factory = new Configuration().configure().buildSessionFactory();
			} catch (Exception e) { 
			    e.printStackTrace();
			}
			
			Testexample app = new Testexample();
			app.addEmployee("Zara", "Ali", 1000); // add employee
		    }
		    
		    public Integer addEmployee(String fname, String lname, int salary) {
			
			Session session = factory.openSession();
			Transaction ts = null;
			Integer employeeID = null;
			
			try { 
			    ts = session.beginTransaction();
			    Employee employee = new Employee(fname, lname, salary);
			    employeeID = (Integer) session.save(employee);
			    ts.commit();
			} catch (HibernateException e) {
			    if (ts != null) ts.rollback();
			    e.printStackTrace(); 
			} finally {
			    session.close(); 
			}
			
			return employeeID;
		    }
		}


http://www.tutorialspoint.com/hibernate/hibernate_examples.htm