List of usage examples for org.hibernate Session createSQLQuery
@Override NativeQuery createSQLQuery(String queryString);
From source file:com.dgc.DAO.GenericDao.java
@SuppressWarnings("unchecked") public List<Object> createNativeQuery(String createdQuery) throws SQLException { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction();/*from w w w. j av a 2 s . co m*/ Query query = session.createSQLQuery(createdQuery); List<Object> result = new ArrayList<Object>((ArrayList<Object>) query.list()); session.getTransaction().commit(); return result; }
From source file:com.duroty.application.files.manager.FilesManager.java
License:Open Source License
/** * DOCUMENT ME!/*from w w w . j ava 2 s . c o m*/ * * @param hsession DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param folderName DOCUMENT ME! * @param page DOCUMENT ME! * @param messagesByPage DOCUMENT ME! * @param order DOCUMENT ME! * @param orderType DOCUMENT ME! * * @return DOCUMENT ME! * * @throws FilesException DOCUMENT ME! */ public Vector getFiles(Session hsession, String repositoryName, String folderName, int label, int page, int messagesByPage, int order, String orderType) throws FilesException { Vector files = new Vector(); try { Users user = getUser(hsession, repositoryName); Locale locale = new Locale(user.getUseLanguage()); TimeZone timeZone = TimeZone.getDefault(); Date now = new Date(); Calendar calendar = Calendar.getInstance(timeZone, locale); calendar.setTime(now); SimpleDateFormat formatter1 = new SimpleDateFormat("MMM dd", locale); SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss", locale); SimpleDateFormat formatter3 = new SimpleDateFormat("MM/yy", locale); Query hquery = null; String[] folderNameList = new String[0]; try { folderName = parseFolder(folderName); folderNameList = new String[] { folderName }; if (folderName.equals(this.folderAll) || folderName.equals(this.folderHidden)) { folderNameList = new String[] { this.folderAll, this.folderDraft, this.folderHidden, this.folderImportant, this.folderInbox, this.folderSent }; } } catch (Exception ex) { } if ((folderNameList.length == 0) && (label <= 0)) { hquery = hsession.getNamedQuery("attachments"); } else if ((folderNameList.length > 0) && (label <= 0)) { hquery = hsession.getNamedQuery("attachments-by-folder"); } else if ((folderNameList.length == 0) && (label > 0)) { hquery = hsession.getNamedQuery("attachments-by-label"); } else if ((folderNameList.length > 0) && (label > 0)) { hquery = hsession.getNamedQuery("attachments-by-folder-label"); } String aux = hquery.getQueryString(); switch (order) { case ORDER_BY_SIZE: if (orderType.equals("ASC")) { aux += " order by att_size asc"; } else { aux += " order by att_size desc"; } break; case ORDER_BY_DATE: if (orderType.equals("ASC")) { aux += " order by mes_date asc"; } else { aux += " order by mes_date desc"; } break; case ORDER_BY_TYPE: if (orderType.equals("ASC")) { aux += " order by att_content_type asc"; } else { aux += " order by att_content_type desc"; } break; default: if (!orderType.equals("ASC")) { aux += " order by att_name desc"; } else { aux += " order by att_name asc"; } break; } SQLQuery h2query = hsession.createSQLQuery(aux); if ((folderNameList.length == 0) && (label <= 0)) { h2query.setParameterList("no_boxes", new String[] { this.folderTrash, this.folderChat, this.folderSpam, FOLDER_DELETE }); h2query.setInteger("user", getUser(hsession, repositoryName).getUseIdint()); } else if ((folderNameList.length > 0) && (label <= 0)) { h2query.setParameterList("boxes", folderNameList); h2query.setInteger("user", getUser(hsession, repositoryName).getUseIdint()); } else if ((folderNameList.length == 0) && (label > 0)) { h2query.setInteger("label", label); h2query.setParameterList("no_boxes", new String[] { this.folderTrash, this.folderChat, this.folderSpam, FOLDER_DELETE }); h2query.setInteger("user", getUser(hsession, repositoryName).getUseIdint()); } else if ((folderNameList.length > 0) && (label > 0)) { h2query.setInteger("label", label); h2query.setParameterList("boxes", folderNameList); h2query.setInteger("user", getUser(hsession, repositoryName).getUseIdint()); } h2query.setFirstResult(page * messagesByPage); h2query.setMaxResults(messagesByPage); h2query.addEntity("testo", AttachmentWithDate.class); ScrollableResults scroll = h2query.scroll(); while (scroll.next()) { AttachmentWithDate attachment = (AttachmentWithDate) scroll.get(0); AttachmentObj obj = new AttachmentObj(); obj.setContentType(attachment.getAttContentType()); Date date = attachment.getAttDate(); if (date != null) { Calendar calendar2 = Calendar.getInstance(timeZone, locale); calendar2.setTime(date); if ((calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) && (calendar.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)) && (calendar.get(Calendar.DATE) == calendar2.get(Calendar.DATE))) { obj.setDateStr(formatter2.format(calendar2.getTime())); } else if (calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) { obj.setDateStr(formatter1.format(calendar2.getTime())); } else { obj.setDateStr(formatter3.format(calendar2.getTime())); } } obj.setDate(date); obj.setDate(date); obj.setIdint(attachment.getAttIdint()); obj.setName(attachment.getAttName()); obj.setPart(attachment.getAttPart()); int size = attachment.getAttSize(); size /= 1024; if (size > 1024) { size /= 1024; obj.setSize(size + " MB"); } else { obj.setSize(((size > 0) ? (size + "") : "<1") + " kB"); } String extension = (String) this.extensions.get(attachment.getAttContentType()); if (StringUtils.isBlank(extension)) { extension = "generic"; } obj.setExtension(extension); Message message = attachment.getMessage(); if (message.isMesFlagged()) { obj.setFlagged(true); } else { obj.setFlagged(false); } if (message.getLabMeses() != null) { Iterator it = message.getLabMeses().iterator(); StringBuffer lab = new StringBuffer(); while (it.hasNext()) { if (lab.length() > 0) { lab.append(", "); } LabMes labMes = (LabMes) it.next(); lab.append(labMes.getId().getLabel().getLabName()); } if (lab.length() > 0) { obj.setLabel(lab.toString()); } else { } } obj.setBox(message.getMesBox()); obj.setMid(message.getMesName()); files.addElement(obj); } return files; } catch (Exception e) { throw new FilesException(e); } finally { GeneralOperations.closeHibernateSession(hsession); } }
From source file:com.duroty.application.mail.manager.MailManager.java
License:Open Source License
/** * DOCUMENT ME!//from w ww .j a v a2 s .c o m * * @param hsession DOCUMENT ME! * @param repositoryName DOCUMENT ME! * @param label DOCUMENT ME! * @param page DOCUMENT ME! * @param messagesByPage DOCUMENT ME! * @param order DOCUMENT ME! * @param orderType DOCUMENT ME! * * @return DOCUMENT ME! * * @throws MailException DOCUMENT ME! */ public Vector getMessages(Session hsession, String repositoryName, Label label, int page, int messagesByPage, int order, String orderType) throws MailException { Vector messages = new Vector(); try { Users user = getUser(hsession, repositoryName); Locale locale = new Locale(user.getUseLanguage()); TimeZone timeZone = TimeZone.getDefault(); Date now = new Date(); Calendar calendar = Calendar.getInstance(timeZone, locale); calendar.setTime(now); SimpleDateFormat formatter1 = new SimpleDateFormat("MMM dd", locale); SimpleDateFormat formatter2 = new SimpleDateFormat("HH:mm:ss", locale); SimpleDateFormat formatter3 = new SimpleDateFormat("MM/yy", locale); Query hquery = hsession.getNamedQuery("messages-by-label"); String aux = hquery.getQueryString(); switch (order) { case ORDER_BY_IMPORTANT: if (orderType.equals("ASC")) { aux += " order by mes_flagged asc"; } else { aux += " order by mes_flagged desc"; } break; case ORDER_BY_ADDRESS: if (orderType.equals("ASC")) { aux += " order by mes_from asc"; } else { aux += " order by mes_from desc"; } break; case ORDER_BY_SIZE: if (orderType.equals("ASC")) { aux += " order by mes_size asc"; } else { aux += " order by mes_size desc"; } break; case ORDER_BY_SUBJECT: if (orderType.equals("ASC")) { aux += " order by mes_subject asc"; } else { aux += " order by mes_subject desc"; } break; case ORDER_BY_DATE: if (orderType.equals("ASC")) { aux += " order by mes_date asc"; } else { aux += " order by mes_date desc"; } break; case ORDER_BY_UNREAD: if (orderType.equals("ASC")) { aux += " order by mes_recent asc"; } else { aux += " order by mes_recent desc"; } break; default: aux += " order by mes_date desc"; break; } SQLQuery h2query = hsession.createSQLQuery(aux); h2query.addEntity("i", Message.class); h2query.setInteger("label", label.getLabIdint()); h2query.setInteger("user", user.getUseIdint()); h2query.setString("folderTrash", this.folderTrash); h2query.setString("folderSpam", this.folderSpam); h2query.setString("folderDelete", FOLDER_DELETE); h2query.setFirstResult(page * messagesByPage); h2query.setMaxResults(messagesByPage); ScrollableResults scroll = h2query.scroll(); while (scroll.next()) { Message message = (Message) scroll.get(0); MessageObj obj = new MessageObj(message.getMesName()); obj.setBox(message.getMesBox()); obj.setFrom(message.getMesFrom()); if ((message.getAttachments() != null) && (message.getAttachments().size() > 0)) { obj.setHasAttachment(true); } else { obj.setHasAttachment(false); } int size = message.getMesSize(); size /= 1024; if (size > 1024) { size /= 1024; obj.setSize(size + " MB"); } else { obj.setSize(((size > 0) ? (size + "") : "<1") + " kB"); } if (message.getMesBox().equals(folderSent)) { try { obj.setEmail(message.getMesTo()); } catch (Exception e) { obj.setEmail("unknown to"); } } else { obj.setEmail(message.getMesFrom()); } Date date = message.getMesDate(); if (date != null) { Calendar calendar2 = Calendar.getInstance(timeZone, locale); calendar2.setTime(date); if ((calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) && (calendar.get(Calendar.MONTH) == calendar2.get(Calendar.MONTH)) && (calendar.get(Calendar.DATE) == calendar2.get(Calendar.DATE))) { obj.setDateStr(formatter2.format(calendar2.getTime())); } else if (calendar.get(Calendar.YEAR) == calendar2.get(Calendar.YEAR)) { obj.setDateStr(formatter1.format(calendar2.getTime())); } else { obj.setDateStr(formatter3.format(calendar2.getTime())); } } obj.setDate(date); if (message.getLabMeses() != null) { Iterator it = message.getLabMeses().iterator(); StringBuffer buff = new StringBuffer(); while (it.hasNext()) { if (buff.length() > 0) { buff.append(", "); } LabMes labMes = (LabMes) it.next(); buff.append(labMes.getId().getLabel().getLabName()); } obj.setLabel(buff.toString()); } try { if (StringUtils.isBlank(message.getMesSubject())) { obj.setSubject("(no subject)"); } else { obj.setSubject(message.getMesSubject()); } } catch (Exception ex) { obj.setSubject("(no subject)"); } if (message.isMesFlagged()) { obj.setFlagged(true); } else { obj.setFlagged(false); } if (message.isMesRecent()) { obj.setRecent(true); } else { obj.setRecent(false); } String priority = "normal"; if (MessageUtilities.isHighPriority(message.getMesHeaders())) { priority = "high"; } else if (MessageUtilities.isLowPriority(message.getMesHeaders())) { priority = "low"; } obj.setPriority(priority); messages.addElement(obj); } return messages; } catch (Exception e) { throw new MailException(e); } finally { GeneralOperations.closeHibernateSession(hsession); } }
From source file:com.ett.drv.biz.DrvQueryHelperImp.java
public List<ComboxObject> getDict(String xtlb, String type) { List<ComboxObject> listTmp = new ArrayList<ComboxObject>(); SessionFactory sessionFactory = this.getBaseDaoDrv().getHibernateSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); String queryStr = MessageFormat.format(DictSql, xtlb, type); logger.debug("?" + queryStr.toString()); SQLQuery query = session.createSQLQuery(queryStr.toString()); query.addScalar("value", new org.hibernate.type.StringType()); query.addScalar("text", new org.hibernate.type.StringType()); query.setResultTransformer(Transformers.aliasToBean(ComboxObject.class)); try {/*from ww w. j a v a2 s . co m*/ tx.begin(); //query.uniqueResult() listTmp = query.list(); if (logger.isInfoEnabled()) { logger.info("==" + listTmp.size()); } if (listTmp.size() == 0) { throw new ObjectDontExistException("?"); } session.flush(); tx.commit(); } catch (Exception e) { e.printStackTrace(); logger.info(e); if (tx != null) { tx.rollback(); } throw new ObjectDontExistException(e); } finally { session.close(); } return listTmp; }
From source file:com.ett.drv.biz.DrvQueryHelperImp.java
public DrvUser getDrvUser(String idcard, String glbm) { //System.out.println("Dao"); logger.debug("DAO" + this.getBaseDaoDrv()); logger.debug("??" + this.user); if (this.user != null) { return user; }/* w w w . j av a2s . c o m*/ logger.debug("???" + idcard + "??" + glbm + "??"); List lists = new ArrayList(); SessionFactory sessionFactory = this.getBaseDaoDrv().getHibernateSessionFactory(); Session session = sessionFactory.openSession(); logger.debug("baseDaoDrv?"); Transaction tx = session.beginTransaction(); logger.debug("Sessionsql?"); String queryStr = MessageFormat.format(getDrvUser, idcard, glbm); logger.debug("??" + queryStr.toString()); SQLQuery query = session.createSQLQuery(queryStr.toString()); query.addScalar("lsh", new org.hibernate.type.StringType()); query.addScalar("sfzmhm", new org.hibernate.type.StringType()); query.addScalar("xb", new org.hibernate.type.IntegerType()); query.addScalar("xm", new org.hibernate.type.StringType()); query.addScalar("csrq", new org.hibernate.type.DateType()); query.addScalar("lxzsxxdz", new org.hibernate.type.StringType()); query.addScalar("lxdh", new org.hibernate.type.StringType()); query.addScalar("sjhm", new org.hibernate.type.StringType()); query.setResultTransformer(Transformers.aliasToBean(DrvUser.class)); try { tx.begin(); //query.uniqueResult() lists = query.list(); if (logger.isInfoEnabled()) { logger.info("??==" + lists.size()); } if (lists.size() == 0) { return null; //throw new ObjectDontExistException("??"); } session.flush(); tx.commit(); } catch (Exception e) { e.printStackTrace(); logger.info(e); if (tx != null) { tx.rollback(); } throw new ObjectDontExistException(e); } finally { session.close(); } this.user = (DrvUser) lists.get(0); return this.user; }
From source file:com.ett.drv.biz.DrvQueryHelperImp.java
public DrvUser getUserWithExamCard(DrvUser user) { DrvUser tmpuser = user;/*w w w.ja va2s. c o m*/ List lists = new ArrayList(); SessionFactory sessionFactory = this.getBaseDaoDrv().getHibernateSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); String queryStr = MessageFormat.format(getExamCard, tmpuser.getSfzmhm()); logger.debug("??" + queryStr.toString()); SQLQuery query = session.createSQLQuery(queryStr.toString()); query.addScalar("dabh", new org.hibernate.type.StringType()); query.addScalar("zkzmbh", new org.hibernate.type.StringType()); query.addScalar("zkcx", new org.hibernate.type.StringType()); query.addScalar("cclzrq", new org.hibernate.type.TimestampType()); query.addScalar("fzrq", new org.hibernate.type.TimestampType()); query.addScalar("jxdm", new org.hibernate.type.StringType()); //GregorianCalendar query.addScalar("yxqz", new org.hibernate.type.TimestampType()); query.addScalar("yxqs", new org.hibernate.type.TimestampType()); query.setResultTransformer(Transformers.aliasToBean(DrvUser.class)); try { tx.begin(); //query.uniqueResult() lists = query.list(); if (logger.isInfoEnabled()) { logger.info("getUserWithExamCard?==" + lists.size()); } if (lists.size() == 0) { return null; //throw new ObjectDontExistException("???"); } session.flush(); tx.commit(); } catch (Exception e) { e.printStackTrace(); logger.info(e); if (tx != null) { tx.rollback(); } throw new ObjectDontExistException(e); } finally { session.close(); } DrvUser tmp = (DrvUser) lists.get(0); tmpuser.setDabh(tmp.getDabh()); tmpuser.setZkzmbh(tmp.getZkzmbh()); tmpuser.setYxqs(tmp.getYxqs()); tmpuser.setYxqz(tmp.getYxqz()); tmpuser.setZkcx(tmp.getZkcx()); tmpuser.setCclzrq(tmp.getCclzrq()); tmpuser.setFzrq(tmp.getFzrq()); tmpuser.setJxdm(tmp.getJxdm()); return tmpuser; }
From source file:com.ett.drv.biz.DrvQueryHelperImp.java
public DrvUser getUserWithLicense(DrvUser user) { DrvUser tmpuser = user;/*from w ww . j a va 2s .com*/ List lists = new ArrayList(); SessionFactory sessionFactory = this.getBaseDaoDrv().getHibernateSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); String queryStr = MessageFormat.format(getLicense, tmpuser.getSfzmhm()); logger.debug("getUserWithLicense?" + queryStr.toString()); SQLQuery query = session.createSQLQuery(queryStr.toString()); query.addScalar("zkcx", new org.hibernate.type.StringType()); query.addScalar("dabh", new org.hibernate.type.StringType()); query.addScalar("yxqz", new org.hibernate.type.TimestampType()); query.addScalar("yxqs", new org.hibernate.type.TimestampType()); query.addScalar("cclzrq", new org.hibernate.type.TimestampType()); query.addScalar("syrq", new org.hibernate.type.TimestampType()); query.addScalar("qfrq", new org.hibernate.type.TimestampType()); query.addScalar("jxdm", new org.hibernate.type.StringType()); query.setResultTransformer(Transformers.aliasToBean(DrvUser.class)); try { tx.begin(); //query.uniqueResult() lists = query.list(); if (logger.isInfoEnabled()) { logger.info("getUserWithLicense==" + lists.size()); } if (lists.size() == 0) { return null; //throw new ObjectDontExistException("???"); } session.flush(); tx.commit(); } catch (Exception e) { e.printStackTrace(); logger.info(e); if (tx != null) { tx.rollback(); } throw new ObjectDontExistException(e); } finally { session.close(); } DrvUser tmp = (DrvUser) lists.get(0); tmpuser.setDabh(tmp.getDabh()); tmpuser.setYxqs(tmp.getYxqs()); tmpuser.setYxqz(tmp.getYxqz()); tmpuser.setCclzrq(tmp.getCclzrq()); tmpuser.setQfrq(tmp.getQfrq()); tmpuser.setSyrq(tmp.getSyrq()); tmpuser.setJxdm(tmp.getJxdm()); tmpuser.setZkcx(tmp.getZkcx()); return tmpuser; }
From source file:com.ett.drv.biz.DrvQueryHelperImp.java
public List<ComboxObject> getDict(String xtlb, String type, String glbm) { List<ComboxObject> listTmp = new ArrayList<ComboxObject>(); SessionFactory sessionFactory = this.getBaseDaoDrv().getHibernateSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); String queryStr = MessageFormat.format(DictSqlWithGlbm, xtlb, type, glbm); logger.debug("DictSqlWithGlbm?" + queryStr.toString()); SQLQuery query = session.createSQLQuery(queryStr.toString()); query.addScalar("value", new org.hibernate.type.StringType()); query.addScalar("text", new org.hibernate.type.StringType()); query.setResultTransformer(Transformers.aliasToBean(ComboxObject.class)); try {/* w ww .j a v a 2 s .c o m*/ tx.begin(); //query.uniqueResult() listTmp = query.list(); if (logger.isInfoEnabled()) { logger.info("==" + listTmp.size()); } if (listTmp.size() == 0) { return null; //throw new ObjectDontExistException("?"); } session.flush(); tx.commit(); } catch (Exception e) { e.printStackTrace(); logger.info(e); if (tx != null) { tx.rollback(); } throw new ObjectDontExistException(e); } finally { session.close(); } return listTmp; }
From source file:com.ett.self.print.biz.PrintBizImpl.java
protected void searchFlowFromSql(String sql, String idcard) { SessionFactory sessionFactory = this.getBaseDaoDrv().getHibernateSessionFactory(); Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); String queryStr = MessageFormat.format(sql, idcard); log.debug("?" + queryStr.toString()); SQLQuery query = session.createSQLQuery(queryStr.toString()); query.addScalar("ywlx", new org.hibernate.type.StringType()); query.addScalar("fee", new org.hibernate.type.StringType()); query.addScalar("ywyy", new org.hibernate.type.StringType()); query.addScalar("yyrq", new org.hibernate.type.TimestampType()); query.addScalar("ksrq", new org.hibernate.type.TimestampType()); query.addScalar("slrq", new org.hibernate.type.TimestampType()); query.addScalar("lsh", new org.hibernate.type.StringType()); query.addScalar("xm", new org.hibernate.type.StringType()); query.addScalar("hphm", new org.hibernate.type.StringType()); query.addScalar("jdsbh", new org.hibernate.type.StringType()); query.addScalar("sfzmhm", new org.hibernate.type.StringType()); query.addScalar("zjcx", new org.hibernate.type.StringType()); query.addScalar("dabh", new org.hibernate.type.StringType()); query.setResultTransformer(Transformers.aliasToBean(DrvPersonFlowObject.class)); try {//from w w w. ja v a 2s .c o m tx.begin(); //query.uniqueResult() this.lists = query.list(); if (log.isInfoEnabled()) { log.info("?==" + lists.size()); } if (this.lists == null || this.lists.size() == 0) { this.lists = new ArrayList(); //this.lists.add(co); } session.flush(); tx.commit(); } catch (Exception e) { e.printStackTrace(); log.info(e); if (tx != null) { tx.rollback(); } } finally { session.close(); } //return null; }
From source file:com.evolveum.midpoint.repo.sql.AddGetObjectTest.java
License:Apache License
@Test public void test100AddUserWithoutAssignmentIds() throws Exception { OperationResult result = new OperationResult("test100AddUserWithoutAssignmentIds"); PrismObject<UserType> user = PrismTestUtil.parseObject(new File(FOLDER_BASIC, "user-big.xml")); //remove ids from assignment values PrismContainer container = user.findContainer(UserType.F_ASSIGNMENT); for (PrismContainerValue value : (List<PrismContainerValue>) container.getValues()) { value.setId(null);//w ww.j a v a 2s.co m } final String OID = repositoryService.addObject(user, null, result); result.computeStatusIfUnknown(); //get user user = repositoryService.getObject(UserType.class, OID, null, result); result.computeStatusIfUnknown(); container = user.findContainer(UserType.F_ASSIGNMENT); List<Short> xmlShorts = new ArrayList<>(); for (PrismContainerValue value : (List<PrismContainerValue>) container.getValues()) { AssertJUnit.assertNotNull(value.getId()); xmlShorts.add(value.getId().shortValue()); } Collections.sort(xmlShorts); Session session = open(); try { Query query = session.createSQLQuery("select id from m_assignment where owner_oid=:oid"); query.setString("oid", OID); List<Short> dbShorts = new ArrayList<>(); for (Number n : (List<Number>) query.list()) { dbShorts.add(n.shortValue()); } Collections.sort(dbShorts); LOGGER.info("assigments ids: expected {} db {}", Arrays.toString(xmlShorts.toArray()), Arrays.toString(dbShorts.toArray())); AssertJUnit.assertArrayEquals(xmlShorts.toArray(), dbShorts.toArray()); } finally { close(session); } }