Unable to configure annotated classes to be mapped by Hibernate while running tests

Hi Dev’s

I have an annotated model class (Client.java) and on running tests, I get an org.hibernate.MappingException: Unknown Entity (annotated class name i.e client)

I have specified PackagesWithMappedClasses tag in Config.xml


I created TestingApplicationContext.xml which should override the sessionFactory bean of applicationContext-service.xml by default and I added the following property

   <property name="packagesToScan">

I created a Question on Ask, but haven’t been able to resolve it yet.

So I delved a but deeper and it turns out that the org.hibernate.SessionFactory instance is not being configured to read the values specified in TestingApplicationContext.xml. Any ideas what needs to be done to wire the tests to read from TestingApplicationContext.xml??

Here is how I drew the conclusion:

I debugged the test and added a breakpoint in AbstractBeanFactory class of Spring Framework. On studying the values of the variables for the bean “sessionFactory” originally defined in openmrs-core at /api/src/main/resources/ applicationContext-service.xml, I see that the variables such as packagesToScan,mappingResources,mappingLocations, annotatedClasses, annotatedPackages are always null throughout the test.

Here is a screenshot:

The String value of packagesToScan and mappingResources were [ ].

I added a test-hibernate.cfg.xml and modified the TestingApplicationContext.xml as follows

  <property name="configLocations">
        <list merge="true">

On debugging again, the configLocations variable showed only 1 value, i.e., class path resouce[hibernate.cfg.xml]

I am out of ideas currently of how to get the annonated classes to be mapped by hibernate during tests. How should I proceed? Thanks :slight_smile:

//CC @harsha89, @surangak

I only skimmed your email just now, but perhaps this pointer to what we did in the Ebola module will help:

1 Like

@darius thanks! I was able to fix this :smile:

I had to configure IntelliJ IDEA’s Project Settings for OAuth2 module to add resources in the src\test\java\resources to the classpath when running tests.

It was a pretty minor glitch but it has taught me some new stuff along the way!

@michael could you close this topic, saving you the trouble of finding this post later I guess :stuck_out_tongue:

1 Like