Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Entity
- @NamedQuery(name="Sondage.findAll", query="SELECT s FROM Sondage s")
- public class Sondage implements Serializable {
- private static final long serialVersionUID = 1L;
- public Sondage() {}
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private int id;
- private String name;
- private byte needLocation;
- //bi-directional many-to-one association to ResultatSondage
- @OneToMany(mappedBy = "sondage", cascade = CascadeType.ALL)
- @OrderBy("sondage ASC")
- private List<ResultatSondage> resultatSondages;
- //bi-directional many-to-one association to SondageSection
- @OneToMany(mappedBy = "sondage", cascade = CascadeType.ALL)
- private List<SondageSection> sondageSections;
- }
- @SuppressWarnings("unchecked")
- public static List<Sondage> GetAllSondage() {
- EntityManager em = PersistenceManager.getEntityManager();
- List<Sondage> allSondages = new ArrayList<>();
- try {
- em.getTransaction().begin();
- Query query = em.createQuery("SELECT s FROM Sondage s");
- allSondages = query.getResultList();
- em.getTransaction().commit();
- } catch (Exception ex) {
- if (em.getTransaction().isActive()) {
- em.getTransaction().rollback();
- }
- allSondages = null;
- } finally {
- em.close();
- }
- return allSondages;
- }
- <body>
- <div class="header">
- <%@include file="../../../Includes/header.jsp" %>
- </div>
- <h2 style="color: green; text-align: center;">الاستمارات</h2>
- <div id="allsurveys" class="pure-menu custom-restricted-width">
- <%
- List<Sondage> allSondages = (List<Sondage>) request.getAttribute("sondages");
- for (int i = 0; i < allSondages.size(); i++) {
- %>
- <a href="${pageContext.request.contextPath }/auth/dosurvey?id=<%= allSondages.get(i).getId()%>"><%= allSondages.get(i).getName()%></a>
- <%
- if (request.getSession().getAttribute("user") != null) {
- Utilisateur user = (Utilisateur) request.getSession().getAttribute("user");
- if (user.getType().equals("admin")) {
- %>
- <a href="${pageContext.request.contextPath }/aauth/editsurvey?id=<%= allSondages.get(i).getId()%>">تعديل</a>
- <%
- }
- }
- %>
- <br />
- <%
- }
- %>
- </div>
- </body>
- <persistence version="2.1"
- xmlns="http://xmlns.jcp.org/xml/ns/persistence"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
- http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
- <persistence-unit name="CAOE" transaction-type="RESOURCE_LOCAL">
- <class>com.caoe.Models.ChoixQuestion</class>
- <class>com.caoe.Models.Question</class>
- <class>com.caoe.Models.Reponse</class>
- <class>com.caoe.Models.ResultatSondage</class>
- <class>com.caoe.Models.Section</class>
- <class>com.caoe.Models.Sondage</class>
- <class>com.caoe.Models.SondageSection</class>
- <class>com.caoe.Models.SousQuestion</class>
- <class>com.caoe.Models.Utilisateur</class>
- <properties>
- <property name="hibernate.connection.provider_class"
- value=" org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider" />
- <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
- <property name="hibernate.connection.password" value=""/>
- <property name="hibernate.connection.url"
- value="jdbc:mysql://localhost:3306/caoe?useUnicode=yes&characterEncoding=UTF-8"/>
- <property name="hibernate.connection.username" value="root"/>
- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
- <property name="hibernate.show_sql" value="true" />
- <property name="hibernate.c3p0.max_size" value="50" />
- <property name="hibernate.c3p0.min_size" value="3" />
- <property name="hibernate.c3p0.max_statements" value="20" />
- <property name="hibernate.c3p0.acquire_increment" value="1" />
- <property name="hibernate.c3p0.idle_test_period" value="30" />
- <property name="hibernate.c3p0.timeout" value="35" />
- <property name="hibernate.c3p0.checkoutTimeout" value="60000" />
- <property name="hibernate.connection.release_mode" value="after_statement" />
- <property name="debugUnreturnedConnectionStackTraces"
- value="true" />
- </properties>
- </persistence-unit>
- </persistence>
- List<Sondage> allSondages = SondageDao.getAllSondages();
- for (Sondage sondage : allSondages) {
- List<Question> questions = sondage.getQuestions();
- //code to display questions for example
- }
- @SuppressWarnings("unchecked")
- public static List<Sondage> GetAllSondage() {
- EntityManager em = PersistenceManager.getEntityManager();
- List<Sondage> allSondages = new ArrayList<>();
- try {
- em.getTransaction().begin();
- Query query = em.createQuery("SELECT s FROM Sondage s");
- allSondages = query.getResultList();
- em.getTransaction().commit();
- } catch (Exception ex) {
- if (em.getTransaction().isActive()) {
- em.getTransaction().rollback();
- }
- allSondages = null;
- } finally {
- em.close();
- }
- return allSondages;
- }
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.Persistence;
- public class PersistenceManager
- {
- private static EntityManagerFactory emf = null;
- public static EntityManager getEntityManager()
- {
- return getEntityManagerFactory().createEntityManager();
- }
- public static EntityManagerFactory getEntityManagerFactory()
- {
- if(emf == null) {
- emf = Persistence.createEntityManagerFactory("CAOE");
- return emf;
- }
- else
- return emf;
- }
- }
- @SuppressWarnings("unchecked")
- public static List<Sondage> GetAllSondage() {
- //this is the method that return the EntityManagerFactory Singleton Object
- EntityManagerFactory emf = PersistenceManager.getEntitManagerFactory();
- EntityManager em = emf.createEntityManager();
- List<Sondage> allSondages = new ArrayList<>();
- try {
- em.getTransaction().begin();
- Query query = em.createQuery("SELECT s FROM Sondage s");
- allSondages = query.getResultList();
- em.getTransaction().commit();
- } catch (Exception ex) {
- if (em.getTransaction().isActive()) {
- em.getTransaction().rollback();
- }
- allSondages = null;
- } finally {
- em.close();
- emf.close();
- }
- return allSondages;
- }
- @WebListener
- public class AppInit implements ServletContextListener {
- public void contextInitialized(ServletContextEvent sce) {}
- public void contextDestroyed(ServletContextEvent sce) {
- PersistenceManager.closeEntityMangerFactory();
- }
- }
- <property name="hibernate.connection.release_mode" value="after_transaction" />
- <property name="hibernate.current_session_context_class" value="jta" />
- import javax.persistence.EntityManagerFactory;
- import javax.persistence.Persistence;
- public final class EMF {
- private static final EntityManagerFactory emfInstance =
- Persistence.createEntityManagerFactory("CAOE");
- private EMF() {}
- public static EntityManagerFactory get() {
- return emfInstance;
- }
- }
- import javax.persistence.EntityManager;
- import javax.persistence.EntityManagerFactory;
- import EMF;
- // ...
- EntityManager em = EMF.get().createEntityManager();
- @OneToMany(mappedBy = "sondage", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
Add Comment
Please, Sign In to add comment