Java Code Examples for org.hibernate.Session.createQuery()

The following are Jave code examples for showing how to use createQuery() of the org.hibernate.Session class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: sjk   File: AppHistory4IndexDaoImpl.java   View Source Code Vote up 11 votes
/**
 * 更新 状态到数据库,仅针对 appStatus 为 1(add) 2(update) 更新索引 ,
 */
@Override
public int updateAppHistory4indeToIndexed(List<Integer> appIds) {
    String hql = "update AppHistory4Index set indexStatus=1,lastIndexTime=:lastIndexTime where (appStatus=1 or appStatus=2) and appId in (:appIds)";

    Session session = null;
    try {
        session = this.sessions.openSession();
        Query query = session.createQuery(hql);
        query.setParameterList("appIds", appIds);
        query.setTimestamp("lastIndexTime", new Date());

        return query.executeUpdate();
    } catch (Exception e) {
        logger.error("error:", e);
        return 0;
    } finally {
        if (session != null && session.isOpen()) {
            session.flush();
            session.clear();
            session.close();
        }
    }

}
 
Example 2
Project: ImageClassify   File: MarkService.java   View Source Code Vote up 7 votes
@Override
public int deleteMark(int user_id, int img_id) {
	Session session = HibernateUtils.getSession(); //生成session实例
    Transaction tx = session.beginTransaction();  //创建transaction实例 
    int temp = 0;
    try {  
    	String hql = "delete from Mark where user_id  = ? and img_id = ?"; 
    	Query query = session.createQuery(hql);
    	query.setInteger(0, user_id);
    	query.setInteger(1, img_id);
    	temp = query.executeUpdate(); 
    	tx.commit();            //提交事务  
	} catch (Exception e) {
		e.printStackTrace();
		tx.rollback();   
	}finally {
	  HibernateUtils.closeSession();  //关闭Session实例
	} 
    return temp; 
}
 
Example 3
Project: sjk   File: MarketAppDaoImpl.java   View Source Code Vote up 7 votes
@Override
public List<MarketApp> getByIds(Session session, List<Integer> ids) {
    StringBuilder queryString = new StringBuilder("from MarketApp where id in (:ids)");
    Query q = session.createQuery(queryString.toString());
    q.setParameterList("ids", ids);
    List<MarketApp> list = HibernateHelper.list(q);
    return list;
}
 
Example 4
Project: sjk   File: AppDaoImpl.java   View Source Code Vote up 6 votes
@Override
public int updateWeekDownload(Session session, int id) {
    String hql = "update App set LastWeekDelta =RealDownload - LastWeekDownload , LastWeekDownload = RealDownload where id = :id";
    Query query = session.createQuery(hql);
    query.setParameter("id", id);
    return query.executeUpdate();
}
 
Example 5
Project: sjk   File: AppDaoImpl.java   View Source Code Vote up 6 votes
@Override
public int deleteByMarketApps(Session session, List<Integer> marketAppIds) {
    String hql = "delete App where marketAppId in (:marketAppIds)";
    Query query = session.createQuery(hql);
    query.setParameterList("marketAppIds", marketAppIds);
    return query.executeUpdate();
}
 
Example 6
Project: lams   File: CredentialDAO.java   View Source Code Vote up 6 votes
/**
    * Checks whether a user can login to this workspace. The Credential must include the password.
    */
   @Override
   public boolean checkCredential(ICredentials credential, IWorkspace workspace) throws RepositoryRuntimeException {
if (log.isDebugEnabled()) {
    log.debug("Checking credential " + credential + " for workspace " + workspace);
}
if ((credential == null) || (workspace == null) || (workspace.getWorkspaceId() == null)) {
    return false;
}

Session hibernateSession = getSessionFactory().getCurrentSession();
Query query = hibernateSession.createQuery(CredentialDAO.CHECK_CREDENTIAL_WITH_WORKSPACE);
query.setString(0, credential.getName());
query.setString(1, String.valueOf(credential.getPassword()));
query.setLong(2, workspace.getWorkspaceId());

Long count = (Long) query.uniqueResult();
if (count > 2) {
    log.warn("More than one credential found for workspace " + workspace.getWorkspaceId() + " and credential "
	    + credential.getName());
}

return count > 0;
   }
 
Example 7
Project: Library   File: LibraryServicesImpl.java   View Source Code Vote up 5 votes
/**
 * Add Borrower
 */
public RestResponse addBorrower(Borrower borrower) {

	RestResponse response = new RestResponse();
	Session session = this.sessionFactory.openSession();
	Transaction transaction = (Transaction) session.beginTransaction();

	String queryString = "from Borrower where ssn=:ssn";
	Query query = session.createQuery(queryString);
	query.setString("ssn", borrower.getSsn());
	Object object = query.uniqueResult();

	if (object != null) {
		response.setError("Account already exists");
		response.setSuccess(false);

	} else {

		session.persist(borrower);
		transaction.commit();

		query = session.createQuery(queryString);
		query.setString("ssn", borrower.getSsn());
		object = query.uniqueResult();

		Borrower borrower2 = (Borrower) object;
		response.setSuccess(true);
		response.setResult(String.valueOf(borrower2.getCardId()));
	}

	session.close();

	return response;

}
 
