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.

Thanks guys!

Edit/Solution

So my here is my code (I used a class which 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.

1
  • you can do this by using HQL. Commented Jun 21, 2016 at 13:17

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

8 Comments

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.
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)
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 docs.oracle.com/javaee/6/tutorial/doc/gjivm.html
Yeah i've looked for this but i can't find a way to do it the same way with criteriaQuery
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. stackoverflow.com/questions/4231109/…
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.