Java Code Examples for org.springframework.dao.support.DataAccessUtils

The following examples show how to use org.springframework.dao.support.DataAccessUtils. 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 check out the related API usage on the sidebar.
Example 1
@Override
public Object invoke(MethodInvocation mi) throws Throwable {

	// Invoke the method potentially returning a reactive type
	Object m = mi.proceed();

	PersistenceExceptionTranslator translator = getPersistenceExceptionTranslator();
	if (translator == null) {
		return m;
	} else {
		// Add the translation. Nothing will happen if no-one subscribe the reactive result.
		Function<RuntimeException, Throwable> errorMappingFunction =
			t -> t instanceof DataAccessException ? t : DataAccessUtils.translateIfNecessary(t, translator);
		if (m instanceof Mono) {
			return ((Mono<?>) m).onErrorMap(RuntimeException.class, errorMappingFunction);
		} else if (m instanceof Flux) {
			return ((Flux<?>) m).onErrorMap(RuntimeException.class, errorMappingFunction);
		} else {
			return m;
		}
	}
}
 
Example 2
Source Project: DataHubSystem   Source File: UserDao.java    License: GNU Affero General Public License v3.0 6 votes vote down vote up
public User getByName (final String name)
{
   User user = (User)DataAccessUtils.uniqueResult (
      getHibernateTemplate ().find (
      "From User u where u.username=?", name));   

   // Optimization user extraction: most of the users uses case-sensitive
   // match for the login. A Requirement of the project asked for non-case
   // sensitive match. The extraction of non-case sensitive login from
   // database requires conversions and forbid the usage of indexes, so it
   // is much more slow.
   // This Fix aims to first try the extraction of the user with exact match
   // equals operator, then if not match use the toLower conversion.
   if (user==null)
      user = (User)DataAccessUtils.uniqueResult (
         getHibernateTemplate ().find (
         "From User u where lower(u.username)=lower(?)", name));
   return user;
}
 
Example 3
/**
 * Counts collections whose the given user is authorized.
 * @param user the given user.
 * @return number of authorized collection for user.
 */
public int count (User user)
{
   String userString = "";
   // Bypass for Data Right Managers. They can see all products and
   // collections.
   if ( !cfgManager.isDataPublic () && (user != null) &&
      !user.getRoles ().contains (Role.DATA_MANAGER))
   {
      userString =
         "WHERE ('" + user.getUUID () + "' in elements(authorizedUsers) OR '" +
                  userDao.getPublicData ().getUUID () +
                  "' in elements(authorizedUsers))";
   }

   return DataAccessUtils
      .intResult (find ("select count(*) FROM Collection " + userString));
}
 
Example 4
@SuppressWarnings ("rawtypes")
private int countOpenSessions ()
{
   return DataAccessUtils.intResult (getHibernateTemplate ().execute (
      new HibernateCallback<List>()
      {
         @Override
         public List doInHibernate(Session session) 
            throws HibernateException, SQLException
         {
            String sql = 
               "SELECT count (*) FROM INFORMATION_SCHEMA.SYSTEM_SESSIONS";
            SQLQuery query = session.createSQLQuery (sql);
            return query.list ();
         }
      }));
}
 
Example 5
@SuppressWarnings ({ "unchecked", "rawtypes" })
private String getSystemByName (final String name, final int index)
{
   return DataAccessUtils.uniqueResult (getHibernateTemplate ().execute (
      new HibernateCallback<List>()
      {
         @Override
         public List doInHibernate(Session session) 
            throws HibernateException, SQLException
         {
            String sql = 
               "SELECT " + name +
               " FROM INFORMATION_SCHEMA.SYSTEM_SESSIONS" +
               " LIMIT  1 OFFSET " + index;
            SQLQuery query = session.createSQLQuery (sql);
            return query.list ();
         }
      })).toString ();
}
 
Example 6
/**
 * Returns the number of Product records whose `processed` field is `true`.
 * @param filter an optional additionnal `where` clause (without the "where" token).
 * @param collection_uuid an optional parent collection ID.
 * @return a number of rows in table `PRODUCTS`.
 */
public int count (String filter, final String collection_uuid)
{
   StringBuilder sb = new StringBuilder("select count(*) ");
   if (collection_uuid != null)
   {
         sb.append("from Collection c left outer join c.products p ");
         sb.append("where c.uuid='").append(collection_uuid).append("' and ");
   }
   else
   {
      sb.append("from Product p where ");
   }
   sb.append("p.processed=true");

   if (filter != null && !filter.isEmpty())
   {
      sb.append(" and ").append(filter);
   }

   return DataAccessUtils.intResult(find(sb.toString()));
}
 
