-->
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.  [ 5 posts ] 
Author Message
 Post subject: Spring Data with Hibernate JPA provider and MongoDB
PostPosted: Tue Sep 29, 2015 7:06 am 
Beginner
Beginner

Joined: Mon Jul 27, 2015 4:03 am
Posts: 29
I am trying to use Spring Data + Hibernate JPA provider + MongoDB. (Although Spring Data has a component specific to mongodb, I prefer to use this way because of various factors eg. L2 cache etc ). I am encountering the following exception when Spring is trying to instantiate the entity manager factory.

Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Could not instantiate named strategy class [org.hibernate.ogm.datastore.mongodb.MongoDBDialect]
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveDefaultableStrategy(StrategySelectorImpl.java:162) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:136) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.constructDialect(DialectFactoryImpl.java:78) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.buildDialect(DialectFactoryImpl.java:68) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:209) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:857) ~[hibernate-entitymanager-4.3.9.Final.jar:4.3.9.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850) ~[hibernate-entitymanager-4.3.9.Final.jar:4.3.9.Final]
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:425) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:849) ~[hibernate-entitymanager-4.3.9.Final.jar:4.3.9.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343) ~[spring-orm-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318) ~[spring-orm-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1633) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1570) ~[spring-beans-4.1.7.RELEASE.jar:4.1.7.RELEASE]
... 66 common frames omitted
Caused by: java.lang.InstantiationException: org.hibernate.ogm.datastore.mongodb.MongoDBDialect
at java.lang.Class.newInstance(Class.java:427) ~[na:1.8.0_45]
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveDefaultableStrategy(StrategySelectorImpl.java:159) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]
... 84 common frames omitted
Caused by: java.lang.NoSuchMethodException: org.hibernate.ogm.datastore.mongodb.MongoDBDialect.<init>()
at java.lang.Class.getConstructor0(Class.java:3082) ~[na:1.8.0_45]
at java.lang.Class.newInstance(Class.java:412) ~[na:1.8.0_45]
... 85 common frames omitted

-----------------------------------------------------------------------------------------------------------------------------------------

Seems to be coming from -

158 try {
159 return implementationClass.newInstance();
160 }
161 catch (Exception e) {
162 throw new StrategySelectionException(
163 String.format( "Could not instantiate named strategy class [%s]", implementationClass.getName() ),
164 e
165 );
166 }

of org.hibernate.boot.registry.selector.internal.StrategySelectorImpl , where the hibernate.dialect property is set to "org.hibernate.ogm.dialect.mongodb.MongoDBDialect". MongoDBDialect does not have a default constructor.

If I do not specify the hibernate.dialect property, I get another exception -
Caused by: org.hibernate.HibernateException: Access to DialectResolutionInfo cannot be null when 'hibernate.dialect' not set
at org.hibernate.engine.jdbc.dialect.internal.DialectFactoryImpl.determineDialect(DialectFactoryImpl.java:104) ~[hibernate-core-4.3.11.Final.jar:4.3.11.Final]

-----------------------------------------------------------------------------------------------------------------------------------------------------------

I have tried both xml configuration and Java configuration for Spring :

@Configuration
@EnableJpaRepositories("com.olp.jpa")
@EnableTransactionManagement
@ComponentScan("com.olp")
@EnableLoadTimeWeaving
public class AppContextConfig {

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory(Environment env) {

PersistenceTypes type = PersistenceTypes.getType("productHub"); // my code

LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();

factoryBean.setBeanName("entityManagerFactory");
factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
factoryBean.setPersistenceXmlLocation("classpath:META-INF/persistence.xml");
factoryBean.setPersistenceUnitName("productHub");
factoryBean.setJpaPropertyMap(type.getProperties()); // returns all hibernate and mongodb properties.

return(factoryBean);
}

Persistence.xml --

<?xml version="1.0" encoding="utf-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0">
<persistence-unit name="productHub" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ogm.jpa.HibernateOgmPersistence</provider>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<!-- properties are externalized
<properties>
<property name="hibernate.ogm.datastore.provider" value="mongodb" />
<property name="hibernate.ogm.datastore.database" value="test" />
<property name="hibernate.ogm.datastore.host" value="localhost" />
<property name="hibernate.ogm.datastore.username" value="xxxx" />
<property name="hibernate.ogm.datastore.password" value="xxxx" />
</properties>
-->
</persistence-unit>
</persistence>

---------------------------------------------

Is there anything obvious which I am missing here ? Appreciate some inputs.


Top
 Profile  
 
