Hibernate, Quick solutions, Spring Framework

How to solve: java.lang.IllegalArgumentException: Not an managed type

This is the first post of series “quick solutions”.

I have met issue mentioned in the topic in my current job. The reason occurred to be pretty simple. I have written wrong path in packagesToScan parameter of LocalContainerEntityManagerFactoryBean.

@Bean
public EntityManagerFactory entityManagerFactory() throws PropertyVetoException {
    HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
    vendorAdapter.setGenerateDdl(true);

    LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
    factory.setJpaVendorAdapter(vendorAdapter);
    factory.setPackagesToScan("misspelled.package.path.to.entities");
    factory.setDataSource(dataSource());
    factory.afterPropertiesSet();

    return factory.getObject();
}

As a side note, Hibernate forces us to have default constructor (without params) in all managed entities, but exception is different then and looks more like the one I pasted below:

org.hibernate.InstantiationException: No default constructor for (…)

Advertisements