Guest User

Untitled

a guest
Dec 14th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. package com.HibernateLearn;
  2. import org.hibernate.Session;
  3. import org.hibernate.SessionFactory;
  4. import org.hibernate.Transaction;
  5. import org.hibernate.cfg.Configuration;
  6. public class Client {
  7. public static void main(String[] args) {
  8.  
  9. // creating configuration object
  10. Configuration cfg = new Configuration();
  11. cfg.configure("hibernate.cfg.xml");// populates the data of the
  12. // configuration file
  13.  
  14. // creating seession factory object
  15. SessionFactory factory = cfg.buildSessionFactory();
  16.  
  17. // creating session object
  18. Session session = factory.openSession();
  19.  
  20. // creating transaction object
  21. Transaction t = session.beginTransaction();
  22.  
  23. Employee e1 = new Employee();
  24. e1.setId(103);
  25. e1.setF_name("Ahammed");
  26. e1.setL_name("Naseer");
  27. //e1.getId();
  28. //e1.getF_name();
  29. e1.getL_name();
  30. session.persist(e1);// persisting the object
  31.  
  32. t.commit();// transaction is committed
  33. session.close();
  34.  
  35. System.out.println("successfully saved");
  36.  
  37. }}
  38.  
  39. <?xml version="1.0" encoding="UTF-8"?>
  40. <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate
  41. Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-
  42. configuration-3.0.dtd">
  43. <hibernate-configuration>
  44. <session-factory>
  45. <property name="show_sql">true</property>
  46. <!-- <property name="hbm2ddl.auto">create-update</property> -->
  47. <!--Below are other values for hbm2ddl.auto validate: validate the schema,
  48. makes no changes to the database. update: update the schema. create: creates
  49. the schema, destroying previous data. create-drop: drop the schema at the
  50. end of the session. -->
  51. <!--property name="hibernate.cache.use_query_cache">true</property -->
  52. <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
  53. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  54. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/joctopusdb</property>
  55. <property name="hibernate.connection.username">root</property>
  56. <property name="hibernate.connection.password">abc123</property>
  57. <mapping resource="hibernate.hbm.xml"/>
  58. </session-factory>
  59.  
  60. <?xml version='1.0' encoding='UTF-8'?>
  61. <!DOCTYPE hibernate-mapping PUBLIC
  62. "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
  63. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
  64. <hibernate-mapping>
  65. <class name="com.HibernateLearn.Employee" table="emp_table">
  66. <id name="Id">
  67. <generator class="increment"></generator>
  68. </id>
  69.  
  70. <property name="F_name"></property>
  71. <property name="L_name"></property>
  72. </class>
  73. </hibernate-mapping>
Add Comment
Please, Sign In to add comment