Guest User

Untitled

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