List of usage examples for javax.persistence EntityManager getDelegate
public Object getDelegate();
EntityManager
, if available. From source file:it.webappcommon.lib.jpa.ControllerStandard.java
/** * Funziona SOLO CON IMPLEMENTAZIONE HIBERNATE * //from ww w . j av a 2s. co m * @param aList * @param aFilter * @return * @throws Exception */ public <T extends EntityBaseStandard> ArrayList<T> getFilteredList(Class<T> classObj, String aFilter) throws Exception { ArrayList<T> returnValue = null; EntityManagerFactory emf = null; EntityManager em = null; Session session = null; Criteria cri = null; try { /* Istanzio l'entity manager */ emf = getEntityManagerFactory(); em = emf.createEntityManager(); session = (Session) em.getDelegate(); org.hibernate.Query q = session.createQuery("from " + classObj.getName()); q.setFirstResult(0); q.setMaxResults(100); List<T> resAll = q.list(); org.hibernate.Query filterQuery = session.createFilter(resAll, aFilter); /* Effettuo la query */ returnValue = (ArrayList) filterQuery.list(); } catch (Exception e) { throw e; } finally { session = null; cri = null; em = null; } return returnValue; }
From source file:it.webappcommon.lib.jpa.ControllerStandard.java
public <T extends EntityBaseStandard> List<T> listByExample(Class<T> classObj, T anExample) throws Exception { List<T> returnValue = null; EntityManagerFactory emf = null;//from w w w.j a va2 s .c o m EntityManager em = null; Session session = null; Criteria cri = null; try { /* Istanzio l'entity manager */ emf = getEntityManagerFactory(); em = emf.createEntityManager(); session = (Session) em.getDelegate(); List res = session.createCriteria(classObj).add(Example.create(anExample).excludeZeroes().enableLike()) .list(); // org.hibernate.Query filterQuery = session.createFilter(resAll, // aFilter); /* Effettuo la query */ returnValue = (List<T>) res; } catch (Exception e) { throw e; } finally { if (!passedEm) { PersistenceManagerUtil.close(em); } session = null; cri = null; em = null; } return returnValue; }
From source file:edu.ucsb.eucalyptus.cloud.ws.WalrusManager.java
private void doRenameEvent(EntityManager em, final String account, final String bucketName, final String object_key, final String renameTo) throws EucalyptusCloudException { Session sess = null;/* w w w. j av a2 s.c o m*/ try { sess = ((Session) em.getDelegate()); } catch (Throwable t) { sess = null; } final String sql = "UPDATE objects SET object_key=regexp_replace(object_key, ?, ?, 'q') " + "where bucket_name = ? and owner_id = ? and (object_key LIKE ? or object_key = ?)"; final String like_key = object_key.replaceAll("%", "\\%").replaceAll("_", "\\_"); if (sess != null) { sess.doWork(new Work() { public void execute(Connection connection) throws SQLException { PreparedStatement stmt = null; try { stmt = connection.prepareStatement(sql); stmt.setString(1, object_key); stmt.setString(2, renameTo); stmt.setString(3, bucketName); stmt.setString(4, account); //stmt.setString(5, "^" + object_key + "/"); stmt.setString(5, like_key + "/%"); stmt.setString(6, object_key); int exR = stmt.executeUpdate(); if (exR == 0) { throw new SQLException("CssError", account + ":" + bucketName + ":" + object_key + "***" + "no objects are renamed"); } } finally { try { if (stmt != null) stmt.close(); } catch (Throwable t) { /*NOP*/} } } }); } else { int exR = em.createNativeQuery(sql).setParameter(1, object_key).setParameter(2, renameTo) .setParameter(3, bucketName).setParameter(4, account) //.setParameter(5, "^" + object_key + "/") .setParameter(5, like_key + "/%").setParameter(6, object_key).executeUpdate(); if (exR == 0) { throw new AccessDeniedException("CssError", account + ":" + bucketName + ":" + object_key + "***" + "no objects are renamed", true); } } }
From source file:edu.ucsb.eucalyptus.cloud.ws.WalrusManager.java
private void writeRenameEvent(EntityManager em, final String eventType, final String account, final String bucketName, final String object_key, final String renameTo, final long syncid, final String reqid, final int objseq) throws EucalyptusCloudException { Session sess = null;/*from w w w .j av a2s . c o m*/ try { sess = ((Session) em.getDelegate()); } catch (Throwable t) { sess = null; } if ("webApp".equals(eventType)) { final String sql = "insert into web_opt_log(rectime,time,cssact,op,fpth,fsz,file_version,syncid,isfolder,rslt,inst_id,reqid)" + "select ?,to_timestamp(coalesce(value::bigint,0)/1000),?,'Rename',?,size,?,?,content_type='application/x-directory','succ','dummy',? from objects " + "obj left join metadata mtd on obj.object_name=mtd.object_id and mtd.\"name\"='mtime' " + "where object_key = ? AND bucket_name = ? AND owner_id = ? limit 1"; if (sess != null) { sess.doWork(new Work() { public void execute(Connection connection) throws SQLException { PreparedStatement stmt = null; try { stmt = connection.prepareStatement(sql); Calendar c = Calendar.getInstance(); stmt.setTimestamp(1, new Timestamp(c.getTimeInMillis() - c.getTimeZone().getRawOffset())); stmt.setString(2, account); stmt.setString(3, object_key + "||" + renameTo); stmt.setInt(4, objseq); stmt.setLong(5, syncid); stmt.setString(6, reqid); stmt.setString(7, renameTo); stmt.setString(8, bucketName); stmt.setString(9, account); stmt.executeUpdate(); } finally { try { if (stmt != null) stmt.close(); } catch (Throwable t) { /*NOP*/} } } }); } else { Calendar c = Calendar.getInstance(); em.createNativeQuery(sql) .setParameter(1, new Timestamp(c.getTimeInMillis() - c.getTimeZone().getRawOffset())) .setParameter(2, account).setParameter(3, object_key + "||" + renameTo) .setParameter(4, objseq).setParameter(5, syncid).setParameter(6, reqid) .setParameter(7, renameTo).setParameter(8, bucketName).setParameter(9, account) .executeUpdate(); } } else if ("mobileApp".equals(eventType)) { } else { final String sql = "insert into opt_log(rectime,time,cssact,op,fpth,fsz,file_version,syncid,isfolder,rslt,inst_id,reqid) " + "select ?,to_timestamp(coalesce(value::bigint,0)/1000),?,'Rename',?,size,?,?,content_type='application/x-directory','succ','dummy',? from objects " + "obj left join metadata mtd on obj.object_name=mtd.object_id and mtd.\"name\"='mtime' " + "where object_key = ? AND bucket_name = ? AND owner_id = ? limit 1"; if (sess != null) { sess.doWork(new Work() { public void execute(Connection connection) throws SQLException { PreparedStatement stmt = null; try { stmt = connection.prepareStatement(sql); Calendar c = Calendar.getInstance(); stmt.setTimestamp(1, new Timestamp(c.getTimeInMillis() - c.getTimeZone().getRawOffset())); stmt.setString(2, account); stmt.setString(3, object_key + "||" + renameTo); stmt.setInt(4, objseq); stmt.setLong(5, syncid); stmt.setString(6, reqid); stmt.setString(7, renameTo); stmt.setString(8, bucketName); stmt.setString(9, account); stmt.executeUpdate(); } finally { try { if (stmt != null) stmt.close(); } catch (Throwable t) { /*NOP*/} } } }); } else { Calendar c = Calendar.getInstance(); em.createNativeQuery(sql) .setParameter(1, new Timestamp(c.getTimeInMillis() - c.getTimeZone().getRawOffset())) .setParameter(2, account).setParameter(3, object_key + "||" + renameTo) .setParameter(4, objseq).setParameter(5, syncid).setParameter(6, reqid) .setParameter(7, renameTo).setParameter(8, bucketName).setParameter(9, account) .executeUpdate(); } } }
From source file:edu.ucsb.eucalyptus.cloud.ws.WalrusManager.java
private void writeEvent(EntityManager em, final String eventType, final String account, final String opt, final String object_key, final Long size, final Boolean isFolder, final long syncid, final long lastModified, final String reqid, final int objseq) throws EucalyptusCloudException { Session sess = null;//from w ww. jav a2s . c o m try { sess = ((Session) em.getDelegate()); } catch (Throwable t) { sess = null; } final java.sql.Timestamp mtime = new java.sql.Timestamp(lastModified); if ("webApp".equals(eventType)) { final String sql = "insert into web_opt_log(id,rectime,time,cssact,op,fpth,fsz,file_version,syncid,isfolder,rslt,inst_id, reqid) values(DEFAULT,CURRENT_TIMESTAMP AT TIME ZONE 'UTC +0',?,?,?,?,?,?,NULL,?,'succ','dummy',?)"; if (sess != null) { sess.doWork(new Work() { public void execute(Connection connection) throws SQLException { PreparedStatement stmt = null; try { stmt = connection.prepareStatement(sql); stmt.setTimestamp(1, mtime); stmt.setString(2, account); stmt.setString(3, opt); stmt.setString(4, object_key); stmt.setLong(5, size); stmt.setInt(6, objseq); stmt.setBoolean(7, isFolder); stmt.setString(8, reqid); stmt.executeUpdate(); } finally { if (stmt != null) stmt.close(); } } }); } else { em.createNativeQuery(sql).setParameter(1, mtime).setParameter(2, account).setParameter(3, opt) .setParameter(4, object_key).setParameter(5, size).setParameter(6, objseq) .setParameter(7, isFolder).setParameter(8, reqid).executeUpdate(); } } else if ("mobileApp".equals(eventType)) { } else { final String sql = "insert into opt_log(id,rectime,time,cssact,op,fpth,fsz,file_version,syncid,isfolder,rslt,inst_id, reqid) values(DEFAULT,CURRENT_TIMESTAMP AT TIME ZONE 'UTC +0',?,?,?,?,?,?,?,?,'succ','dummy',?)"; if (sess != null) { sess.doWork(new Work() { public void execute(Connection connection) throws SQLException { PreparedStatement stmt = null; try { stmt = connection.prepareStatement(sql); stmt.setTimestamp(1, mtime); stmt.setString(2, account); stmt.setString(3, opt); stmt.setString(4, object_key); stmt.setLong(5, size); stmt.setInt(6, objseq); stmt.setLong(7, syncid); stmt.setBoolean(8, isFolder); stmt.setString(9, reqid); stmt.executeUpdate(); } finally { if (stmt != null) stmt.close(); } } } ); } else { em.createNativeQuery(sql).setParameter(1, mtime).setParameter(2, account).setParameter(3, opt) .setParameter(4, object_key).setParameter(5, size).setParameter(6, objseq) .setParameter(7, syncid).setParameter(8, isFolder).setParameter(9, reqid).executeUpdate(); } } }