Hibernate "Not a managed type" Error

549 views
Skip to first unread message

Keiron O'Shea

unread,
Nov 15, 2024, 7:31:58 AM11/15/24
to HAPI FHIR
Hi everyone,

I'm encountering an issue with the hapi-fhir-jpa-starter with Hibernate entity management, and I'm hoping someone might be able to help.

When I run my application, I receive the following error:

Caused by: java.lang.IllegalArgumentException: Not a managed type: class wales.nhs.ctmuhb.hapiserver.entities.AuditEventRow

This error occurs during the init phase when Hibernate is setting up the JPA repositories.

Here's my AuditEventRow entity:

package wales.nhs.ctmuhb.hapiserver.entities;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import jakarta.persistence.Column;
import java.util.UUID;

@Entity
@Table(name = "AU_EVENTS")
public class AuditEventRow {
@Id
@Column(name = "request_id", nullable = false, unique = true)
private UUID requestId;

// Default constructor
public AuditEventRow() {}

// Getters and Setters
public UUID getRequestId() {
return requestId;
}

public void setRequestId(UUID requestId) {
this.requestId = requestId;
}
}

Repository Interface:

package wales.nhs.ctmuhb.hapiserver.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
import wales.nhs.ctmuhb.hapiserver.entities.AuditEventRow;
import java.util.UUID;

public interface IAuditEventRowRepository extends JpaRepository<AuditEventRow, UUID> {
}

Application Configuration:

In my Application.java:

@ServletComponentScan(basePackageClasses = {RestfulServer.class, SmartServlet.class})
@SpringBootApplication(
scanBasePackages = {
"ca.uhn.fhir.jpa.starter",
"wales.nhs.ctmuhb.hapiserver"
},
exclude = {
ElasticsearchRestClientAutoConfiguration.class,
ThymeleafAutoConfiguration.class
}
)
@EntityScan(basePackages = "wales.nhs.ctmuhb.hapiserver.entities")
@EnableJpaRepositories("wales.nhs.ctmuhb.hapiserver.repositories")
@Import({
StarterCrR4Config.class,
StarterCrDstu3Config.class,
StarterCdsHooksConfig.class,
SubscriptionSubmitterConfig.class,
SubscriptionProcessorConfig.class,
SubscriptionChannelConfig.class,
WebsocketDispatcherConfig.class,
MdmConfig.class,
JpaBatch2Config.class,
Batch2JobsConfig.class
})
public class Application extends SpringBootServletInitializer {

Steps Taken to Debug:

  1. Checked Package Scanning:

    • Confirmed that @EntityScan and @EnableJpaRepositories point to the correct packages.
  2. Cleaned and Rebuilt the Project:

    • Ran mvn clean install to clean the build and rebuild all artifacts.
  3. Verified Managed Entities:

    • Added a CommandLineRunner to list all managed entities at startup:

      java
      Copy code
      @Bean public CommandLineRunner listManagedEntities(EntityManager entityManager) { return args -> { Set<EntityType<?>> entities = entityManager.getMetamodel().getEntities(); System.out.println("Managed Entities:"); entities.forEach(entity -> System.out.println(entity.getName())); }; }
    • Observed that AuditEventRow is not listed among the managed entities

Observations:

  • It seems that AuditEventRow is not being picked up during the entity scanning phase, despite the configurations.
  • The error persists even after ensuring that annotations and dependencies are compatible with Spring Boot 3.x.

Has anyone experienced a similar issue or have insights on how to resolve this?

Any suggestions on steps I might have missed or alternative approaches to debug this problem?

Thank you in advance for your help!

Best wishes,

Keiron O'Shea

Keiron O'Shea

unread,
Nov 15, 2024, 11:15:19 AM11/15/24
to HAPI FHIR
Quick update, so we worked out that the LocalContainerEntityManagerFactoryBean needed a bit of modification to get it to work.

Example here:

@Primary
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(
DataSource myDataSource,
ConfigurableListableBeanFactory myConfigurableListableBeanFactory,
FhirContext theFhirContext,
JpaStorageSettings theStorageSettings) {

LocalContainerEntityManagerFactoryBean retVal =
HapiEntityManagerFactoryUtil.newEntityManagerFactory(myConfigurableListableBeanFactory, theFhirContext, theStorageSettings);
retVal.setPersistenceUnitName("HAPI_PU");

try {
retVal.setDataSource(myDataSource);
} catch (Exception e) {
throw new ConfigurationException("Could not set the data source due to a configuration issue", e);
}

retVal.setPackagesToScan(
"ca.uhn.fhir.jpa.entity",       // HAPI FHIR default entities
"ca.uhn.fhir.jpa.model.entity",  // Include `ResourceTable` package
"xyz.keiron.fhirserver.entities" // Your custom entities
);
retVal.setJpaProperties(
EnvironmentHelper.getHibernateProperties(configurableEnvironment, myConfigurableListableBeanFactory)
);

return retVal;
}
Reply all
Reply to author
Forward
0 new messages