Guest User

Untitled

a guest
Dec 20th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.80 KB | None | 0 0
  1. @Entity
  2. @NamedQuery(name="Sondage.findAll", query="SELECT s FROM Sondage s")
  3. public class Sondage implements Serializable {
  4.  
  5. private static final long serialVersionUID = 1L;
  6.  
  7. public Sondage() {}
  8.  
  9. @Id
  10. @GeneratedValue(strategy = GenerationType.IDENTITY)
  11. private int id;
  12.  
  13. private String name;
  14.  
  15. private byte needLocation;
  16.  
  17. //bi-directional many-to-one association to ResultatSondage
  18. @OneToMany(mappedBy = "sondage", cascade = CascadeType.ALL)
  19. @OrderBy("sondage ASC")
  20. private List<ResultatSondage> resultatSondages;
  21.  
  22. //bi-directional many-to-one association to SondageSection
  23. @OneToMany(mappedBy = "sondage", cascade = CascadeType.ALL)
  24. private List<SondageSection> sondageSections;
  25. }
  26.  
  27. @SuppressWarnings("unchecked")
  28. public static List<Sondage> GetAllSondage() {
  29. EntityManager em = PersistenceManager.getEntityManager();
  30. List<Sondage> allSondages = new ArrayList<>();
  31. try {
  32. em.getTransaction().begin();
  33. Query query = em.createQuery("SELECT s FROM Sondage s");
  34. allSondages = query.getResultList();
  35. em.getTransaction().commit();
  36. } catch (Exception ex) {
  37. if (em.getTransaction().isActive()) {
  38. em.getTransaction().rollback();
  39. }
  40. allSondages = null;
  41. } finally {
  42. em.close();
  43. }
  44. return allSondages;
  45. }
  46.  
  47. <body>
  48. <div class="header">
  49. <%@include file="../../../Includes/header.jsp" %>
  50. </div>
  51. <h2 style="color: green; text-align: center;">الاستمارات</h2>
  52. <div id="allsurveys" class="pure-menu custom-restricted-width">
  53. <%
  54. List<Sondage> allSondages = (List<Sondage>) request.getAttribute("sondages");
  55.  
  56. for (int i = 0; i < allSondages.size(); i++) {
  57. %>
  58. <a href="${pageContext.request.contextPath }/auth/dosurvey?id=<%= allSondages.get(i).getId()%>"><%= allSondages.get(i).getName()%></a> &nbsp;
  59. <%
  60. if (request.getSession().getAttribute("user") != null) {
  61. Utilisateur user = (Utilisateur) request.getSession().getAttribute("user");
  62. if (user.getType().equals("admin")) {
  63. %>
  64. <a href="${pageContext.request.contextPath }/aauth/editsurvey?id=<%= allSondages.get(i).getId()%>">تعديل</a>
  65. <%
  66. }
  67. }
  68. %>
  69. <br />
  70. <%
  71. }
  72. %>
  73. </div>
  74. </body>
  75.  
  76. <persistence version="2.1"
  77. xmlns="http://xmlns.jcp.org/xml/ns/persistence"
  78. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  79. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
  80. http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
  81.  
  82. <persistence-unit name="CAOE" transaction-type="RESOURCE_LOCAL">
  83. <class>com.caoe.Models.ChoixQuestion</class>
  84. <class>com.caoe.Models.Question</class>
  85. <class>com.caoe.Models.Reponse</class>
  86. <class>com.caoe.Models.ResultatSondage</class>
  87. <class>com.caoe.Models.Section</class>
  88. <class>com.caoe.Models.Sondage</class>
  89. <class>com.caoe.Models.SondageSection</class>
  90. <class>com.caoe.Models.SousQuestion</class>
  91. <class>com.caoe.Models.Utilisateur</class>
  92. <properties>
  93. <property name="hibernate.connection.provider_class"
  94. value=" org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" />
  95.  
  96. <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
  97. <property name="hibernate.connection.password" value=""/>
  98.  
  99. <property name="hibernate.connection.url"
  100. value="jdbc:mysql://localhost:3306/caoe?useUnicode=yes&characterEncoding=UTF-8"/>
  101.  
  102. <property name="hibernate.connection.username" value="root"/>
  103. <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
  104. <property name="hibernate.show_sql" value="true" />
  105.  
  106. <property name="hibernate.c3p0.max_size" value="50" />
  107. <property name="hibernate.c3p0.min_size" value="3" />
  108. <property name="hibernate.c3p0.max_statements" value="20" />
  109. <property name="hibernate.c3p0.acquire_increment" value="1" />
  110. <property name="hibernate.c3p0.idle_test_period" value="30" />
  111. <property name="hibernate.c3p0.timeout" value="35" />
  112. <property name="hibernate.c3p0.checkoutTimeout" value="60000" />
  113. <property name="hibernate.connection.release_mode" value="after_statement" />
  114.  
  115. <property name="debugUnreturnedConnectionStackTraces"
  116. value="true" />
  117. </properties>
  118. </persistence-unit>
  119. </persistence>
  120.  
  121. List<Sondage> allSondages = SondageDao.getAllSondages();
  122.  
  123. for (Sondage sondage : allSondages) {
  124. List<Question> questions = sondage.getQuestions();
  125. //code to display questions for example
  126. }
  127.  
  128. @SuppressWarnings("unchecked")
  129. public static List<Sondage> GetAllSondage() {
  130. EntityManager em = PersistenceManager.getEntityManager();
  131. List<Sondage> allSondages = new ArrayList<>();
  132. try {
  133. em.getTransaction().begin();
  134. Query query = em.createQuery("SELECT s FROM Sondage s");
  135. allSondages = query.getResultList();
  136. em.getTransaction().commit();
  137. } catch (Exception ex) {
  138. if (em.getTransaction().isActive()) {
  139. em.getTransaction().rollback();
  140. }
  141. allSondages = null;
  142. } finally {
  143. em.close();
  144. }
  145. return allSondages;
  146. }
  147.  
  148. import javax.persistence.EntityManager;
  149. import javax.persistence.EntityManagerFactory;
  150. import javax.persistence.Persistence;
  151.  
  152. public class PersistenceManager
  153. {
  154. private static EntityManagerFactory emf = null;
  155.  
  156. public static EntityManager getEntityManager()
  157. {
  158. return getEntityManagerFactory().createEntityManager();
  159. }
  160.  
  161. public static EntityManagerFactory getEntityManagerFactory()
  162. {
  163. if(emf == null) {
  164. emf = Persistence.createEntityManagerFactory("CAOE");
  165. return emf;
  166. }
  167. else
  168. return emf;
  169. }
  170. }
  171.  
  172. @SuppressWarnings("unchecked")
  173. public static List<Sondage> GetAllSondage() {
  174. //this is the method that return the EntityManagerFactory Singleton Object
  175. EntityManagerFactory emf = PersistenceManager.getEntitManagerFactory();
  176. EntityManager em = emf.createEntityManager();
  177. List<Sondage> allSondages = new ArrayList<>();
  178. try {
  179. em.getTransaction().begin();
  180. Query query = em.createQuery("SELECT s FROM Sondage s");
  181. allSondages = query.getResultList();
  182. em.getTransaction().commit();
  183. } catch (Exception ex) {
  184. if (em.getTransaction().isActive()) {
  185. em.getTransaction().rollback();
  186. }
  187. allSondages = null;
  188. } finally {
  189. em.close();
  190. emf.close();
  191. }
  192. return allSondages;
  193. }
  194.  
  195. @WebListener
  196. public class AppInit implements ServletContextListener {
  197.  
  198. public void contextInitialized(ServletContextEvent sce) {}
  199.  
  200. public void contextDestroyed(ServletContextEvent sce) {
  201. PersistenceManager.closeEntityMangerFactory();
  202. }
  203. }
  204.  
  205. <property name="hibernate.connection.release_mode" value="after_transaction" />
  206. <property name="hibernate.current_session_context_class" value="jta" />
  207.  
  208. import javax.persistence.EntityManagerFactory;
  209. import javax.persistence.Persistence;
  210.  
  211. public final class EMF {
  212. private static final EntityManagerFactory emfInstance =
  213. Persistence.createEntityManagerFactory("CAOE");
  214.  
  215. private EMF() {}
  216.  
  217. public static EntityManagerFactory get() {
  218. return emfInstance;
  219. }
  220. }
  221.  
  222. import javax.persistence.EntityManager;
  223. import javax.persistence.EntityManagerFactory;
  224. import EMF;
  225.  
  226. // ...
  227. EntityManager em = EMF.get().createEntityManager();
  228.  
  229. @OneToMany(mappedBy = "sondage", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
Add Comment
Please, Sign In to add comment