Example 8
Project: sjk   File: AppDaoImpl.java   View Source Code Vote up 5 votes
@Override
public int updateDayDownload(Session session, int id) {
    String hql = "update App set LastDayDelta =RealDownload - LastDayDownload , LastDayDownload = RealDownload where id = :id";
    Query query = session.createQuery(hql);
    query.setParameter("id", id);
    return query.executeUpdate();
}
 
Example 9
Project: sjk   File: AppDaoImpl.java   View Source Code Vote up 5 votes
@Override
public long countAppsOfDropMarket(Session session, String marketName) {
    Query query = session.createQuery("select count(id) from App  where marketName = :marketName");
    query.setParameter("marketName", marketName);
    Object o = query.uniqueResult();
    return Long.valueOf(o.toString());
}
 
Example 10
Project: FlashBoard   File: StudyGuideDaoImpl.java   View Source Code Vote up 5 votes
/**
 * Gets the comments associated with a study guide
 * 
 * @param id for study guide
 * 
 * @return list of comments
 * 
 */
@Override
public List<CommentSG> getStudyGuideComments(int guideId) {
	List<CommentSG> comments = new ArrayList<>();
	Session session = HibernateUtil.getSession();
	Query query = null;
	StudyGuide sg = null;
	String hql = "FROM StudyGuide WHERE studyGuideId = :id";
	
	try {
		query = session.createQuery(hql);
		query.setParameter("id", guideId);
		
		sg = (StudyGuide)query.uniqueResult();
		
		comments = sg.getComments();
	}
	catch(HibernateException he) {
		he.printStackTrace();
	}
	
	if(comments.isEmpty()) {
		return null;
	}
	
	return comments;
}
 
Example 11
Project: SchTtableServer   File: KeyDaoImpl.java   View Source Code Vote up 5 votes
@Override
public boolean contains(String userKey) {
    if (userKey == null) {
        return false;
    }
    
    String hql = "SELECT COUNT(*) FROM KeyBean WHERE userKey = :userKey";
    
    Session session = sessionFactory.getCurrentSession();
    Query query = session.createQuery(hql);
    query.setString("userKey", userKey);
    
    return ((Long) query.uniqueResult()) > 0L;
}
 
Example 12
Project: djpt   File: DaoImpl.java   View Source Code Vote up 5 votes
public <T> List<T> findByHql(String hql, Object[] vals, int from_id, int len) {
	Session session = hibernateTemplate.getSessionFactory().openSession();
	org.hibernate.Transaction tx = session.beginTransaction(); 
	
	Query query = session.createQuery(hql);
	if(vals != null) {
		for(int i=0;i<vals.length;i++) {
			if(vals[i] instanceof Integer) {
				query.setInteger(i, (Integer)vals[i]);
			} else {
				query.setString(i, vals[i].toString());
			}
		}
	}
	
	if(len>0) {
		query.setFirstResult(from_id);
		query.setMaxResults(len);
	}
	
	try {
		tx.commit();
		return query.list();
	} catch (Exception e) {
		e.printStackTrace();
		tx.rollback();
		return null;
	} finally {
		session.close(); 
	}
	
}
 
Example 13
Project: sjk   File: AppDaoImpl.java   View Source Code Vote up 5 votes
@Override
public int updateIncrementDownload(Session session, int id, int delta) {
    String queryString = "update App set realDownload = realDownload + :delta , downloadrank = realDownload +deltaDownload where id = :id";
    Query q = session.createQuery(queryString);
    q.setParameter("id", id);
    q.setParameter("delta", delta);
    return q.executeUpdate();
}
 
Example 14
Project: EBwn2_ForOpenShift   File: Validate.java   View Source Code Vote up 4 votes
synchronized public static boolean isPlaceExists(String desc)
{
         int chk=0;
         sf=Logic.getSf();
         Session s = null;
         List<String> lst=new ArrayList<String> ();
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("select pl_id from Places pl where pl.pl_desc=:pldesc");
                qry.setParameter("pldesc", desc);
                lst=qry.getResultList();
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                          System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                  if (lst.isEmpty())
                  {
                        return (false);
                  }
                  else
                  {
                       return (true);
                  }
            }
            else
            {
                 return (true);
            }
}
 