Example 7
Source Project: spring-analysis-note   Source File: JpaTransactionManager.java    License: MIT License 5 votes vote down vote up
@Override
public void flush() {
	try {
		getEntityManagerHolder().getEntityManager().flush();
	}
	catch (RuntimeException ex) {
		throw DataAccessUtils.translateIfNecessary(ex, getJpaDialect());
	}
}
 
Example 8
Source Project: spring-analysis-note   Source File: JdbcTemplate.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper)
		throws DataAccessException {

	List<T> results = query(sql, args, argTypes, new RowMapperResultSetExtractor<>(rowMapper, 1));
	return DataAccessUtils.nullableSingleResult(results);
}
 
Example 9
Source Project: spring-analysis-note   Source File: NamedParameterJdbcTemplate.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public <T> T queryForObject(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper)
		throws DataAccessException {

	List<T> results = getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rowMapper);
	return DataAccessUtils.nullableSingleResult(results);
}
 
Example 10
Source Project: java-technology-stack   Source File: JpaTransactionManager.java    License: MIT License 5 votes vote down vote up
@Override
public void flush() {
	try {
		getEntityManagerHolder().getEntityManager().flush();
	}
	catch (RuntimeException ex) {
		throw DataAccessUtils.translateIfNecessary(ex, getJpaDialect());
	}
}
 
Example 11
Source Project: java-technology-stack   Source File: JdbcTemplate.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper)
		throws DataAccessException {

	List<T> results = query(sql, args, argTypes, new RowMapperResultSetExtractor<>(rowMapper, 1));
	return DataAccessUtils.nullableSingleResult(results);
}
 
Example 12
Source Project: java-technology-stack   Source File: NamedParameterJdbcTemplate.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public <T> T queryForObject(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper)
		throws DataAccessException {

	List<T> results = getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rowMapper);
	return DataAccessUtils.nullableSingleResult(results);
}
 
Example 13
Source Project: GyJdbc   Source File: EntityDaoImpl.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public T queryOne(Id id) throws Exception {
    String sql = "SELECT * FROM " + tableName + " WHERE " + primaryKey + " = ?";
    List<T> result = jdbcTemplate.query(sql, rowMapper, id);
    return DataAccessUtils.singleResult(result);
}
 
Example 14
Source Project: lams   Source File: JdbcTemplate.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper)
		throws DataAccessException {

	List<T> results = query(sql, args, argTypes, new RowMapperResultSetExtractor<T>(rowMapper, 1));
	return DataAccessUtils.requiredSingleResult(results);
}
 
Example 15
@Override
public <T> T queryForObject(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper)
		throws DataAccessException {

	List<T> results = getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rowMapper);
	return DataAccessUtils.requiredSingleResult(results);
}
 
Example 16
Source Project: lams   Source File: JpaTransactionManager.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void flush() {
	try {
		this.entityManagerHolder.getEntityManager().flush();
	}
	catch (RuntimeException ex) {
		throw DataAccessUtils.translateIfNecessary(ex, getJpaDialect());
	}
}
 
Example 17
@Override
public void flush() {
	try {
		this.entityManagerHolder.getEntityManager().flush();
	}
	catch (RuntimeException ex) {
		throw DataAccessUtils.translateIfNecessary(ex, getJpaDialect());
	}
}
 
Example 18
Source Project: spring4-understanding   Source File: JdbcTemplate.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public <T> T queryForObject(String sql, Object[] args, int[] argTypes, RowMapper<T> rowMapper)
		throws DataAccessException {

	List<T> results = query(sql, args, argTypes, new RowMapperResultSetExtractor<T>(rowMapper, 1));
	return DataAccessUtils.requiredSingleResult(results);
}
 
Example 19
@Override
public <T> T queryForObject(String sql, SqlParameterSource paramSource, RowMapper<T> rowMapper)
		throws DataAccessException {

	List<T> results = getJdbcOperations().query(getPreparedStatementCreator(sql, paramSource), rowMapper);
	return DataAccessUtils.requiredSingleResult(results);
}
 
Example 20
Source Project: bamboobsc   Source File: BaseDAO.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int count(String hql, Object... args) throws Exception {
	Query query=this.getCurrentSession().createQuery(hql);
	for (int position=0; args!=null && position<args.length; position++) {
		this.setQueryParams(query, Integer.toString(position), args[position]);
	}
	return DataAccessUtils.intResult(query.list());
}
 
