-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: findByExample, Example dont works
PostPosted: Sat Apr 16, 2011 12:26 pm 
Newbie

Joined: Sat Apr 16, 2011 12:13 pm
Posts: 2
hello all, i have a problem i hope that u can help me:

i have thi code:
i am trying to findByExample but dont works, always list is empty even if there are matches, but if i remove the example then give me all results what happend ??
and not show any error.
Code:
public class CrudServiceImpl<T> extends JpaDaoSupport implements CrudService<T> {

    @PersistenceUnit
    private EntityManagerFactory entityManagerFactory;
    private EntityManager entityManager;
    private EntityTransaction tx;
    private Session session;

    //ME TOCA CREARLO SIEMPRE PORQUE NO LO ESTA INYECTANDO T_T
    public void makeEntity() {
        entityManager = entityManagerFactory.createEntityManager();
        tx = entityManager.getTransaction();
        session = (Session) entityManager.getDelegate();
    }

    public <T> List<T> findByExample(final T entity) throws RuntimeException {
        makeEntity();
        List<T> lista = null;
        Example example = null;
        try {
            example = Example.create(entity).ignoreCase().enableLike(MatchMode.ANYWHERE);
            Criteria crit =  session.createCriteria(entity.getClass());
            crit.add(example);
            lista=crit.list();
        } catch (Exception e) {
            System.out.println("ERROR BY EXAMPLE: ");
            e.printStackTrace();
            throw new RuntimeException("Error al realizar la consulta "+e.getMessage());
        } finally {
            return lista;
        }
    }
}


Top
 Profile  
 
 Post subject: Re: findByExample, Example dont works
PostPosted: Mon Apr 18, 2011 1:56 am 
Beginner
Beginner

Joined: Mon Apr 04, 2011 3:31 am
Posts: 41
Hi

Check this

http://docs.jboss.org/hibernate/core/3. ... teria.html

_________________
Thanks
Niki


Top
 Profile  
 
 Post subject: Re: findByExample, Example dont works
PostPosted: Mon Apr 18, 2011 11:37 am 
Newbie

Joined: Sat Apr 16, 2011 12:13 pm
Posts: 2
well i use this:

example = Example.create(entity).ignoreCase().enableLike(MatchMode.ANYWHERE);
Criteria crit = session.createCriteria(entity.getClass());
crit.add(example);
lista=crit.list();

like here: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querycriteria.html -> 15.6. Example queries

Cat cat = new Cat();
cat.setSex('F');
cat.setColor(Color.BLACK);
List results = session.createCriteria(Cat.class)
.add( Example.create(cat) )
.list();

is the same but i am using generics, if i don't use example, cirt.list() returns all from database table, but if i use example don't returns anything even if matches with some register from database.

i dont know what to do i am using jpa spring and zk


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.