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

The following examples show how to use org.hibernate.Session#getNamedQuery() . 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 check out the related API usage on the sidebar.
Example 1
Source File: TypeManagerImpl.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * @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
Source File: TypeManagerImpl.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * @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
Source File: KickstartFactory.java    From spacewalk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 4
Source File: ErrataFactory.java    From spacewalk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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
Source File: KickstartFactory.java    From spacewalk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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 6
Source File: UserDAOImpl.java    From Building-Web-Apps-with-Spring-5-and-Angular with MIT License 5 votes vote down vote up
@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 7
Source File: KickstartFactory.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 8
Source File: DoctorDAOImpl.java    From Building-Web-Apps-with-Spring-5-and-Angular with MIT License 5 votes vote down vote up
@Override
public int findAllCount() {
	Session session = this.sessionFactory.getCurrentSession();
       TypedQuery<Number> query = session.getNamedQuery("findAllCount"); 
       int count = ((Number)query.getSingleResult()).intValue();
       return count;
}
 
Example 9
Source File: TransactionalTest.java    From redisson with 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 10
Source File: DoctorDAOImpl.java    From Building-Web-Apps-with-Spring-5-and-Angular with MIT License 5 votes vote down vote up
@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 11
Source File: WebSessionFactory.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 12
Source File: HibernateFactory.java    From spacewalk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 13
Source File: ActionFactory.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
Source File: ConfigurationFactory.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 15
Source File: TypeManagerImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * @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 16
Source File: ReadWriteTest.java    From redisson with 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 17
Source File: AreaManagerImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
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 18
Source File: AreaManagerImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
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 19
Source File: WebSessionFactory.java    From uyuni with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 20
Source File: NamedQueryPerformanceTest.java    From high-performance-java-persistence with Apache License 2.0 4 votes vote down vote up
@Override
protected Query getEntityQuery2(EntityManager entityManager) {
    Session session = entityManager.unwrap(Session.class);
    return session.getNamedQuery(QUERY_NAME_2);
}