2

I have few JPA annotated entity classes in my sample application i am building and almost all are specified in the persistence unit to be managed by the JPA provider.

For those entity which are annotated but not included in the persistence.xml:

  1. What is the use of the entity annotation ?
  2. Do they still map to the tables in the database ?
  3. Since they are not managed by provider, how can i use those entities to persist data?

Appreciate any answers to clarify my understanding.

iCrus
  • 1,210
  • 16
  • 30

2 Answers2

4

JPA will include any class annotated with @Entity in the persistence management setup. You don't need persistence.xmlif you use annotations.

Cheers,

Anders R. Bystrup
  • 15,729
  • 10
  • 59
  • 55
  • After removing any reference to the entity classes in the persistence-unit in persistence.xml, the application hits an exception: `Exception in thread "main" java.lang.IllegalArgumentException: NamedQuery of name: Article.findByArticleid not found.` – iCrus Oct 23 '12 at 10:41
  • Sounds like you had a named query in your `persistence.xml` move that to the `@Entity` using the `@NamedQuery` annotation. In general, anything you had in `persistence.xml` should be converted to annotation-based config, if you wish to do without it. – Anders R. Bystrup Oct 23 '12 at 10:57
  • 1
    It works after adding `false` element in the `` in persistence.xml as suggested by @datanucleus – iCrus Oct 23 '12 at 10:58
1

JPA scans automatically for classes annotated with @Entity and includes them.

Petar Minchev
  • 46,889
  • 11
  • 103
  • 119