Java Code Examples for org.hibernate.Session#getNamedQuery()

The following examples show how to use org.hibernate.Session#getNamedQuery() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
/**
 * @see org.sakaiproject.service.common.type.TypeManager#getType(java.lang.String)
 */
public Type getType(final String uuid)
{
	if (log.isDebugEnabled())
	{
		log.debug("getType(String " + uuid + ")");
	}
	if (uuid == null || uuid.length() < 1)
	{
		throw new IllegalArgumentException("uuid");
	}

	final HibernateCallback hcb = new HibernateCallback()
	{
		public Object doInHibernate(Session session) throws HibernateException
		{
			Query q = session.getNamedQuery(FINDTYPEBYUUID);
			q.setString(UUID, uuid);
			q.setCacheable(cacheFindTypeByUuid);
			q.setCacheRegion(Type.class.getCanonicalName());
			return q.uniqueResult();
		}
	};
	Type type = (Type) getHibernateTemplate().execute(hcb);
	return type;
}
 
Example 2
/**
 * @see org.sakaiproject.service.common.type.TypeManager#getType(java.lang.String)
 */
public Type getType(final String uuid)
{
	if (log.isDebugEnabled())
	{
		log.debug("getType(String " + uuid + ")");
	}
	if (uuid == null || uuid.length() < 1)
	{
		throw new IllegalArgumentException("uuid");
	}

	final HibernateCallback hcb = new HibernateCallback()
	{
		public Object doInHibernate(Session session) throws HibernateException
		{
			Query q = session.getNamedQuery(FINDTYPEBYUUID);
			q.setString(UUID, uuid);
			q.setCacheable(cacheFindTypeByUuid);
			q.setCacheRegion(Type.class.getCanonicalName());
			return q.uniqueResult();
		}
	};
	Type type = (Type) getHibernateTemplate().execute(hcb);
	return type;
}
 
Example 3
/**
 * Verfies that a given kickstart tree can be used based on a channel id
 * and org id
 * @param channelId base channel
 * @param orgId org
 * @param treeId kickstart tree
 * @return true if it can, false otherwise
 */
public static boolean verifyTreeAssignment(Long channelId, Long orgId, Long treeId) {
    Session session = null;
    boolean retval = false;
    if (channelId != null && orgId != null && treeId != null) {
        session = HibernateFactory.getSession();
        Query query = session.
                getNamedQuery("KickstartableTree.verifyTreeAssignment");
        query.setLong("channel_id", channelId.longValue());
        query.setLong("org_id", orgId.longValue());
        query.setLong("tree_id", treeId.longValue());
        Object tree = query.uniqueResult();
        retval = (tree != null);
    }
    return retval;
}
 
Example 4
/**
 * Lookup ErrataFiles by errata and file type
 * @param errataId errata id
 * @param fileType file type label
 * @return list of ErrataFile instances
 */
public static List lookupErrataFilesByErrataAndFileType(Long errataId,
        String fileType) {
    Session session = null;
    List retval = null;
    try {
        session = HibernateFactory.getSession();
        Query q = session.getNamedQuery("PublishedErrataFile.listByErrataAndFileType");
        q.setLong("errata_id", errataId.longValue());
        q.setString("file_type", fileType.toUpperCase());
        retval =  q.list();

        if (retval == null) {
            q = session.getNamedQuery("UnpublishedErrataFile.listByErrataAndFileType");
            q.setLong("errata_id", errataId.longValue());
            q.setString("file_type", fileType.toUpperCase());
            retval =  q.list();
        }
    }
    catch (HibernateException e) {
        throw new HibernateRuntimeException(e.getMessage(), e);
    }
    return retval;


}
 
Example 5
/**
 * Lookup KickstartableTree by tree id and org id
 * @param treeId desired tree
 * @param org owning org
 * @return KickstartableTree if found, otherwise null
 */
public static KickstartableTree lookupKickstartTreeByIdAndOrg(Long treeId, Org org) {
    Session session = null;
    KickstartableTree retval = null;
    String queryName = "KickstartableTree.findByIdAndOrg";
    if (treeId != null && org != null) {
        session = HibernateFactory.getSession();
        Query query = session.getNamedQuery(queryName);
        query.setLong("org_id", org.getId().longValue());
        query.setLong("tree_id", treeId.longValue());
        //Retrieve from cache if there
        retval = (KickstartableTree)
                query.setCacheable(true).uniqueResult();
    }
    return retval;
}
 
Example 6
/**
 * Looks up a specific KickstartCommandName
 * @param commandName name of the KickstartCommandName
 * @return found instance, if any
 */
