File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes JPA how to auto scan for entities? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "JPA how to auto scan for entities?" Watch "JPA how to auto scan for entities?" New topic
Author

JPA how to auto scan for entities?

Marcos Vidolin
Greenhorn

Joined: May 10, 2011
Posts: 28
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

Joined: Aug 11, 2011
Posts: 440

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


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
James Boswell
Bartender

Joined: Nov 09, 2011
Posts: 1012
    
    5

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

Joined: May 10, 2011
Posts: 28
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

Joined: Jun 25, 2001
Posts: 16019
    
  20



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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 10067
    
163

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.

[My Blog] [JavaRanch Journal]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JPA how to auto scan for entities?