 Post subject: Re: Spring Data with Hibernate JPA provider and MongoDB
PostPosted: Wed Sep 30, 2015 3:14 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hi Rajesh,

I can't spot it in your listing, but it seems you set the property "hibernate.dialect" to the FQN of MongoDBDialect. That property is not supported with Hibernate OGM, though. Instead set the provider to HibernateOgmPersistence (as you have), and then specify the following property (no need for FQN, short name "mongodb" suffices):

Code:
<property name="hibernate.ogm.datastore.provider" value="mongodb" />


Check out http://docs.jboss.org/hibernate/ogm/4.2/reference/en-US/html_single/#ogm-configuration-optionsogm-configuration-options for the properties known from ORM which should *not* be used with OGM.

Hth,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Spring Data with Hibernate JPA provider and MongoDB
PostPosted: Thu Oct 01, 2015 6:14 am 
Beginner
Beginner

Joined: Mon Jul 27, 2015 4:03 am
Posts: 29
Thanks Gunnar.

Infact I already tried with only the datastore provider property. It did not work. Did some further investigation.

The problem seems to be that Spring is trying to use "org.springframework.orm.jpa.vendor.SpringHibernateEjbPersistenceProvider" as persistence provider instead of the configured one i.e. "org.hibernate.ogm.jpa.HibernateOgmPersistence" . The dialect was misleading. Spring is expecting its own dialect i.e. "org.springframework.orm.jpa.vendor.HibernateJpaDialect". The class "org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" in my listing, forces both the persistence provider & spring dialect.

Probably need to raise the question in Spring forum. If you have any other pointer, let me know.

Thanks,
Rajesh


Top
 Profile  
 
 Post subject: Re: Spring Data with Hibernate JPA provider and MongoDB
PostPosted: Fri Oct 02, 2015 2:57 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Ah, I see. An idea then may be to write your own variant of HibernateJpaVendorAdapter which bootstraps Hibernate OGM instead of ORM. Still would be nice to have "official" support for Hibernate OGM on the Spring side.

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Spring Data with Hibernate JPA provider and MongoDB
PostPosted: Mon Oct 05, 2015 6:31 am 
Beginner
Beginner

Joined: Mon Jul 27, 2015 4:03 am
Posts: 29
Yes Gunnar. I tried the same thing. Wrote my implementation of JpaVendorAdapter and sourced it in the entity manager factory bean. However Spring data query processor complained saying the provider does not support JPQL --

"nested exception is org.springframework.data.repository.query.QueryCreationException: Could not create query for public abstract <<my repository>>(java.lang.String,org.springframework.data.domain.Pageable)!

Reason: Your persistence provider does not support extracting the JPQL query from a named query thus you can't use Pageable inside your query method. Make sure you have a JpaDialect configured at your EntityManagerFactoryBean as this affects discovering the concrete persistence provider."

I needed the pagination support. So I couldn't use Spring data jpa in it's original form. Had to resort to a kind of traditional DAO pattern for now, until Spring forum gives me some solution.

The other problem I faced is with multiple persistence units. Apparently there is no easy way to associate a specific persistence unit with a repository. There is a way using <jpa:repository base-package="<< package containing a set of repositories needing a specific persistence unit >>" entity-manager-factory-ref="entityManagerFactoryDocType"/> . This also is throwing an error as some spring code is always looking for "entityManagerFactory" as the bean name. The question is open in the Spring forum at this moment.

Thanks,
Rajesh


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.