public static KickstartCommandName lookupKickstartCommandName(String commandName) {
    Session session = null;
    KickstartCommandName retval = null;
    session = HibernateFactory.getSession();
    Query query =
            session.getNamedQuery("KickstartCommandName.findByLabel");
    //Retrieve from cache if there
    query.setCacheable(true);
    query.setParameter("name", commandName);
    retval = (KickstartCommandName) query.uniqueResult();
    return retval;

}
 
Example 7
/**
 * Removes all the sessions of a user. This action is useful
 * especially when we disable/deactivate a user. We donot want
 * a deactivated user's sessions to be alive..
 * @param user the user whose sessions are to be purged.
 */
public static void purgeUserSessions(User user) {
    Session session = HibernateFactory.getSession();
    Query query = session.getNamedQuery("WebSession.deleteByUserId");
    query.setParameter("user_id", user.getId());
    query.executeUpdate();
}
 
Example 8
public Area getAreaByType(final String typeId) {
  final String currentUser = getCurrentUser();
  log.debug("getAreaByType executing for current user: " + currentUser);
  HibernateCallback hcb = new HibernateCallback() {
      public Object doInHibernate(Session session) throws HibernateException {
          Query q = session.getNamedQuery(QUERY_AREA_BY_TYPE);              
          q.setParameter("typeId", typeId, StringType.INSTANCE);
          return q.uniqueResult();
      }
  };        
  return (Area) getHibernateTemplate().execute(hcb);
}
 
Example 9
public Area getAreaByType(final String typeId) {
  final String currentUser = getCurrentUser();
  log.debug("getAreaByType executing for current user: " + currentUser);
  HibernateCallback hcb = new HibernateCallback() {
      public Object doInHibernate(Session session) throws HibernateException {
          Query q = session.getNamedQuery(QUERY_AREA_BY_TYPE);              
          q.setParameter("typeId", typeId, StringType.INSTANCE);
          return q.uniqueResult();
      }
  };        
  return (Area) getHibernateTemplate().execute(hcb);
}
 
Example 10
Source Project: redisson   File: ReadWriteTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testQuery() {
    Statistics stats = sessionFactory().getStatistics();

    Session s = openSession();
    s.beginTransaction();
    ItemReadWrite item = new ItemReadWrite("data");
    item.getEntries().addAll(Arrays.asList("a", "b", "c"));
    s.save(item);
    s.flush();
    s.getTransaction().commit();
    
    s = openSession();
    s.beginTransaction();
    Query query = s.getNamedQuery("testQuery");
    query.setCacheable(true);
    query.setCacheRegion("myTestQuery");
    query.setParameter("name", "data");
    item = (ItemReadWrite) query.uniqueResult();
    s.getTransaction().commit();
    s.close();
    
    Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getPutCount());

    s = openSession();
    s.beginTransaction();
    Query query2 = s.getNamedQuery("testQuery");
    query2.setCacheable(true);
    query2.setCacheRegion("myTestQuery");
    query2.setParameter("name", "data");
    item = (ItemReadWrite) query2.uniqueResult();
    s.delete(item);
    s.getTransaction().commit();
    s.close();
    
    Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getHitCount());
    
    stats.logSummary();
    
}
 
Example 11
/**
 * @see org.sakaiproject.service.common.type.TypeManager#getType(java.lang.String, java.lang.String, java.lang.String)
 */
public Type getType(final String authority, final String domain, final String keyword)
{
	if (log.isDebugEnabled())
	{
		log.debug("getType(String " + authority + ", String " + domain + ", String " + keyword + ")");
	}
	// validation
	if (authority == null || authority.length() < 1) throw new IllegalArgumentException("authority");
	if (domain == null || domain.length() < 1) throw new IllegalArgumentException("domain");
	if (keyword == null || keyword.length() < 1) throw new IllegalArgumentException("keyword");

	final HibernateCallback hcb = new HibernateCallback()
	{
		public Object doInHibernate(Session session) throws HibernateException
		{
			Query q = session.getNamedQuery(FINDTYPEBYTUPLE);
			q.setString(AUTHORITY, authority);
			q.setString(DOMAIN, domain);
			q.setString(KEYWORD, keyword);
			q.setCacheable(cacheFindTypeByTuple);
			q.setCacheRegion(Type.class.getCanonicalName());
			return q.uniqueResult();
		}
	};
	Type type = (Type) getHibernateTemplate().execute(hcb);
	return type;
}
 
Example 12
/**
 * Finds configuration revisions for a given configuration file
 * @param cf The ConfigFile to look for.
 * @return List of configuration revisions for given configuration file.
 */
