public Cargo find(TrackingId tid) { return (Cargo) getSession(). createQuery("from Cargo where trackingId = :tid"). setParameter("tid", tid). uniqueResult(); }
private Agent fetchAgentByUuid(final String uuid) { return (Agent) getHibernateTemplate().execute(session -> { Query query = session.createQuery("from Agent where uuid = :uuid"); query.setString("uuid", uuid); return query.uniqueResult(); }); } }
public List<Cargo> findAll() { return getSession().createQuery("from Cargo").list(); }
protected List getCountedStudentGradeRecords(final Long gradebookId, final String studentId) throws HibernateException { return getSessionFactory().getCurrentSession().createQuery( "select agr from AssignmentGradeRecord as agr, GradebookAssignment as asn where agr.studentId = :studentid and agr.gradableObject = asn and asn.removed is false and asn.notCounted is false and asn.gradebook.id = :gradebookid and asn.ungraded is false") .setString("studentid", studentId) .setLong("gradebookid", gradebookId) .list(); }
/** * Call setEntity() on a cacheable query - see FORGE-265 */ public List listEventsOfOrganizer(Person organizer) { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); Query query = session.createQuery("from Event ev where ev.organizer = :organizer"); query.setCacheable(true); query.setEntity("organizer", organizer); List result = query.list(); session.getTransaction().commit(); return result; }
@Test public void test2ndLevelSubClass() throws Exception { Session s = openSession(); Transaction tx = s.beginTransaction(); A320b a = new A320b(); a.setJavaEmbeddedVersion( "Elephant" ); a.setNbrOfSeats( 300 ); s.persist( a ); tx.commit(); s.close(); s = openSession(); tx = s.beginTransaction(); Query q = s.createQuery( "from " + A320.class.getName() + " as a where a.javaEmbeddedVersion = :version" ); q.setString( "version", "Elephant" ); List a320s = q.list(); assertNotNull( a320s ); assertEquals( 1, a320s.size() ); tx.commit(); s.close(); }
Query q = s.createQuery("from Simple s where s.name=?"); q.setCacheable(true); q.setString(0, "Simple 1"); assertTrue( q.list().size()==1 ); assertTrue( q.list().size()==1 ); assertTrue( q.list().size()==1 ); q = s.createQuery("from Simple s where s.name=:name"); q.setCacheable(true); q.setString("name", "Simple 1"); assertTrue( q.list().size()==1 ); simple = (Simple) q.list().get(0); q.setString("name", "Simple 2"); assertTrue( q.list().size()==0 ); assertTrue( q.list().size()==0 ); q = s.createQuery("from Simple s where s.name=:name"); q.setString("name", "Simple 2"); q.setCacheable(true); assertTrue( q.list().size()==1 ); assertTrue( q.list().size()==1 ); q = s.createQuery("from Simple s where s.name=?"); q.setCacheable(true); q.setString(0, "Simple 1");
Modification findModificationWithRevision(Session session, long materialId, String revision) { Modification modification; String key = cacheKeyForModificationWithRevision(materialId, revision); modification = (Modification) goCache.get(key); if (modification == null) { synchronized (key) { modification = (Modification) goCache.get(key); if (modification == null) { Query query = session.createQuery("FROM Modification WHERE materialId = ? and revision = ? ORDER BY id DESC"); query.setLong(0, materialId); query.setString(1, revision); modification = (Modification) query.uniqueResult(); goCache.put(key, modification); } } } return modification; }
public Integer getCountItemFacades(final Long questionPoolId) { final HibernateCallback<Number> hcb = session -> { Query q = session.createQuery("select count(ab) from ItemData ab, QuestionPoolItemData qpi where ab.itemId = qpi.itemId and qpi.questionPoolId = :id"); q.setLong("id", questionPoolId); q.setCacheable(true); return (Number) q.uniqueResult(); }; return getHibernateTemplate().execute(hcb).intValue(); }
s.save( bar2 ); List list = s.createQuery( "from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like 'Bar %'" ).list(); assertTrue( row instanceof Object[] && ( (Object[]) row ).length==3 ); Query q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like 'Bar%'"); list = q.list(); if ( !(getDialect() instanceof SAPDBDialect) ) assertTrue( list.size()==2 ); q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where ( bar.name in (:nameList) or bar.name in (:nameList) ) and bar.string = :stringVal"); HashSet nameList = new HashSet(); nameList.add( "bar" ); q = s.createQuery("select bar, b from Bar bar inner join bar.baz baz inner join baz.cascadingBars b where bar.name like 'Bar%'"); Object result = q.uniqueResult(); assertTrue( result != null ); q = s.createQuery("select bar, b from Bar bar left join bar.baz baz left join baz.cascadingBars b where bar.name like :name and b.name like :name"); q.setString( "name", "Bar%" ); list = q.list(); assertTrue( list.size()==1 ); q = s.createQuery("select bar from Bar bar order by ((bar.x - :valueX)*(bar.x - :valueX))"); q.setInteger( "valueX", bar.getX() + 1 ); list = q.list();
public Modifications getModificationsFor(final MaterialInstance materialInstance, final Pagination pagination) { String key = materialModificationsWithPaginationKey(materialInstance); String subKey = materialModificationsWithPaginationSubKey(pagination); Modifications modifications = (Modifications) goCache.get(key, subKey); if (modifications == null) { synchronized (key) { modifications = (Modifications) goCache.get(key, subKey); if (modifications == null) { List<Modification> modificationsList = (List<Modification>) getHibernateTemplate().execute((HibernateCallback) session -> { Query q = session.createQuery("FROM Modification WHERE materialId = ? ORDER BY id DESC"); q.setFirstResult(pagination.getOffset()); q.setMaxResults(pagination.getPageSize()); q.setLong(0, materialInstance.getId()); return q.list(); }); if (!modificationsList.isEmpty()) { modifications = new Modifications(modificationsList); goCache.put(key, subKey, modifications); } } } } return modifications; }
Session session = openSession(); session.beginTransaction(); for ( Object entity : session.createQuery( "from Category" ).list() ) { session.delete( entity ); query.list(); String[] str = new String[] { "WannaBeFound", "NotThere" }; query.setParameterList( "names", str ); query.uniqueResult(); query.uniqueResult(); str = new String[] { "WannaBeFound" }; query.setParameterList( "names", str ); query.uniqueResult();
public Long getTotalModificationsFor(final MaterialInstance materialInstance) { String key = materialModificationCountKey(materialInstance); Long totalCount = (Long) goCache.get(key); if (totalCount == null || totalCount == 0) { synchronized (key) { totalCount = (Long) goCache.get(key); if (totalCount == null || totalCount == 0) { totalCount = (Long) getHibernateTemplate().execute((HibernateCallback) session -> { Query q = session.createQuery("select count(*) FROM Modification WHERE materialId = ?"); q.setLong(0, materialInstance.getId()); return q.uniqueResult(); }); goCache.put(key, totalCount); } } } return totalCount; }
protected void txFragment(Session session) throws Exception { FlushMode flushMode = session.getFlushMode(); session.setFlushMode(FlushMode.COMMIT); StringBuffer sql = new StringBuffer(); sql.append("from ").append(DataProviderImpl.class.getName()).append(" as instance "); sql.append("where instance.id = :id"); Query query = session.createQuery(sql.toString()); if (id != null) query.setLong("id", id.longValue()); query.setCacheable(true); results.addAll(query.list()); session.setFlushMode(flushMode); }}.execute(); if (results.size() > 0) return (DataProviderImpl) results.get(0);
public List<Permission> getPermissionsForUser(final Long gradebookId, final String userId) throws IllegalArgumentException { if(gradebookId == null || userId == null) { throw new IllegalArgumentException("Null parameter(s) in BaseHibernateManager.getPermissionsForUser"); } final HibernateCallback<List<Permission>> hc = session -> session .createQuery("from Permission as perm where perm.gradebookId = :gradebookId and perm.userId = :userId") .setLong("gradebookId", gradebookId) .setString("userId", userId) .list(); return getHibernateTemplate().execute(hc); }
@SuppressWarnings("unchecked") public List<User> getAll() { return getSession().createQuery("from User").list(); }
/** * Call setEntity() on a cacheable query - see FORGE-265 */ public List listEventsOfOrganizer(Person organizer) { Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); Query query = session.createQuery("from Event ev where ev.organizer = :organizer"); query.setCacheable(true); query.setEntity("organizer", organizer); List result = query.list(); session.getTransaction().commit(); return result; }
s = openSession(); tx = s.beginTransaction(); Query q = s.createQuery( "from Being h where h.identity = :name1 or h.identity = :name2" ); q.setString("name1", "marc"); q.setString("name2", "steve"); final List result = q.list(); assertEquals( 2, result.size() ); s.delete( result.get(0) );
/** * @see org.openmrs.api.db.PersonDAO#getPersonByUuid(java.lang.String) */ @Override public Person getPersonByUuid(String uuid) { return (Person) sessionFactory.getCurrentSession().createQuery("from Person p where p.uuid = :uuid").setString( "uuid", uuid).uniqueResult(); }
Query q = s.createQuery("from Simple s where s.name=?"); q.setCacheRegion("foo"); q.setCacheable(true); q.setString(0, "Simple 1"); assertTrue( q.list().size()==1 ); assertTrue( q.list().size()==1 ); assertTrue( q.list().size()==1 ); q = s.createQuery("from Simple s where s.name=:name"); q.setCacheRegion("foo"); q.setCacheable(true); q.setString("name", "Simple 1"); assertTrue( q.list().size()==1 ); simple = (Simple) q.list().get(0); q.setString("name", "Simple 2"); assertTrue( q.list().size()==0 ); assertTrue( q.list().size()==0 ); q = s.createQuery("from Simple s where s.name=?"); q.setCacheRegion("foo"); q.setCacheable(true); q.setString(0, "Simple 1"); assertTrue( q.list().size()==0 );