• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA how to auto scan for entities?

 
Marcos Vidolin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys

I'm developing a Spring MVC + Hibernate/JPA project and that is my persistence.xml:



Note that I must to declare my entity User (and others ones). I'd like just indicate the entities package like a scan.
How can I do?

Thanks
[]'s
 
Saif Asif
Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Marcus

Note that I must to declare my entity User (and others ones). I'd like just indicate the entities package like a scan.
How can I do?


If you mean that you want your entities to be scanned in from some Java class rather than mentioning each entity in the persistence.xml file, then I suggest you take a look at org.hibernate.cfg.Configuration#addAnnotatedClass(Class annotatedClass). You will have a HibernateUtil class where you initiate the sessionFactory object, so make changes something like this

 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcos

My understanding is that most application servers will automatically look for classes annotated with @Entity when a META-INF/persistence.xml file is encountered and the transaction-type is JTA.
e.g.

I believe if the transaction type is RESOURCE_LOCAL, you need to specify which classes to scan.
 
Marcos Vidolin
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I insert the follow line in my entityManagerFactory bean:




Is there any problem doing my web application with entityManagerFactory be a org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean?

[]'s
 
Tim Holloway
Saloon Keeper
Pie
Posts: 17990
47
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


There may be some lint in here. It has evolved over time and getting it precisely squeaky clean hasn't been a priority.

My DAO and persistent service objects are all annotated with transaction and @Repository annotations.
 
Jaikiran Pai
Marshal
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JPA spec explains which classes are scanned for annotations. Section 8.2.1.6 to be precise in JPA 2 spec. To summarize:


The set of managed persistence classes that are managed by a persistence unit is defined by using one or more of the following:

- Annotated managed persistence classes contained in the root of the persistence unit (unless the exclude-unlisted-classes element is specified)
- One or more object/relational mapping XML files
- One or more jar files that will be searched for classes
- An explicit list of classes


Take a look at the xsd of persistence.xml, especially the jar-file element where you can specify additional jars (that are in the classpath) for annotation scanning.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic