3

Simple question here :

If i've got an object with initialized and uninitialized values in it. Is there an easy way to find in my db all the Entities that fit this one with hibernate ? (without listing and checking every variable of the object)

Example :

I got this class :

public class User {
    private int id;
    private String name;
    private String email;
    private boolean activ;
}

I would like to be able to do that :

User user1 = new User();
user.setActive() = true;

User user2 = new User();
user.setActive(true);
user.setName("petter")

listUser1 = findAllUser(user1);
listUser2 = findAllUser(user2);

Here listUser1 will contain all the active users and listUser2 will contain all the active user that are named petter.

Thx guys !

Edit/Solution

So my here is my code (i used a class wich is similar at the one of my example). It work just fine but the problem is that according to Eclipse : "The method createCriteria(Class) from the type SharedSessionContract is deprecated"...

public static List<Personne> findAllPersonne(Personne personne) {
    List<Personne> listPersonne;

    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("testhibernate0");
    EntityManager entityManager = entityManagerFactory.createEntityManager();

    Session session = entityManager.unwrap(Session.class);

    Example personneExample = Example.create(personne);
    Criteria criteria = session.createCriteria(Personne.class).add(personneExample);

    listPersonne = criteria.list();

    entityManager.close();
    return listPersonne;
}  

So .. How could i do that in a better way? I've looked into CriteriaQuery but i can't find how to use it with an example.

Antoine Grenard
  • 1,712
  • 3
  • 21
  • 41

2 Answers2

4

Yes it exists : the key word for google is "query by exemple" or "qbe". https://dzone.com/articles/hibernate-query-example-qbe

Mr_Thorynque
  • 1,749
  • 1
  • 20
  • 31
  • Aaaah thx man ! I tried a lot of search key word but couldn't find it ! ;) Again i won't have to reinvent the wheel. – Antoine Grenard Jun 21 '16 at 13:22
  • I've tried your solution but i end up with an "The method createCriteria(Class) from the type SharedSessionContract is deprecated" from Eclipse .. (See my EDIT in the first post) – Antoine Grenard Jun 22 '16 at 08:49
  • Yes it is since last version of hibernate, it said the now we have to use JPA criteria. So you can use this deprecated method, it'll work. Even you can adapt the code with JPA criteria http://docs.oracle.com/javaee/6/tutorial/doc/gjivm.html – Mr_Thorynque Jun 22 '16 at 09:13
  • Yeah i've looked for this but i can't find a way to do it the same way with criteriaQuery – Antoine Grenard Jun 22 '16 at 09:47
  • In the article it says that it can't be use with JPA implementation, so you need to use specific hibernate method even if it is flag as deprecated. http://stackoverflow.com/questions/4231109/implementing-query-by-example-functionality-in-jpa – Mr_Thorynque Jun 22 '16 at 09:55
  • Hi @Antoine I've looking into your [question tab](http://stackoverflow.com/users/6231539/antoine?tab=questions) and you've never accepted any answer to your questions (total of 16!!!). You must know: if this or any answer has solved your question please consider [accepting it](http://meta.stackexchange.com/q/5234/179419) by clicking the check-mark. This indicates to the wider community that you've found a solution and **gives some reputation to both the answerer and yourself**. Of course there is no obligation to do this. – Jordi Castilla Jul 01 '16 at 06:36
  • @Jordi Oh woah ! I didn't knew i had to do this sorry. – Antoine Grenard Jul 01 '16 at 06:41
  • don't worry, I was looking your today's question and check that, you're new here, so it's ok more experienced users can guide newbies into this features. If you need help with something don't hesitate to ask. :) – Jordi Castilla Jul 01 '16 at 06:44
0

In general, if an entity instance is already in your Persistence context, you can find it by primary key with EntityManager.find. Otherwise, you can pick up a result from your database by way of JPQL or native querying.

For your particular use case, it sounds like a querying solution would be the best fit; use one of the linked query creation methods from your entity, then use the Query.getResultList() method to pick up a list of objects that match the query criteria.

QueryByExample is also a good and valid solution, as Mr_Thorynque indicates, but as the article he linked mentions, that functionality is specific to certain JPA providers (Hibernate among them) and not JPA provider agnostic.

Brandon McKenzie
  • 1,655
  • 11
  • 26