public static List lookupConfigRevisions(ConfigFile cf) {
    Session session = HibernateFactory.getSession();
    Query q = session.getNamedQuery("ConfigRevision.findByConfigFile");
    q.setParameter("cf", cf);
    return q.list();
}
 
Example 13
/**
 * Check to see if a server has a pending kickstart scheduled
 * @param serverId server
 * @return true if found, otherwise false
 */
public static boolean doesServerHaveKickstartScheduled(Long serverId) {
    Session session = HibernateFactory.getSession();
    Query query =
            session.getNamedQuery("ServerAction.findPendingKickstartsForServer");
    query.setParameter("serverId", serverId);
    query.setParameter("label", "kickstart.initiate");
    List retval = query.list();
    return (retval != null && retval.size() > 0);
}
 
Example 14
/**
 * Using a named query, find all the objects matching the criteria within.
 * Warning: This can be very expensive if the returned list is large. Use
 * only for small tables with static data
 * @param qryName Named query to use to find a list of objects.
 * @param qryParams Map of named bind parameters whose keys are Strings. The
 * map can also be null.
 * @param cacheable if we should cache the results of this query
 * @return List of objects returned by named query, or null if nothing
 * found.
 */
protected List listObjectsByNamedQuery(String qryName, Map qryParams,
        boolean cacheable) {
    Session session = null;
    List retval = null;
    session = HibernateFactory.getSession();
    Query query = session.getNamedQuery(qryName);
    query.setCacheable(cacheable);
    bindParameters(query, qryParams);
    retval = query.list();
    return retval;
}
 
Example 15
/**
 * Removes all the sessions of a user. This action is useful
 * especially when we disable/deactivate a user. We donot want
 * a deactivated user's sessions to be alive..
 * @param user the user whose sessions are to be purged.
 */
public static void purgeUserSessions(User user) {
    Session session = HibernateFactory.getSession();
    Query query = session.getNamedQuery("WebSession.deleteByUserId");
    query.setParameter("user_id", user.getId());
    query.executeUpdate();
}
 
Example 16
@Override
public Doctor findByUserId(int userId) {
	Session session = this.sessionFactory.getCurrentSession();
       TypedQuery<Doctor> query = session.getNamedQuery("findById");  
       query.setParameter("id", userId);
       List<Doctor> doctors = query.getResultList();
       return doctors.get(0);
}
 
Example 17
Source Project: redisson   File: TransactionalTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testQuery() {
    Statistics stats = sessionFactory().getStatistics();

    Session s = openSession();
    s.beginTransaction();
    ItemTransactional item = new ItemTransactional("data");
    item.getEntries().addAll(Arrays.asList("a", "b", "c"));
    s.save(item);
    s.flush();
    s.getTransaction().commit();
    
    s = openSession();
    s.beginTransaction();
    Query query = s.getNamedQuery("testQuery");
    query.setCacheable(true);
    query.setCacheRegion("myTestQuery");
    query.setParameter("name", "data");
    item = (ItemTransactional) query.uniqueResult();
    s.getTransaction().commit();
    s.close();
    
    Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getPutCount());

    s = openSession();
    s.beginTransaction();
    Query query2 = s.getNamedQuery("testQuery");
    query2.setCacheable(true);
    query2.setCacheRegion("myTestQuery");
    query2.setParameter("name", "data");
    item = (ItemTransactional) query2.uniqueResult();
    s.delete(item);
    s.getTransaction().commit();
    s.close();
    
    Assert.assertEquals(1, stats.getSecondLevelCacheStatistics("myTestQuery").getHitCount());
    
    stats.logSummary();
    
}
 
Example 18
@Override
public int findAllCount() {
	Session session = this.sessionFactory.getCurrentSession();
       TypedQuery<Number> query = session.getNamedQuery("findAllCount"); 
       int count = ((Number)query.getSingleResult()).intValue();
       return count;
}
 
Example 19
@SuppressWarnings("unchecked")
    @Override
	public List<User> findByEmail(String email) {
    	Session session = this.sessionFactory.getCurrentSession();
        TypedQuery<User> query = session.getNamedQuery("findByEmail");  
        query.setParameter("email", email);
//    	Query query = session.getNamedQuery("findByEmail");
//    	query.setString("email", email);
        return query.getResultList();
	}
 
Example 20
@Override
protected Query getEntityQuery2(EntityManager entityManager) {
    Session session = entityManager.unwrap(Session.class);
    return session.getNamedQuery(QUERY_NAME_2);
}