List of usage examples for javax.persistence EntityTransaction begin
public void begin();
From source file:fr.natoine.dao.annotation.DAOAnnotation.java
/** * Retrieves a list of Annotation according to the specified label * @param label/*from w ww .j a v a 2 s . com*/ * @return */ public List<Annotation> retrieveAnnotation(String label) { //EntityManagerFactory emf = this.setEMF(); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); try { tx.begin(); //List docs = em.createQuery("from Annotation where label = '" + label + "'").getResultList(); List<Annotation> docs = ((List<Annotation>) em.createQuery("from Annotation where label = ?") .setParameter(1, label).getResultList()); tx.commit(); return docs; } catch (Exception e) { tx.rollback(); //em.close(); System.out.println("[RetrieveAnnotation.retrieveAnnotation] unable to retrieve Annotation" + " label : " + label + " cause : " + e.getMessage()); return new ArrayList<Annotation>(); } }
From source file:fr.natoine.dao.annotation.DAOAnnotation.java
/** * Retrieves the Resource in the database with the specified id. * @param id/* www . j a va2s .com*/ * @return a Resource that may be empty */ public Resource retrieveResource(long id) { //TODO move this method elsewhere, it's here because extension of Resource Class are not queried if it were in controler-resource package //EntityManagerFactory emf = this.setEMF(); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); try { tx.begin(); Resource resource = (Resource) em.createQuery("from Resource where id = ?").setParameter(1, id) .getSingleResult(); tx.commit(); ////em.close(); return resource; } catch (Exception e) { tx.rollback(); //em.close(); System.out.println("[RetrieveResource.retrieveResource] unable to retrieve Resource" + " id : " + id + " cause : " + e.getMessage()); return new Resource(); } }
From source file:com.espirit.moddev.examples.uxbridge.newsdrilldown.jpa.NewsHandler.java
/** * handle the update and save process./*from www .j av a 2 s . c om*/ * * @param entity the newsdrilldown entity * @throws Exception the exception */ private void handle(UXBEntity entity) throws Exception { /** * Due to a hibernate problem when updating or saving a newsdrilldown with * related categories we have to perform these step for saving the newsdrilldown * * 1. save/update all categories and remove them from the newsdrilldown * 1.1 save/update all metaCategories and remove them from the categories * 1.2 save the categories * 1.3 read all metaCategories to the categories * 1.4 save the categories again to create the relations * 2. save the newsdrilldown * 3. read all categories to the newsdrilldown */ EntityManager em = null; EntityTransaction tx = null; try { News news = buildNews(entity); List<Long> categories = new ArrayList<Long>(); /* * 1. update or save all categories * Steps 1.1 - 1.4 */ if (news.getCategories() != null && !news.getCategories().isEmpty()) { for (NewsCategory cat : news.getCategories()) { cat = saveNewsCategory(cat); if (!categories.contains(cat.getFs_id())) { categories.add(cat.getFs_id()); } } news.setCategories(new ArrayList<NewsCategory>()); } em = emf.createEntityManager(); tx = em.getTransaction(); tx.begin(); // 2. save the newsdrilldown news = saveNews(news, em); // 3. read all categories to the newsdrilldown if (!categories.isEmpty()) { for (Long cat : categories) { NewsCategory ncat = getNewsCategory(cat, news.getLanguage(), em); news.getCategories().add(ncat); } } tx.commit(); } catch (Exception e) { if (tx != null && tx.isActive()) { tx.setRollbackOnly(); } throw e; } finally { if (tx != null && tx.isActive()) { if (tx.getRollbackOnly()) { tx.rollback(); } } if (em != null) { em.close(); } } }
From source file:fr.natoine.dao.annotation.DAOAnnotation.java
/** * Retrieves an annotationStatus/* w ww.j av a 2 s .c om*/ * @param id * @return */ public AnnotationStatus retrieveAnnotationStatus(long id) { // EntityManagerFactory emf = this.setEMF(); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); try { tx.begin(); AnnotationStatus _synchro = em.find(AnnotationStatus.class, id); tx.commit(); if (_synchro != null) return _synchro; System.out.println( "[RetrieveAnnotationStatus.retrieveAnnotationStatus] unable to retrieve AnnotationStatus" + " id : " + id); return new AnnotationStatus(); } catch (Exception e) { tx.rollback(); //em.close(); System.out.println( "[RetrieveAnnotationStatus.retrieveAnnotationStatus] unable to retrieve AnnotationStatus" + " id : " + id + " cause : " + e.getMessage()); return new AnnotationStatus(); } }
From source file:fr.natoine.dao.annotation.DAOAnnotation.java
/** * Computes the number of annotations for associated to a specified Resource through its url * @param _url/*from www .j a v a 2 s . co m*/ * @return */ public long computeNbAnnotations(String _url) { long nb = 0; //EntityManagerFactory emf = this.setEMF(); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); try { tx.begin(); URI _uri = (URI) em.createQuery("from URI where effectiveURI = ?").setParameter(1, _url) .getSingleResult(); if (_uri == null) { tx.commit(); System.out.println("[RetrieveAnnotation.computeNbAnnotations] unable to retrieve Annotations" + " cause : there is no uri " + _url); return nb; } Object nb_annotations = em.createQuery( " select count(distinct annotation.id) from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=?") .setParameter(1, _uri).getSingleResult(); //System.out.println("[RetrieveAnnotation.computeNbAnnotations] nb_annotations : " + nb_annotations + " classe : " + nb_annotations.getClass().getSimpleName()); tx.commit(); if (nb_annotations instanceof Long) { nb = ((Long) nb_annotations).longValue(); } return nb; } catch (Exception e) { tx.rollback(); return nb; } }
From source file:fr.natoine.dao.annotation.DAOAnnotation.java
/** * Retrieves all the annotations about a specified Resource, by ascendant or descendant chronological order * @param _url// ww w .j a va2 s.com * @param asc * @return */ public List<Annotation> retrieveAnnotations(String _url, boolean asc) { //EntityManagerFactory emf = this.setEMF(); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); try { tx.begin(); URI _uri = (URI) em.createQuery("from URI where effectiveURI = ?").setParameter(1, _url) .getSingleResult(); if (_uri == null) { tx.commit(); // //em.close(); System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations" + " cause : there is no uri " + _url); return new ArrayList<Annotation>(); } //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotated as annotated inner join annotated.representsResource as uri where uri=?").setParameter(1, _uri).getResultList(); String order_by_clause = " order by annotation.id desc"; if (asc) order_by_clause = " order by annotation.id asc"; List<Annotation> annotations = ((List<Annotation>) em.createQuery( "select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=?" + order_by_clause) .setParameter(1, _uri).getResultList()); tx.commit(); //em.close(); return annotations; } catch (Exception e) { //tx.commit(); tx.rollback(); //em.close(); System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations" + " cause : " + e.getMessage()); return new ArrayList<Annotation>(); } }
From source file:fr.natoine.dao.annotation.DAOAnnotation.java
/** * Retrieves all the annotations associated to a Resource specified by its URL, grouped by annotationStatus * @param _url//from www . j a v a2s . c o m * @param asc * @return */ public List<Annotation> retrieveAnnotationsGroupByStatus(String _url, boolean asc) { //EntityManagerFactory emf = this.setEMF(); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); try { tx.begin(); URI _uri = (URI) em.createQuery("from URI where effectiveURI = ?").setParameter(1, _url) .getSingleResult(); if (_uri == null) { tx.commit(); // //em.close(); System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations" + " cause : there is no uri " + _url); return new ArrayList<Annotation>(); } //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotated as annotated inner join annotated.representsResource as uri where uri=?").setParameter(1, _uri).getResultList(); String order_by_clause = " annotation.id desc"; if (asc) order_by_clause = " annotation.id asc"; List<Annotation> annotations = em.createQuery( "select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? order by annotation.status," + order_by_clause) .setParameter(1, _uri).getResultList(); //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? group by annotation.status" ).setParameter(1, _uri).getResultList(); tx.commit(); //em.close(); return annotations; } catch (Exception e) { //tx.commit(); tx.rollback(); //em.close(); System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations" + " cause : " + e.getMessage()); return new ArrayList<Annotation>(); } }
From source file:fr.natoine.dao.annotation.DAOAnnotation.java
/** * Retrieves all the annotations associated to a specified Resource through its URL, ordered by FirstAuthor * @param _url//from w ww . jav a 2 s . c o m * @param asc * @return */ public List<Annotation> retrieveAnnotationsGroupByFirstAuthor(String _url, boolean asc) { //EntityManagerFactory emf = this.setEMF(); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); try { tx.begin(); URI _uri = (URI) em.createQuery("from URI where effectiveURI = ?").setParameter(1, _url) .getSingleResult(); if (_uri == null) { tx.commit(); // //em.close(); System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations" + " cause : there is no uri " + _url); return new ArrayList<Annotation>(); } //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotated as annotated inner join annotated.representsResource as uri where uri=?").setParameter(1, _uri).getResultList(); String order_by_clause = " annotation.id desc"; if (asc) order_by_clause = " annotation.id asc"; List<Annotation> annotations = em.createQuery( "select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? order by annotation.creator," + order_by_clause) .setParameter(1, _uri).getResultList(); //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? group by annotation.status" ).setParameter(1, _uri).getResultList(); tx.commit(); //em.close(); return annotations; } catch (Exception e) { //tx.commit(); tx.rollback(); //em.close(); System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations" + " cause : " + e.getMessage()); return new ArrayList<Annotation>(); } }
From source file:fr.natoine.dao.annotation.DAOAnnotation.java
/** * Retrieves a specified quantity of Annotations associated to a Resource specified by its URL * @param _url//from www . j a v a 2 s . c om * @param asc * @param first_indice * @param max_results * @return */ public List<Annotation> retrieveAnnotations(String _url, boolean asc, int first_indice, int max_results) { //EntityManagerFactory emf = this.setEMF(); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); try { tx.begin(); URI _uri = (URI) em.createQuery("from URI where effectiveURI = ?").setParameter(1, _url) .getSingleResult(); if (_uri == null) { tx.commit(); // //em.close(); System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations" + " cause : there is no uri " + _url); return new ArrayList<Annotation>(); } //List annotations = em.createQuery("select distinct annotation from Annotation as annotation inner join annotation.annotated as annotated inner join annotated.representsResource as uri where uri=?").setParameter(1, _uri).getResultList(); String order_by_clause = " order by annotation.id desc"; if (asc) order_by_clause = " order by annotation.id asc"; Query _query = em.createQuery( "select distinct annotation from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=?" + order_by_clause) .setParameter(1, _uri); _query.setFirstResult(first_indice); _query.setMaxResults(max_results); List<Annotation> annotations = _query.getResultList(); tx.commit(); //em.close(); return annotations; } catch (Exception e) { //tx.commit(); tx.rollback(); //em.close(); System.out.println("[RetrieveAnnotation.retrieveAnnotations] unable to retrieve Annotations" + " cause : " + e.getMessage()); return new ArrayList<Annotation>(); } }
From source file:fr.natoine.dao.annotation.DAOAnnotation.java
/** * Tests if an agent has expressed a Spaming on a resource * @param _creator/* www . j a v a2s. c o m*/ * @param _url_to_test * @return */ public boolean spamExpressed(Agent _creator, String _url_to_test) { long nb = 0; //EntityManagerFactory emf = this.setEMF(); EntityManager em = emf.createEntityManager(); EntityTransaction tx = em.getTransaction(); try { tx.begin(); URI _uri = (URI) em.createQuery("from URI where effectiveURI = ?").setParameter(1, _url_to_test) .getSingleResult(); if (_uri == null) { tx.commit(); System.out.println("[RetrieveAnnotation.spamExpressed] unable to retrieve Annotations" + " cause : there is no uri " + _url_to_test); return false; } AnnotationStatus status_spam = (AnnotationStatus) em .createQuery("from AnnotationStatus where label = ?").setParameter(1, "Spam").getSingleResult(); if (status_spam != null) { Object nb_annotations_troll = em.createQuery( " select count(distinct annotation.id) from Annotation as annotation inner join annotation.annotatedURIs as uri where uri=? and annotation.creator=? and annotation.status=?") .setParameter(1, _uri).setParameter(2, _creator).setParameter(3, status_spam) .getSingleResult(); if (nb_annotations_troll instanceof Long) { nb = ((Long) nb_annotations_troll).longValue(); } } tx.commit(); if (nb > 0) return true; else return false; } catch (Exception e) { tx.rollback(); //em.close(); System.out.println("[RetrieveAnnotation.spamExpressed] unable to retrieve Annotations" + " on url : " + _url_to_test + " cause : " + e.getMessage()); return false; //to prevent to express its opinion if the system fails } }