Example 21
Source Project: bamboobsc   Source File: BaseDAO.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public int countByPK(PK pk) throws Exception {
	return DataAccessUtils.intResult(
			this.getCurrentSession().createQuery(
					" select count(*) from "+this.getPersisentName() + 
					" where " + BaseEntityUtil.getPKOneName((BaseEntity<PK>)entityClass.newInstance()) + "=?0 ")
					.setString("0", (String)pk).list());		
}
 
Example 22
Source Project: bamboobsc   Source File: BaseDAO.java    License: Apache License 2.0 5 votes vote down vote up
public int countByPK(Map<String, Object> pkMap) throws Exception {
	if (pkMap==null || pkMap.size()<1) {
		return 0;
	}		
	Query query=this.getQueryByKeyMap("select count(*)", pkMap);
	return DataAccessUtils.intResult(query.list());		
}
 
Example 23
Source Project: bamboobsc   Source File: BaseDAO.java    License: Apache License 2.0 5 votes vote down vote up
public int countByEntityUK(Map<String, Object> ukMap) throws Exception {
	if (ukMap==null || ukMap.size()<1) {
		return 0;
	}		
	Query query=this.getQueryByKeyMap("select count(*)", ukMap);
	return DataAccessUtils.intResult(query.list());
}
 
Example 24
Source Project: bamboobsc   Source File: BaseDAO.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int count(String hql, Object... args) throws Exception {
	Query query=this.getCurrentSession().createQuery(hql);
	for (int position=0; args!=null && position<args.length; position++) {
		this.setQueryParams(query, Integer.toString(position), args[position]);
	}
	return DataAccessUtils.intResult(query.list());
}
 
Example 25
Source Project: bamboobsc   Source File: BaseDAO.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public int countByPK(PK pk) throws Exception {
	return DataAccessUtils.intResult(
			this.getCurrentSession().createQuery(
					" select count(*) from "+this.getPersisentName() + 
					" where " + BaseEntityUtil.getPKOneName((BaseEntity<PK>)entityClass.newInstance()) + "=?0 ")
					.setString("0", (String)pk).list());		
}
 
Example 26
Source Project: bamboobsc   Source File: BaseDAO.java    License: Apache License 2.0 5 votes vote down vote up
public int countByPK(Map<String, Object> pkMap) throws Exception {
	if (pkMap==null || pkMap.size()<1) {
		return 0;
	}		
	Query query=this.getQueryByKeyMap("select count(*)", pkMap);
	return DataAccessUtils.intResult(query.list());		
}
 
Example 27
Source Project: bamboobsc   Source File: BaseDAO.java    License: Apache License 2.0 5 votes vote down vote up
public int countByEntityUK(Map<String, Object> ukMap) throws Exception {
	if (ukMap==null || ukMap.size()<1) {
		return 0;
	}		
	Query query=this.getQueryByKeyMap("select count(*)", ukMap);
	return DataAccessUtils.intResult(query.list());
}
 
Example 28
Source Project: jeewx   Source File: GenericBaseCommonDao.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 查询指定实体的总记录数
 * 
 * @param clazz
 * @return
 */
public int getCount(Class<T> clazz) {

	int count = DataAccessUtils.intResult(getSession().createQuery(
			"select count(*) from " + clazz.getName()).list());
	return count;
}
 
Example 29
Source Project: DataHubSystem   Source File: UserDao.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
public int countNotDeleted (String filter)
{
   return DataAccessUtils.intResult (find (
      "select count(*) FROM " + entityClass.getName () +
         " u WHERE u.deleted is false AND u.username LIKE '%" + filter +
         "%' and " + "not u.username='" +
         cfgManager.getAdministratorConfiguration ().getName () + "'" +
         " and not u.username LIKE '"+getPublicData ().getUsername ()+"' "));
}
 
Example 30
Source Project: DataHubSystem   Source File: UserDao.java    License: GNU Affero General Public License v3.0 5 votes vote down vote up
public int countForDataRight (String filter)
{
   return DataAccessUtils.intResult (find (
      "select count(*) FROM " + entityClass.getName () +
         " u WHERE u.deleted is false AND u.username LIKE '%" + filter +
         "%' and " + "not u.username='" +
         cfgManager.getAdministratorConfiguration ().getName () + "' "));
}