Guest User

Untitled

a guest
Nov 12th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. <hibernate-mapping>
  2. <class name="contacts.employee" table="contacts">
  3. <meta attribute="class-description"></meta>
  4. <id column="contactId" name="contactId" type="string">
  5. <generator class="assigned"/>
  6. </id>
  7. <property column="contactName" length="100" name="contactName" not-null="true" type="string"/>
  8. <property column="password" length="100" name="password" not-null="true" type="string"/>
  9. <set cascade="all" name="groupOfResponsibilities" table="employee_responsibilty">
  10. <key column="contactId"/>
  11. <many-to-many class="contacts.responsibilities" column="responsibilityId"/>
  12.  
  13. </set>
  14.  
  15. </class>
  16. </hibernate-mapping>
  17.  
  18. <hibernate-mapping>
  19. <class name="contacts.responsibilities" table="responsibilities">
  20. <meta attribute="class-description">
  21. This class list of responsibilities if an employee
  22. </meta>
  23. <id column="responsibilityId" name="responsibilityId" type="long">
  24. <generator class="increment"/>
  25. </id>
  26. <property column="responsibilityName" name="responsibilityName" type="string"/>
  27. </class>
  28. </hibernate-mapping>
  29.  
  30. <hibernate-configuration>
  31. <session-factory>
  32. <property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
  33. <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
  34. <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/****</property>
  35. <property name="hibernate.connection.username">*****</property>
  36. <property name="hibernate.connection.password">*****</property>
  37. <property name="hibernate.hbm2ddl.auto">update</property>
  38. <property name="hibernate.show_sql">true</property>
  39. <mapping resource="contacts/employee.hbm.xml"/>
  40. <mapping resource="contacts/responsibilitiy.hbm.xml"/>
  41. </session-factory>
  42. </hibernate-configuration>
  43.  
  44. public class Main {
  45.  
  46. public static void main(String[] args) {
  47.  
  48. SessionFactory sessionfactory = NewHibernateUtil.getSessionFactory();
  49. Transaction transaction = null;
  50. try {
  51. Session session = sessionfactory.openSession();
  52. transaction = session.beginTransaction();
  53. Set<responsibilities> groups = new HashSet<responsibilities>();
  54. responsibilities responsibilityOne=new responsibilities("Java");
  55. responsibilities responsibilityTwo=new responsibilities("SQL");
  56. responsibilities responsibilityThree=new responsibilities("Oracle");
  57. groups.add(responsibilityOne);
  58. groups.add(responsibilityTwo);
  59. groups.add(responsibilityThree);
  60. String uuid = UUID.randomUUID().toString();
  61. String uuid2 = UUID.randomUUID().toString();
  62. employee firstEmployee;
  63. firstEmployee = new employee(uuid, "Mike", groups);
  64. employee secondEmployee = new employee(uuid2, "Marc", groups);
  65. session.save(responsibilityOne);
  66. session.save(responsibilityTwo);
  67. session.save(responsibilityThree);
  68. session.save(firstEmployee);
  69. session.save(secondEmployee);
  70.  
  71. transaction.commit();
  72. } catch (HibernateException e) {
  73. transaction.rollback();
  74. e.printStackTrace();
  75. } finally {
  76.  
  77. }
  78.  
  79. }
  80. }
  81.  
  82. <property name="hibernate.hbm2ddl.auto">create</property>
  83.  
  84. @Entity
  85. public class Employee {
  86. @Id
  87. @GeneratedValue
  88. private String empID;
  89. private String name;
  90. }
  91.  
  92. <property name="hibernate.hbm2ddl.auto">create-drop</property>
  93.  
  94. <property name="hibernate.hbm2ddl">create-drop</property>
Add Comment
Please, Sign In to add comment