Question

Hopefully easy questions:

  • If I have an application that is supposed to only get data from database (it doesn't persist anything)?
  • Do I need an exact structure of db presented by set of @Entity-annotated classes?
  • Do I need Entities at all anyway?
  • Or can I just use DAO and do something like:

    ObjectFromDb ob = dao.find(someProperty);

    given that ObjectFromDb is just a regular POJO without a single JPA annotation?

I google'd it for a short while, but it seems to be too specific questions...
Thanks for any advice!

Was it helpful?

Solution

You do not need entities for querying, but they can make your life easier.

You can use regular SQL for queries.

You can also have some entities defined, but query only for a subset of their data - i.e. a projection query.

You can also use JPA constructor command with projection queries to map directly to your result objects:

List<MyClass> dtos = em.createQuery("SELECT NEW com.example.MyClass( e.name, e.data) FROM Entity e").resultList();

EDIT: With annotated entities you can use features of JPQL that are not available in SQL, e.g. path navigation. Properly annotated entities can clarify the DB mapping.

An entity does not need to map all columns of a table, you can use any subset you like, as long as it includes the id.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top