1

Example:

List<Object[]> list = em.createQuery(
  "SELECT 'Foo', 123 FROM IrcEvent ev", Object[].class).getResultList();

What I don't like in that example:

  • How do I know the table name? Can't I specify the entity class instead?
  • How do I know the column name? jOOQ provides auto-completion by creating a DSL from the database schema.
  • There could be syntax errors everywhere.

What I basically want is something like

entityManager.deleteAll(EntityClass.class);

to delete the rows of an antire table (for example).

Lukas Eder
  • 211,314
  • 129
  • 689
  • 1,509
user1050755
  • 11,218
  • 4
  • 45
  • 56
  • HQL doesn't use table names, but entity class names. You can use "select e.foo, e.bar from " + MyEntity.class + " e". But it's less readable, and you should have unit tests for your query anyway, which would detect a name problem. named queries are also checked at deploy time. – JB Nizet Mar 09 '13 at 12:23

1 Answers1

1

JPA 2 Criteria API http://docs.oracle.com/javaee/6/tutorial/doc/gjitv.html is for creating type safe queries programmatically, but it does not support deletes

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • Is there any method to determine table names in a type-safe way? – user1050755 Mar 09 '13 at 03:58
  • @user1050755 Is there a specific reason why you are not using hibernate with spring? As far as I know your table names will be bound to real objects and if you want to truncate a table all you need to do is http://stackoverflow.com/questions/1262723/using-hibernate-hql-to-truncate-a-table – cgon Mar 10 '13 at 14:45