-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: [RESOLVED] "Unable to locate persister"
PostPosted: Thu Nov 10, 2016 12:32 pm 
Newbie

Joined: Tue Aug 02, 2016 7:51 am
Posts: 6
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?


Last edited by Sobakaa on Fri Nov 11, 2016 4:59 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: "Unable to locate persister" when deploying on the server
PostPosted: Fri Nov 11, 2016 4:57 am 
Newbie

Joined: Tue Aug 02, 2016 7:51 am
Posts: 6
Well, apparently, even if i don't use SessionFactory directly, it's still required.
Adding the following fixed the issue:
Code:
    @Autowired
    @Bean(name = "hibernate5AnnotatedSessionFactory")
    LocalSessionFactoryBuilder getLocalSessionFactoryBean() {
        LocalSessionFactoryBuilder localSessionFactoryBean = new LocalSessionFactoryBuilder(getPooledDataSource());
        localSessionFactoryBean.scanPackages(
                "user.management.impl.repository",
        );
        localSessionFactoryBean.addProperties(getHibernateProperties());
        localSessionFactoryBean.buildSessionFactory();

        return localSessionFactoryBean;
    }


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.