Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.HibernateLearn;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.Configuration;
- public class Client {
- public static void main(String[] args) {
- // creating configuration object
- Configuration cfg = new Configuration();
- cfg.configure("hibernate.cfg.xml");// populates the data of the
- // configuration file
- // creating seession factory object
- SessionFactory factory = cfg.buildSessionFactory();
- // creating session object
- Session session = factory.openSession();
- // creating transaction object
- Transaction t = session.beginTransaction();
- Employee e1 = new Employee();
- e1.setId(103);
- e1.setF_name("Ahammed");
- e1.setL_name("Naseer");
- //e1.getId();
- //e1.getF_name();
- e1.getL_name();
- session.persist(e1);// persisting the object
- t.commit();// transaction is committed
- session.close();
- System.out.println("successfully saved");
- }}
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate
- Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-
- configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="show_sql">true</property>
- <!-- <property name="hbm2ddl.auto">create-update</property> -->
- <!--Below are other values for hbm2ddl.auto validate: validate the schema,
- makes no changes to the database. update: update the schema. create: creates
- the schema, destroying previous data. create-drop: drop the schema at the
- end of the session. -->
- <!--property name="hibernate.cache.use_query_cache">true</property -->
- <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
- <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
- <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/joctopusdb</property>
- <property name="hibernate.connection.username">root</property>
- <property name="hibernate.connection.password">abc123</property>
- <mapping resource="hibernate.hbm.xml"/>
- </session-factory>
- <?xml version='1.0' encoding='UTF-8'?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="com.HibernateLearn.Employee" table="emp_table">
- <id name="Id">
- <generator class="increment"></generator>
- </id>
- <property name="F_name"></property>
- <property name="L_name"></property>
- </class>
- </hibernate-mapping>
Add Comment
Please, Sign In to add comment