Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6977193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:35:40+00:00 2026-05-27T17:35:40+00:00

With Hibernate you can load your Entity classes as: sessionFactory = new AnnotationConfiguration() .addPackage(test.animals)

  • 0

With Hibernate you can load your Entity classes as:

sessionFactory = new AnnotationConfiguration()
                    .addPackage("test.animals")
                    .addAnnotatedClass(Flight.class)
                    .addAnnotatedClass(Sky.class)
                    .addAnnotatedClass(Person.class)
                    .addAnnotatedClass(Dog.class);

Is there a way to do the same thing – programmatically loading your Entity classes – in a JPA 2.0 compliant way?

The reason for this question is because I’d like to dynamically load my Entity classes, thus not necessarily programmatically.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:35:41+00:00Added an answer on May 27, 2026 at 5:35 pm

    With the help of Spring I did this in a JPA compliant way.

    My “persistence.xml” looks empty, with no entities listed within the <persistence-unit> element.

    I then wrote a class that implemented PersistenceUnitPostProcessor like so:

    import java.util.Set;
    import javax.persistence.Entity;
    import javax.persistence.MappedSuperclass;
    import org.reflections.Reflections;
    import org.reflections.scanners.TypeAnnotationsScanner;
    import org.springframework.orm.jpa.persistenceunit.MutablePersistenceUnitInfo;
    import org.springframework.orm.jpa.persistenceunit.PersistenceUnitPostProcessor;
    
    public class ReflectionsPersistenceUnitPostProcessor implements PersistenceUnitPostProcessor {
    
        private String reflectionsRoot;
        private Logger log = LoggerFactory.getLogger(ReflectionsPersistenceUnitPostProcessor.class);
    
        @Override
        public void postProcessPersistenceUnitInfo(MutablePersistenceUnitInfo pui) {
                Reflections r = new Reflections(this.reflectionsRoot, new TypeAnnotationsScanner());
                Set<String> entityClasses = r.getStore().getTypesAnnotatedWith(Entity.class.getName());
                Set<String> mappedSuperClasses = r.getStore().getTypesAnnotatedWith(MappedSuperclass.class.getName());
    
                for (String clzz : mappedSuperClasses)
                {
                        pui.addManagedClassName(clzz);
                }
    
    
                for (String clzz : entityClasses)
                {
                        pui.addManagedClassName(clzz);
                }
    
        }
    
        public String getReflectionsRoot() {
                return reflectionsRoot;
        }
    
        public void setReflectionsRoot(String reflectionsRoot) {
                this.reflectionsRoot = reflectionsRoot;
        }
    }
    

    Then I adjusted my spring context xml like this:

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
                <property name="dataSource" ref="dataSource" />
                <property name="jpaVendorAdapter">
                        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                                <property name="showSql" value="false" />
                                <property name="generateDdl" value="true" />
                                <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
                        </bean>
                </property>
                <property name="persistenceUnitName" value="GenericPersistenceUnit"/>
                <property name="persistenceXmlLocation" value="classpath:META-INF/persistence.xml"/>
                <property name="persistenceUnitPostProcessors">
                        <list>
                                <bean class="com.austinmichael.core.repository.ReflectionsPersistenceUnitPostProcessor">
                                        <property name="reflectionsRoot" value="com.austinmichael"/>
                                </bean>
                        </list>
                </property>
        </bean>
    

    Note the registration of the ReflectionsPersistenceUnitPostProcessor in the persistenceUnitPostProcessors setting.

    And that’s it. Every class with a JPA Entity or MappedSuperclass annotation on the classpath is added to the classpath. I had to give reflections the prefix of a package name to scan through which is why com.austinmichael is there at all. You could register a second ReflectionsPersistenceUnitPostProcessor with a different package name prefix if you want if your entities don’t share a common package name prefix.

    But, this is now JPAVendor agnostic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just example of xml file will be enough) OR Can Hibernate load needed entity(all
How can hibernate can access a private field/method of a java class , for
You can also write hibernate hql or criteria queries. I have Teacher entity and
How can I update an entity effectively in hibernate just as by using SQL.
With Hibernate, how do I load an entity using generics? Curretly I am doing:
How can I configure hibernate event listeners post-insert, post-delete, post-load, post-update in java configuration?
I just started using Hibernate on a new project, and with my first entity,
Can Hibernate 3.5.x be used as the JPA provider instead of the default provider
I want fetch last record using hibernate criteria Can u help me?
Can I configure hibernate properties to connect without using an instance name to sql

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.