List of usage examples for org.hibernate SQLQuery setFirstResult
@Override
Query<R> setFirstResult(int startPosition);
From source file:org.xerela.provider.telemetry.TelemetryProvider.java
License:Mozilla Public License
/** {@inheritDoc} */ @SuppressWarnings({ "unchecked", "nls" }) public ArpPageData getArpTable(ArpPageData pageData, String ipAddress, String managedNetwork) { ZDeviceCore device = getDevice(ipAddress, managedNetwork); if (device == null) { pageData.setArpEntries(new ArpTableEntry[0]); pageData.setTotal(0);//from w w w. jav a 2 s . c om return pageData; } boolean ownTransaction = TransactionElf.beginOrJoinTransaction(); try { Session session = TelemetryActivator.getSessionFactory().getCurrentSession(); String fromClause = "FROM discovery_arp arp WHERE arp.device_id = " + device.getDeviceId(); SQLQuery query = session.createSQLQuery( "SELECT ip_address, mac_address, interface " + fromClause + " ORDER BY ip_address"); query.addScalar("ip_address", Hibernate.STRING); query.addScalar("mac_address", Hibernate.LONG); query.addScalar("interface", Hibernate.STRING); query.setFirstResult(pageData.getOffset()).setMaxResults(pageData.getPageSize()); query.scroll(ScrollMode.SCROLL_INSENSITIVE); List<Object[]> resultList = (List<Object[]>) query.list(); if (resultList == null || resultList.isEmpty()) { pageData.setArpEntries(new ArpTableEntry[0]); pageData.setTotal(0); return pageData; } else { if (pageData.getOffset() == 0) { // Set the total result size into the page data. query = session.createSQLQuery("SELECT count(arp.ip_address) " + fromClause); pageData.setTotal(getCount(query)); } List<ArpTableEntry> arpTable = new ArrayList<ArpTableEntry>(); for (Object[] resultEntry : resultList) { ArpTableEntry entry = new ArpTableEntry(); entry.setIpAddress((String) resultEntry[0]); entry.setMacAddress((Long) resultEntry[1]); entry.setInterfaceName((String) resultEntry[2]); arpTable.add(entry); } pageData.setArpEntries(arpTable.toArray(new ArpTableEntry[0])); return pageData; } } finally { if (ownTransaction) { TransactionElf.commit(); } } }
From source file:org.xerela.provider.telemetry.TelemetryProvider.java
License:Mozilla Public License
/** * {@inheritDoc}/*from w w w . j a v a 2 s . c om*/ */ @SuppressWarnings({ "unchecked", "nls" }) public DeviceArpPageData getArpEntries(DeviceArpPageData pageData, String networkAddress, String sort, boolean descending) { StringBuilder selectClause = new StringBuilder( "SELECT d.ip_address as device, d.network as managedNetwork, arp.device_id as device_id, arp.ip_address as ipAddress, arp.mac_address as macAddress, arp.interface as interfaceName"); StringBuilder fromClause = new StringBuilder( " FROM discovery_arp arp LEFT JOIN device d on arp.device_id = d.device_id"); if (networkAddress.indexOf('/') > 0) { Long[] hiLoRange = NetworkAddressElf.getHiLoRange(networkAddress); if (hiLoRange[0] == null) { if (NetworkAddressElf.isIPv6AddressOrMask(networkAddress)) { String[] ipAndCidr = networkAddress.split("/"); long[] hiLo = NetworkAddressElf.getHiLo(ipAndCidr[0]); fromClause.append(String.format(" WHERE arp.ip_low BETWEEN %d AND %d AND arp.ip_high=%d", hiLoRange[2], hiLoRange[1], hiLo[0])); } else { fromClause.append( String.format(" WHERE arp.ip_low BETWEEN %d AND %d", hiLoRange[2], hiLoRange[1])); } } else { fromClause .append(String.format(" WHERE arp.ip_high BETWEEN %d AND %d", hiLoRange[1], hiLoRange[0])); } } else { long[] hiLo = NetworkAddressElf.getHiLo(networkAddress); fromClause.append(String.format(" WHERE arp.ip_high=%d AND arp.ip_low=%d", hiLo[0], hiLo[1])); } selectClause.append(fromClause); if (sort != null) { selectClause.append(" ORDER BY ").append(sort); if (descending) { selectClause.append(" DESC"); } } boolean ownTransaction = TransactionElf.beginOrJoinTransaction(); try { Session session = TelemetryActivator.getSessionFactory().getCurrentSession(); SQLQuery query = session.createSQLQuery(selectClause.toString()); query.addScalar("device", Hibernate.STRING); query.addScalar("managedNetwork", Hibernate.STRING); query.addScalar("device_id", Hibernate.INTEGER); query.addScalar("ipAddress", Hibernate.STRING); query.addScalar("macAddress", Hibernate.LONG); query.addScalar("interfaceName", Hibernate.STRING); query.setFirstResult(pageData.getOffset()).setMaxResults(pageData.getPageSize()); query.scroll(ScrollMode.SCROLL_INSENSITIVE); List<Object[]> resultList = (List<Object[]>) query.list(); if (resultList == null || resultList.isEmpty()) { pageData.setArpEntries(new DeviceArpTableEntry[0]); pageData.setTotal(0); return pageData; } else { if (pageData.getOffset() == 0) { // Set the total result size into the page data. query = session.createSQLQuery("SELECT count(arp.ip_address) " + fromClause.toString()); pageData.setTotal(getCount(query)); } List<DeviceArpTableEntry> arpTable = new ArrayList<DeviceArpTableEntry>(); for (Object[] resultEntry : resultList) { DeviceArpTableEntry entry = new DeviceArpTableEntry(); entry.setDevice((String) resultEntry[0]); entry.setManagedNetwork((String) resultEntry[1]); entry.setDeviceId((Integer) resultEntry[2]); entry.setIpAddress((String) resultEntry[3]); entry.setMacAddress((Long) resultEntry[4]); entry.setInterfaceName((String) resultEntry[5]); arpTable.add(entry); } pageData.setArpEntries(arpTable.toArray(new DeviceArpTableEntry[0])); return pageData; } } finally { if (ownTransaction) { TransactionElf.commit(); } } }
From source file:org.yamj.core.hibernate.HibernateDao.java
License:Open Source License
/** * Execute a query to return the results * * Gets the options from the wrapper for start and max * * Puts the total count returned from the query into the wrapper * * @param <T>//from w w w. jav a 2 s .c o m * @param T The class to return the transformed results of. * @param sqlScalars * @param wrapper * @return */ @SuppressWarnings("rawtypes") public <T> List<T> executeQueryWithTransform(Class T, SqlScalars sqlScalars, IApiWrapper wrapper) { SQLQuery query = sqlScalars.createSqlQuery(currentSession()); query.setReadOnly(true); query.setCacheable(true); if (T != null) { if (T.equals(String.class)) { // no transformer needed } else if (T.equals(Long.class)) { // no transformer needed } else if (T.equals(Object[].class)) { // no transformer needed } else { query.setResultTransformer(Transformers.aliasToBean(T)); } } // Add the scalars to the query sqlScalars.populateScalars(query); List<T> queryResults = query.list(); // If the wrapper is populated, then run the query to get the maximum results if (wrapper != null) { wrapper.setTotalCount(queryResults.size()); // If there is a start or max set, we will need to re-run the query after setting the options IOptions options = wrapper.getOptions(); if (options != null) { if (options.getStart() > 0 || options.getMax() > 0) { if (options.getStart() > 0) { query.setFirstResult(options.getStart()); } if (options.getMax() > 0) { query.setMaxResults(options.getMax()); } // This will get the trimmed list queryResults = query.list(); } } } return queryResults; }