Advertisement
Guest User

Untitled

a guest
Dec 19th, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1. Illegal attempt to associate a collection with two open sessions
  2.  
  3. public class Hibernate
  4. {
  5. protected static final SessionFactory sessionFactory;
  6.  
  7. private Session session;
  8.  
  9. static
  10. {
  11. try
  12. {
  13. // Create the SessionFactory from hibernate.cfg.xml
  14. sessionFactory = new Configuration().configure().buildSessionFactory();
  15. session
  16. }
  17. catch (Throwable ex)
  18. {
  19. // Make sure you log the exception, as it might be swallowed
  20. System.err.println("Initial SessionFactory creation failed." + ex);
  21. throw new ExceptionInInitializerError(ex);
  22. }
  23. }
  24.  
  25. public void create(Object obj)
  26. {
  27. this.session = sessionFactory.openSession();
  28. session.getTransaction().begin();
  29. session.save(obj);
  30. session.getTransaction().commit();
  31. session.close();
  32. }
  33.  
  34. public void refresh(Object obj)
  35. {
  36. this.session = sessionFactory.openSession();
  37. session.getTransaction().begin();
  38. session.refresh(obj);
  39. session.getTransaction().commit();
  40. session.close();
  41. }
  42.  
  43. public void update(Object obj)
  44. {
  45. this.session = sessionFactory.openSession();
  46. session.getTransaction().begin();
  47. session.saveOrUpdate(obj);
  48. session.getTransaction().commit();
  49. session.close();
  50.  
  51. }
  52. public void delete(Object obj)
  53. {
  54. this.session = sessionFactory.openSession();
  55. session.getTransaction().begin();
  56. session.delete(obj);
  57. session.flush();
  58. session.getTransaction().commit();
  59. session.close();
  60. }
  61. protected String protectString(String toProtect)
  62. {
  63. return (toProtect.replace("'", "''"));
  64. }
  65.  
  66. }
  67.  
  68. public class DAOPerson extends Hibernate
  69. {
  70.  
  71. public void remove(Person p)
  72. {
  73. if (p instanceof Student)
  74. {
  75. Student s = (Student)p;
  76. Set<persistenceClass.Class> set = s.getClasses();
  77. Iterator<persistenceClass.Class> it = set.iterator();
  78. while (it.hasNext())
  79. {
  80. persistenceClass.Class r = it.next();
  81. r.getStudents().remove(s);
  82. }
  83. p.getBirthCountry();
  84. p.getCountry();
  85. this.delete(p);
  86. }
  87. else
  88. this.delete(p);
  89. }
  90.  
  91. <class name="persistenceClass.Person" table="T_PERSON">
  92. <id name="Id" column="PERSON_ID">
  93. <generator class="native" />
  94. </id>
  95. <property name="FirstName" column="PERSON_FIRST_NAME" not-null="true" />
  96. <property name="LastName" column="PERSON_LAST_NAME" not-null="true" />
  97. <property name="Type" column="PERSON_TYPE" not-null="true" />
  98. <property name="BirthDate" column="PERSON_BIRTH_DATE" />
  99. <property name="BirthCity" column="PERSON_BIRTH_CITY" />
  100. <property name="PhoneNumber" column="PERSON_PHONE_NUMBER" />
  101. <property name="MobileNumber" column="PERSON_MOBILE_NUMBER" />
  102. <property name="Mail" column="PERSON_MAIL" />
  103. <property name="Address" column="PERSON_ADDRESS_ADDRESS" />
  104. <property name="ZipCode" column="PERSON_ADDRESS_ZIPCODE" />
  105. <property name="City" column="PERSON_ADDRESS_CITY" />
  106. <property name="Image" column="PERSON_IMAGE" type="image" />
  107. <many-to-one name="Country" column="PERSON_ADDRESS_COUNTRY" class="persistenceClass.Country" />
  108. <many-to-one name="BirthCountry" column="PERSON_BIRTH_COUNTRY" class="persistenceClass.Country" />
  109. <many-to-one name="Civility" column="PERSON_CIVILITY" class="persistenceClass.Civility" />
  110. <many-to-one name="Sex" column="PERSON_SEX" class="persistenceClass.Sex" />
  111. <joined-subclass name="persistenceClass.Student" table="T_STUDENT">
  112. <key column="PERSON_ID" />
  113. <set name="Classes" table="T_CLASS_STUDENT" inverse="true" >
  114. <key column="PERSON_ID" />
  115. <many-to-many class="persistenceClass.Class" column="CLASS_ID" />
  116. </set>
  117. </joined-subclass>
  118. <joined-subclass name="persistenceClass.Teacher" table="T_TEACHER">
  119. <key column="PERSON_ID" />
  120. </joined-subclass>
  121. </class>
  122.  
  123. <hibernate-configuration>
  124. <session-factory>
  125. <!-- Database connection settings -->
  126. <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
  127. <property name="connection.url">jdbc:mysql://localhost/projet</property>
  128. <property name="connection.username">root</property>
  129. <property name="connection.password"></property>
  130. <!-- JDBC connection pool (use the built-in) -->
  131. <property name="connection.pool_size">10</property>
  132. <!-- SQL dialect -->
  133. <property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
  134. <!-- Drop and re-create the database schema on start-up, also try with “update” to keep the previous values -->
  135. <property name="hbm2ddl.auto">update</property>
  136. <!-- Echo all executed SQL to stdout -->
  137. <property name="show_sql">true</property>
  138. <mapping resource="persistenceConfigurations/Person.hbm.xml"/>
  139. <mapping resource="persistenceConfigurations/Country.hbm.xml"/>
  140. <mapping resource="persistenceConfigurations/Civility.hbm.xml"/>
  141. <mapping resource="persistenceConfigurations/Sex.hbm.xml"/>
  142. <mapping resource="persistenceConfigurations/Formation.hbm.xml"/>
  143. <mapping resource="persistenceConfigurations/Year.hbm.xml"/>
  144. <mapping resource="persistenceConfigurations/Class.hbm.xml"/>
  145. <mapping resource="persistenceConfigurations/Subject.hbm.xml"/>
  146. <mapping resource="persistenceConfigurations/Room.hbm.xml"/>
  147. <mapping resource="persistenceConfigurations/Lesson.hbm.xml"/>
  148. </session-factory>
  149. </hibernate-configuration>
  150.  
  151. public void remove(Person p)
  152. {
  153. if (p instanceof Student)
  154. {
  155. Student s = (Student)p;
  156. Set<persistenceClass.Class> set = s.getClasses();
  157. Iterator<persistenceClass.Class> it = set.iterator();
  158. while (it.hasNext())
  159. {
  160. persistenceClass.Class r = it.next();
  161. r.getStudents().remove(s);
  162.  
  163. //now effectively removing the student from the class.
  164. //this will update the class and its persistent set of students.
  165. this.update(r);
  166. }
  167. }
  168. this.delete(p); //now remove the student (or whatever comes as argument)
  169. }
  170.  
  171. //Example
  172. public void create(Object obj){
  173. this.session = sessionFactory.openSession();
  174. try{
  175. session.getTransaction().begin();
  176. session.save(obj);
  177. session.getTransaction().commit();
  178. } catch (Exception e) {
  179. e.printStackTrace();
  180. session.getTransaction().rollback();
  181. } finally {
  182. session.flush();
  183. session.close();
  184. //keep in mind that too many flushes might cause db memory shortage.
  185. //So if you are treating list of objects and so on iterate and save them flushing
  186. //only when your batch size is reached.
  187. }
  188. }
  189.  
  190. @ManyToOne(cascade = CascadeType.ALL)
  191. @JoinColumn(name = “user_id”)
  192. public User getAuthor() {
  193. return author;
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement