Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: Re[eclipselink-users] strictions not changing...

Hi,

so my codes:


public abstract class AbstractDAO<T, ID extends Serializable> implements
Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	protected Root<T> root;
	protected Class<T> persistentClass;
	protected CriteriaQuery<T> criteria;
	protected CriteriaBuilder qb;
	
	@Inject
	@Database
	protected EntityManager entityManager;

	public CriteriaQuery<T> getCriteriaBuilder() {
		if ( criteria == null ) {
			qb = entityManager.getCriteriaBuilder();
			criteria = qb.createQuery( persistentClass );
			root = criteria.from( persistentClass );
		}
		
		return criteria;
	}
	
	public void addRestriction( Expression<Boolean> exp ) {
		try  {
			getCriteriaBuilder().getRestriction().getExpressions().add(exp);	
		} catch ( NullPointerException e ) {
			getCriteriaBuilder().where(exp);
		}
	}
    
	public List<T> getResultList() {
		List<T> result = entityManager.createQuery( criteria ).getResultList();
		clear();
		return result;
	}
	
	public T getSingleResult() {
		T  result =  entityManager.createQuery( criteria ).getSingleResult();
		clear();
		return result;
	}
	
	private void clear() {
		root = null;
		criteria = null;
		qb = null;
	}
	
} 

public class UsuarioDAO extends AbstractDAO<Usuario, Integer> implements
Serializable {

	private static final long serialVersionUID = 1L;

	public UsuarioDAO() {
		persistentClass = Usuario.class;
	}
	
	public UsuarioDAO getUsuario() {
		getCriteriaBuilder().select(root);
		return this;
	}
	
	public UsuarioDAO byLogin( String login ) {
		addRestriction( qb.equal( root.get( Usuario_.login ), login ) );
		return this;
	}
	
	public UsuarioDAO byPassword( String password ) {
		addRestriction( qb.equal( root.get( Usuario_.password ), password ) );
		return this;
	}
	
}

and the way im using it:

Usuario usuario = usuarioDAO
										.getUsuario()
											.byLogin(credentials.getLogin())
											.byPassword(credentials.getPassword())
										.getSingleResult(); 

but it always add only first restriction.. and byPassword doesnt work.. and
my binding is always the first used.. for example if login is 'a' them i use
'diego' on next try i can see in log that bind is always 'a'

tks.. sry about english



christopher delahunt wrote:
> 
> Hello,
> 
> I haven't played with the criteriaBuilder as much as I should have, but it
> looks like you are getting a new Builder from the em rather than using the
> one from your query.  If so, you are adding the new restriction to an
> empty builder, while the one for your query remains exactly the same.  
> 
> How are you building and executing your query, and how are you adding the
> parameter?  Parameters should allow you to pass in new ones dynamically
> without making any changes to the query, such as:
> cq.where(emp.get("address").<String>get("city").in(qb.parameter(String.class,
> "city")));  
> Query query = em.createQuery(cq);  
> qb.parameter(String.class, "city");
> query.setParameter("city", "Ottawa");  
> List<Employee> result = query.getResultList();  
> query.setParameter("city", "Boston");
> List<Employee> result2 = query.getResultList();  
> 
> 
> Best Regards,
> Chris
> 
> ----- Original Message -----
> From: diego.coronel@xxxxxxxxx
> To: eclipselink-users@xxxxxxxxxxx
> Sent: Tuesday, December 29, 2009 10:51:07 AM GMT -05:00 US/Canada Eastern
> Subject: Re[eclipselink-users] strictions not changing...
> 
> 
> Hi, im trying to make some queries with criteriaBuilder and when i did my
> first query 
> 
> i did a query by login: 
> FINE: SELECT ID, STATUS, LOGIN, NOME, PASSWORD FROM geral.USUARIO WHERE
> (LOGIN = ?)
> 	bind => [a]
> 
> and when i change the parameter to 'diego' the same binding is showed
> FINE: SELECT ID, STATUS, LOGIN, NOME, PASSWORD FROM geral.USUARIO WHERE
> (LOGIN = ?)
> 	bind => [a]
> 
> i tried to make this:
> entityManager =
> entityManager.getEntityManagerFactory().createEntityManager()
> 			qb = entityManager.getCriteriaBuilder();
> 
> and this: 
> getCriteriaBuilder().getRestriction().getExpressions().add(exp);
> 
> so.. i dont know how to change this bind..  its always locking in my first
> try... i cant change the query anymore.
> 
> is there any way to add restrictions dinamically to criteriaQuery?
> 
> sry about english.. tks
> -- 
> View this message in context:
> http://old.nabble.com/Restrictions-not-changing...-tp26956138p26956138.html
> Sent from the EclipseLink - Users mailing list archive at Nabble.com.
> 
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
> _______________________________________________
> eclipselink-users mailing list
> eclipselink-users@xxxxxxxxxxx
> https://dev.eclipse.org/mailman/listinfo/eclipselink-users
> 
> 

-- 
View this message in context: http://old.nabble.com/Restrictions-not-changing...-tp26956138p26972476.html
Sent from the EclipseLink - Users mailing list archive at Nabble.com.



Back to the top