MdC
MdC

Reputation: 107

jpa hibernate, is it possible to list all rows of a table withouth specify a query?

The code I use to list all entries of a table is the following:

entityManager.createQuery("SELECT * FROM Person WHERE Cn=?", Entry.class).getResultList();

(I hope it is correct I still don't run the application).

As from title, is it possible to list entries without specifying a query ?

Upvotes: 0

Views: 265

Answers (1)

Branislav Lazic
Branislav Lazic

Reputation: 14806

Yes, if you use Criteria.

List<Person> persons = sessionFactory.getCurrentSession().createCriteria(Entry.class)
    .add(Restrictions.eq("Cn", "some value")).list();

However, there is other magic. Called Spring Data. In Spring Data that would be

public interface PersonRepository extends JpaRepository<Person, Long> {

   List<Person> findByCn(String cn);

}

Upvotes: 1

Related Questions