Hello,
I have a java-configured hibernate setup, which works totally fine on my local machine during testing, but for some reason produces "UnknownEntityTypeException: Unable to locate persister" for any entity i try to fetch when deployed.
Here's how it looks like:
Hibernate Config
Code:
@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(
entityManagerFactoryRef = "entityManagerFactory",
transactionManagerRef = "transactionManager")
public class HibernateConfiguration {
private static final String PROPERTY_NAME_DATABASE_DRIVER = "org.postgresql.Driver";
private static final String PROPERTY_NAME_DATABASE_PASSWORD = "";
private static final String PROPERTY_NAME_DATABASE_URL = "jdbc:postgresql://localhost:5432/postgres";
private static final String PROPERTY_NAME_DATABASE_USERNAME = "postgres";
@Autowired
@Bean(name = "jpaDialect")
HibernateJpaDialect getDialect() {
return new HibernateJpaDialect();
}
@Autowired
@Bean(name = "jpaVendorAdapter")
HibernateJpaVendorAdapter getJpaVendorAdapter() {
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setDatabase(Database.HSQL);
vendorAdapter.setDatabasePlatform("org.hibernate.dialect.HSQLDialect");
return vendorAdapter;
}
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.show_sql", "false");
properties.put("hibernate.current_session_context_class", "thread");
properties.put("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
return properties;
}
@Autowired
@Bean(name = "dataSource")
public DataSource getPooledDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(PROPERTY_NAME_DATABASE_DRIVER);
dataSource.setUrl(PROPERTY_NAME_DATABASE_URL);
dataSource.setUsername(PROPERTY_NAME_DATABASE_USERNAME);
dataSource.setPassword(PROPERTY_NAME_DATABASE_PASSWORD);
return dataSource;
}
@Autowired
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean getEntityManagerFactory() {
LocalContainerEntityManagerFactoryBean theEntityManager = new LocalContainerEntityManagerFactoryBean();
theEntityManager.setPersistenceXmlLocation("classpath:META-INF/persistence.xml");
theEntityManager.setPersistenceUnitName("umPersistenceUnit");
theEntityManager.setDataSource(getPooledDataSource());
theEntityManager.setPackagesToScan(
"user.management.impl.repository.pojo",
);
theEntityManager.setJpaVendorAdapter(getJpaVendorAdapter());
theEntityManager.setJpaDialect(getJpaDialect());
theEntityManager.afterPropertiesSet();
return theEntityManager;
}
@Autowired
@Bean(name = "jpaDialect")
public JpaDialect getJpaDialect() {
return new HibernateJpaDialect();
}
@Autowired
@Bean(name = "transactionManager")
public JpaTransactionManager getTransactionManager(
@Qualifier("entityManagerFactory")
EntityManagerFactory emf) {
JpaTransactionManager jpaTranstactionManager = new JpaTransactionManager(emf);
jpaTranstactionManager.setDataSource(getPooledDataSource());
jpaTranstactionManager.setJpaDialect(getJpaDialect());
jpaTranstactionManager.setEntityManagerFactory(emf);
jpaTranstactionManager.afterPropertiesSet();
return jpaTranstactionManager;
}
//DAO Classes
@Autowired
@Bean(name = "userDao")
public UserDao getUserDao() {
return new UserDaoImpl();
}
}
Persistence XML
Code:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="1.0">
<persistence-unit name="umPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<!-- Make Hibernate work with old-style annotations -->
<properties>
<property name="hibernate.id.new_generator_mappings" value="false"></property>
</properties>
<!--<provider>org.hibernate.ejb.HibernatePersistence</provider>-->
</persistence-unit>
</persistence>
Spring config.xml
Code:
<context:component-scan base-package="user.management"/>
<bean class="HibernateConfiguration"/>
Dao class
Code:
@Repository("userDao")
@Transactional(propagation = Propagation.REQUIRED)
public class UserDaoImpl implements UserRepository {
@PersistenceContext(unitName = "umPersistenceUnit")
private EntityManager entityManager;
@Override
public User getUserById(@Nonnull BigInteger id) {
User user = entityManager.find(User.class, id);
return user;
}
}
User pojo
Code:
@Entity
@Table(name = "USERS")
public class NcUser implements User {
@Id
@Column(name = "USER_ID")
protected BigInteger id;
@Column(name = "USER_NAME")
protected String name;
@Column(name = "PASSWORD")
protected String password;
}
Obviously, i googled what this exception means, but it seems to be tied to SessionFactory and people are mostly fixing it by providing annotated classes to SessionFactory during configuration. As you can see from my config, i don't use SessionFactory at all, so that won't help in my case.
There is at least one other entityManager bean deployed, but my local tests show that specifying unitName to @PersistanceContext fixed it, plus debug shows that the entityManagerFactory and entityManager used in my code are both configured in runtime with the values i've provided.
What can it be that's causing this exception?