Example 15
Project: maven_EWorld_OpenShift   File: Update.java   View Source Code Vote up 4 votes
synchronized public static boolean updateUserPhone(String ... vals)
{
         int chk=0;
         int nor=0;
         sf=Logic.getSf();
         Session s = null;
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("update UserDetails ud set ud.mob_no=:mobno where ud.usr_id=:uid");
                qry.setParameter("uid", vals[0]);
                qry.setParameter("mobno", vals[1]);
                nor=qry.executeUpdate();
                System.out.println("No. Of rows updated="+nor);
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                         System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                 if(nor!=0)
                 {
                      return (true);
                 }
                 else
                 {
                      return (false);
                 }
            }
            else
            {
                 return (false);
            }
}
 
Example 16
Project: EBwn2_ForOpenShift   File: Fetch.java   View Source Code Vote up 4 votes
synchronized public static List<Places> getPlaceFromLocality(String plclocality)
{
         int chk=0;
         sf=Logic.getSf();
         Session s = null;
         List<Places> pllc=new ArrayList<Places> (); 
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("from Places pl where pl.addr.locality=:plcloc");
                qry.setParameter("plcloc", plclocality);
                pllc=(List<Places>)qry.getResultList();
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                          System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                 if(pllc.isEmpty())
                 {
                     return (null);
                 }
                 else
                 {
                      return (pllc);
                 }
            }
            else
            {
                 return (null);
            }
}
 
Example 17
Project: EBwn2_ForOpenShift   File: Fetch.java   View Source Code Vote up 4 votes
synchronized public static List<Places> getPlaceFromName(String pname)
{
         int chk=0;
         sf=Logic.getSf();
         Session s = null;
         List<Places> pllc=new ArrayList<Places> (); 
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("from Places pl where pl.pname=:pnm");
                qry.setParameter("pnm", pname);
                pllc=(List<Places>)qry.getResultList();
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                          System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                 if(pllc.isEmpty())
                 {
                     return (null);
                 }
                 else
                 {
                      return (pllc);
                 }
            }
            else
            {
                 return (null);
            }
}
 
Example 18
Project: maven_EWorld_OpenShift   File: Validate.java   View Source Code Vote up 4 votes
synchronized public static boolean isUserEmailExists(String email)
{
         int chk=0;
         sf=Logic.getSf();
         Session s = null;
         List<Object> lst=new ArrayList<Object> ();
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("select usr_id from UserDetails ud where ud.email_id=:eid");
                qry.setParameter("eid", email);
                lst=qry.getResultList();
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                         System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                  if (lst.isEmpty())
                  {
                        return (false);
                  }
                  else
                  {
                       return (true);
                  }
            }
            else
            {
                 return (true);
            }
}
 
Example 19
Project: EBwn2_ForOpenShift   File: Update.java   View Source Code Vote up 4 votes
synchronized public static boolean updateRating(String ... vals)
{
         int chk=0;
         int nor=0;
         sf=Logic.getSf();
         Session s = null;
         try{
                s=sf.openSession();
                s.beginTransaction();
                Query qry=s.createQuery("update UserRatings ur set ur.rat=:rating, ur.comm=:comment where ur.rat_id=:rid");
                qry.setParameter("rid", vals[0]);
                qry.setParameter("rating", vals[1]);
                qry.setParameter("comment", vals[2]);
                nor=qry.executeUpdate();
                System.out.println("No. Of rows updated="+nor);
                s.getTransaction().commit();
            }catch (Exception e)
                 {
                         chk=-1;
                          System.out.println("HibernateException Occured!!"+e);
                         e.printStackTrace();
                 }
            finally
                 {
                         if(s!=null)
                         {
                              s.clear();
                              s.close();
                         }
                 }
            if(chk==0)
            {
                 if(nor!=0)
                 {
                      return (true);
                 }
                 else
                 {
                      return (false);
                 }
            }
            else
            {
                 return (false);
            }
}
 
Example 20
Project: Equella   File: DMLPartitioner.java   View Source Code Vote up 4 votes
public final int withSession(Session session)
{
	final String idFinderHql = buildIdFinderHql();
	final String dmlHql = buildDmlHql();

	int total = 0;

	long startId = -1;
	long endId = -1;
	while( true )
	{
		// Find the next start and end ID
		startId = endId + 1;

		Query idFinder = session.createQuery(idFinderHql);
		idFinder.setMaxResults(MAX_BATCH_SIZE);
		idFinder.setParameter("startId", startId);
		setWhereParams(idFinder);
		ScrollableResults sr = idFinder.scroll();
		try
		{
			if( sr.last() )
			{
				endId = sr.getLong(0);
			}
			else
			{
				// Nothing more to process
				return total;
			}
		}
		finally
		{
			sr.close();
		}

		// Process rows in our ID range
		Query dml = session.createQuery(dmlHql);
		dml.setParameter("startId", startId);
		dml.setParameter("endId", endId);
		setWhereParams(dml);
		setDmlParams(dml);

		total += dml.executeUpdate();
	}
}