1

I am trying to get hibernate to scan package-info.java files so I can create package level annotations and not have to define annotations in every single entity. I found this post that mentioned how to do it:

Why would Hibernate ignore package-info.java?

The answers say add this:

<mapping package="com.foo.bar.thepackage">

To my hibernate config file. However, since I am only using annotations and using the LocalContainerEntityManagerFactoryBean I don't have an Hibernate XML file. How can I set this hibernate specific property using the jpa factory?

Thanks

Community
  • 1
  • 1
devo
  • 1,290
  • 1
  • 15
  • 28
  • Well, try to get a Hibernate configuration and call the method configuration.addPackage as stated in the link you provided – Dimitri Sep 17 '13 at 15:03
  • The problem is I am using LocalContainerEntityManagerFactoryBean which is JPA not hibernate specific. I don't know how to get access to the hibernate Configuration object through this JPA interface. – devo Sep 18 '13 at 15:45

3 Answers3

0

In the LocalContainerEntityManager, there is a method called setPackagesToScan. Try this method.

 LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
  factoryBean.setPackagesToScan("com.foo.bar.thepackage"); 
Dimitri
  • 8,122
  • 19
  • 71
  • 128
  • Thanks for your response. I am actually setting this in spring like so: but it is not picking it up. It is scanning all the entities underneath that package but not applying the package-info.java annotations. – devo Sep 20 '13 at 13:03
0

I observed the same problem with Hibernate 4.3.1 and Spring 4.0.2

My workaround was to move JPA config annotations from package-info.java to one of the classes.

Seems that this doesn't work because DefaultPersistenceUnitManager#entityTypeFilters gets initialized to only filter classes having following JPA annotations:

    entityTypeFilters = new LinkedHashSet<TypeFilter>(4);
    entityTypeFilters.add(new AnnotationTypeFilter(Entity.class, false));
    entityTypeFilters.add(new AnnotationTypeFilter(Embeddable.class, false));
    entityTypeFilters.add(new AnnotationTypeFilter(MappedSuperclass.class, false));
    try {
        @SuppressWarnings("unchecked")
        Class<? extends Annotation> converterAnnotation = (Class<? extends Annotation>)
                DefaultPersistenceUnitManager.class.getClassLoader().loadClass("javax.persistence.Converter");
        entityTypeFilters.add(new AnnotationTypeFilter(converterAnnotation, false));
    }
    catch (ClassNotFoundException ex) {
        // JPA 2.1 API not available
    }
Ilya Silvestrov
  • 347
  • 3
  • 7
0

Just use both, LocalContainerEntityManagerFactoryBean and a nearly empty persistence.xml and just use the same name for the persistence unit as in your LocalContainerEntityManagerFactoryBean:

META-INF/persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
         xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

    <persistence-unit name="sameAsInLocalContainerEntityManagerFactoryBean" transaction-type="RESOURCE_LOCAL">
    <!-- This is your package-info -->     
    <class>com.foo.bar.thepackage</class>        
    </persistence-unit> 
</persistence>
Michael Simons
  • 4,640
  • 1
  • 27
  • 38
  • I am trying your solution but if I have a nearly empty persistence.xml then my "packagesToScan" does not seem to work. Any ideas ? – sashok_bg Nov 10 '17 at 10:20