List of usage examples for org.hibernate Query list
List<R> list();
From source file:au.com.nicta.ct.solution.tracking.CtTrackingModel.java
License:Open Source License
protected void deleteTracks(boolean showProgress) { // i.e. all tracks if (!valid()) return;//from w w w . j av a 2 s . co m // CtSolutions solution = (CtSolutions)CtObjectDirectory.get( "solution" ); // String hql1 = " DELETE FROM CtTracksDetections td " // + " WHERE td.ctTracks IN ( " // + " SELECT t FROM CtTracks t " // + " INNER JOIN t.ctSolutions s " // + " WHERE s.pkSolution = " + solution.getPkSolution() +" ) "; String hql1 = " SELECT td FROM CtTracksDetections td " + " WHERE td.ctTracks IN ( " + " SELECT t FROM CtTracks t " + " INNER JOIN t.ctSolutions s " + " WHERE s.pkSolution = " + _s.getPkSolution() + " ) "; // String hql1 = " SELECT t FROM CtTracks t " // + " INNER JOIN t.ctSolutions s " // + " WHERE s.pkSolution = " + solution.getPkSolution();// +" ) "; String hql2 = " DELETE FROM CtTracksDetections td " + " WHERE td IN (:vals) "; String hql3 = " DELETE FROM CtTracks t " + " WHERE t.ctSolutions = " + _s.getPkSolution(); // clear(); Session session = CtSession.Current(); session.beginTransaction(); Query q1 = session.createQuery(hql1); List results = q1.list(); if (results.isEmpty()) { session.getTransaction().commit(); return; // mnothing changed } Query q2 = session.createQuery(hql2); q2.setParameterList("vals", results); int rowCount2 = q2.executeUpdate(); // this query fails Query q3 = session.createQuery(hql3); int rowCount3 = q3.executeUpdate(); session.getTransaction().commit(); session.refresh(_s); //////////////////////////////////////////////////////////////////////// // Manually refresh detections and tracks as seems to be a bug with // their consistency //////////////////////////////////////////////////////////////////////// CtPageFrame.showWaitCursor(); Set<CtDetections> cd = _s.getCtDetectionses(); Set<CtTracks> ct = _s.getCtTrackses(); for (CtDetections d : cd) { session.refresh(d); } for (CtTracks t : ct) { session.refresh(t); } CtPageFrame.showDefaultCursor(); //////////////////////////////////////////////////////////////////////// refresh(_cc, _ism, _twm, _s, showProgress); // fireModelChanged(); in update anyway }
From source file:au.com.nicta.ct.solution.tracking.jipda.CtTkDBTracker.java
License:Open Source License
static void dropAllExistingTracks(Session session) { // Clear the current solution of all tracks String hql = "SELECT ctTracksDetections" + " FROM CtTracksDetections as ctTracksDetections" + " JOIN ctTracksDetections.ctTracks ctTracks" + " WHERE ctTracks.ctSolutions = :solutionPk"; Query q = session.createQuery(hql); q.setInteger("solutionPk", CtSolutionController.getSolutions().getPkSolution()); List<CtTracksDetections> results = q.list(); for (CtTracksDetections td : results) { session.delete(td);//from w w w. ja va 2 s . com } hql = "SELECT ctTracks" + " FROM CtTracks as ctTracks" + " WHERE ctTracks.ctSolutions = :solutionPk"; q = session.createQuery(hql); q.setInteger("solutionPk", CtSolutionController.getSolutions().getPkSolution()); List<CtTracks> results2 = q.list(); for (CtTracks t : results2) { session.delete(t); } }
From source file:au.com.nicta.ct.solution.tracking.jipda.CtTkDBTracker.java
License:Open Source License
static void dropExistingTracks(Session session, TkTracks tracks, int startTimeIdxInclusive, int endTimeIdxInclusive) { CtSolutions solution = (CtSolutions) CtObjectDirectory.get("solution"); // Drop all existing track detection associations int trackCnt = 0; for (TkTrack t : tracks) { ++trackCnt;/*from www . j a va2s .c o m*/ for (TkTrackElement e : t.elements) { TkDetection d = e.det; if (d == null) { continue; } System.out.println("d.timeIdx: " + (d.timeIdx)); System.out.println("trackCnt: " + (trackCnt)); System.out.println("total no. tracks: " + (trackCnt)); if (d.timeIdx == startTimeIdxInclusive || d.timeIdx == endTimeIdxInclusive) { continue; } Integer detectionPk = d.find(DETECTION_PK); String hql = "SELECT ctTracksDetections" + " FROM CtTracksDetections as ctTracksDetections" + " JOIN ctTracksDetections.ctDetections ctDetections" + " WHERE ctDetections.pkDetection = :detectionPk"; Query q = session.createQuery(hql); q.setInteger("detectionPk", detectionPk); List<CtTracksDetections> results = q.list(); HashSet<CtTracks> modified = new HashSet<CtTracks>(); for (CtTracksDetections td : results) { CtTracks t_n = td.getCtTracks(); CtDetections d_n = td.getCtDetections(); CtSolutions s = t_n.getCtSolutions(); if (s.getPkSolution() == solution.getPkSolution()) { // if( td.getCtTracks().getCtSolutions().getPkSolution() == solution.getPkSolution() ) { t_n.getCtTracksDetectionses().remove(td); d_n.getCtTracksDetectionses().remove(td); session.delete(td); modified.add(t_n); } } // now clean up tracks that have no surviving detections: for (CtTracks t_n : modified) { Set<CtTracksDetections> tds = t_n.getCtTracksDetectionses(); if (tds.isEmpty()) { CtSolutions s = t_n.getCtSolutions(); s.getCtTrackses().remove(t_n); session.delete(t); session.update(s); } } // String hql = // "DELETE from CtTracksDetections as ctTracksDetections" // + " WHERE fk_detection = " + detectionPk; // System.out.println("hql: " + (hql) ); // // Query query = session.createQuery(hql); // int row = query.executeUpdate(); // System.out.println("Rows deleted: " + row); } } }
From source file:au.com.nicta.ct.solution.tracking.jipda.CtTkDBTracker.java
License:Open Source License
static CtTracks findExistingTrackAtStart(Session session, TkTrack t, int startTimeIdxInclusive) { // session.flush(); TkDetection firstDetection = t.elements.get(0).det; if (firstDetection == null) { throw new Error("First detection of a track can not be null"); }//from ww w.j a va2s.c o m // join first detection to any existing tracks if (firstDetection.timeIdx != startTimeIdxInclusive) { return null; } CtSolutions solution = (CtSolutions) CtObjectDirectory.get("solution"); Integer detectionPk = firstDetection.find(DETECTION_PK); // retrieve the track that contains the detection String hql = " SELECT ctTD" + " FROM CtTracksDetections as ctTD" + " JOIN ctTD.ctDetections as ctD" + " WHERE ctD.pkDetection = :detectionPk"; System.out.println("hql: " + (hql)); // session.flush(); Query q = session.createQuery(hql); q.setInteger("detectionPk", detectionPk); List<CtTracksDetections> results = q.list(); // make sure there's only one track associated with this detection CtTracks track = null; for (CtTracksDetections td : results) { if (td.getCtTracks().getCtSolutions().getPkSolution() == solution.getPkSolution()) { if (track != null) { throw new Error("Detection associated with more than 1 track," + "this should not happen when all tracks-detections assoc" + "have been cleaned between start and end time idx."); } track = td.getCtTracks(); // use existing one } } return track; // may be null if detection is not in a track }
From source file:au.com.nicta.ct.solution.tracking.jipda.CtTkDBTracker.java
License:Open Source License
static boolean mergeTracksAtEnd(Session session, TkTrack t, int endTimeIdxInclusive, CtTracks tt) { // session.flush(); TkDetection lastDetection = t.getLast().det; if (lastDetection == null) { return false; // no detection associated at track's end. }/*from w w w . j a v a2 s .co m*/ // join first detection to any existing tracks if (lastDetection.timeIdx != endTimeIdxInclusive) { return false; // no detection on the boundary } Integer detectionPk = lastDetection.find(DETECTION_PK); // retrieve all tracks that contains the detection // note that this last detection may be a place for a fork. String hql = "SELECT ctTracksDetections" + " FROM CtTracksDetections as ctTracksDetections" + " JOIN ctTracksDetections.ctDetections ctDetections" + " WHERE ctDetections.pkDetection = :detectionPk"; Query q = session.createQuery(hql); q.setInteger("detectionPk", detectionPk); List results = q.list(); if (results.isEmpty()) { return false; // no existing tracks so no need to remap } if (results.size() > 1) { // this is a forking detection. When gets here: // - all tracks-detection assoc before this detection has been cleared // And since this is a fork, the track does not continue past this // detection. Since this detection will be associated with the new track // we don't have to remap anything. return false; } CtTracksDetections td = (CtTracksDetections) results.get(0); Set<CtTracksDetections> remap = td.getCtTracks().getCtTracksDetectionses(); for (CtTracksDetections td2 : remap) { System.out.println( "Remapping: " + td2.getCtTracks().getPkTrack() + " " + td2.getCtDetections().getPkDetection() + " to: " + tt.getPkTrack() + " " + td2.getCtDetections().getPkDetection()); if (td2.getCtTracks().getPkTrack() == tt.getPkTrack()) { continue; // no change } CtTracks tt0 = td2.getCtTracks(); td2.setCtTracks(tt); tt0.getCtTracksDetectionses().remove(td2); tt.getCtTracksDetectionses().add(td2); session.update(td2); session.update(tt); session.update(tt0); } // session.flush(); return true; }
From source file:au.com.nicta.ct.solution.tracking.jipda.CtTkDBTracker.java
License:Open Source License
static void removeEmptyTracks(Session session) { CtSolutions solution = (CtSolutions) CtObjectDirectory.get("solution"); String hql = "SELECT ctTracks" + " FROM CtTracks as ctTracks" + " JOIN ctTracks.ctSolutions as ctSolutions" + " WHERE ctSolutions.pkSolution = :solutionPk"; Query q = session.createQuery(hql); q.setInteger("solutionPk", solution.getPkSolution()); List<CtTracks> results = q.list(); for (CtTracks t : results) { Set<CtTracksDetections> s = t.getCtTracksDetectionses(); // System.out.println("t.getPkTrack(): " + (t.getPkTrack()) ); // System.out.println("t.getCtTracksDetectionses().size(): " + (t.getCtTracksDetectionses().size()) ); // System.out.println("s.isEmpty(): " + (s.isEmpty()) );; if (s.isEmpty()) { // empty track, delete it solution.getCtTrackses().remove(t); session.update(solution);// w w w.j a v a 2s . c o m session.delete(t); } } }
From source file:au.edu.anu.metadatastores.datamodel.aries.grants.ForCodesTest.java
License:Open Source License
@Test public void test2() { Session session = AriesHibernateUtil.getSessionFactory().openSession(); try {/*from ww w .j a v a 2 s . co m*/ Query query = session.createQuery("FROM ForCodes"); // query.setParameter("code", "111706"); List<ForCodes> forCodes = query.list(); assertNotNull("No Fields of Research Found", forCodes); assertTrue("No field of research codes found", forCodes.size() > 0); } finally { session.close(); } }
From source file:au.edu.anu.metadatastores.services.aries.ContractId.java
License:Open Source License
/** * Get the university ids of the investigators for the given contract * // w w w . j a v a 2 s .co m * @param contractId The id of the contract to retrieve university ids for * @return The associated university ids */ public String[] getInvestigatorUniId(String contractId) { Session session = AriesHibernateUtil.getSessionFactory().openSession(); try { Query query = session .createQuery("from ContractsGrantsInvestigators where chrContractCode = :contractId"); query.setParameter("contractId", contractId); @SuppressWarnings("unchecked") List<ContractsGrantsInvestigators> investigators = query.list(); List<String> staffIds = new ArrayList<String>(); for (ContractsGrantsInvestigators investigator : investigators) { if (investigator != null) { if (investigator.getChrStaffNumber() != null && !staffIds.contains(investigator.getChrStaffNumber())) { staffIds.add(investigator.getChrStaffNumber()); } } } return staffIds.toArray(new String[0]); } finally { session.close(); } }
From source file:au.edu.anu.metadatastores.services.aries.ContractId.java
License:Open Source License
/** * Get the funds provider reference number of the grant * // ww w. jav a2s . c om * @param anuActivityId The grant id * @return The reference number */ public String getANUActivityAuthorizeID(String anuActivityId) { Session session = AriesHibernateUtil.getSessionFactory().openSession(); try { Query query = session.createQuery("from ContractsGrantsMain where chrContractCode = :activityId"); query.setParameter("activityId", anuActivityId); @SuppressWarnings("unchecked") List<ContractsGrantsMain> grants = query.list(); String authorId = null; if (grants != null && grants.size() > 0) { authorId = grants.get(0).getChrSchemeRef(); } return authorId; } finally { session.close(); } }
From source file:au.edu.anu.metadatastores.services.aries.ContractId.java
License:Open Source License
/** * Get the grant description//w w w .j a v a2s . co m * * @param anuActivityId The grant id * @return The description */ public String getANUActivityDesc(String anuActivityId) { Session session = AriesHibernateUtil.getSessionFactory().openSession(); try { Query query = session.createQuery("from ContractsGrantsMain where chrContractCode = :activityId"); query.setParameter("activityId", anuActivityId); @SuppressWarnings("unchecked") List<ContractsGrantsMain> grants = query.list(); String description = null; if (grants != null && grants.size() > 0) { description = grants.get(0).getChrShortTitle(); } return description; } finally { session.close(); } }