Spring data using Neo4j and MongoDB - mongodb

I am just about to go crazy,I just spent many hours to try work with spring-data for Neo4J,working with spring-data for MongoDB was a walk in the park compared to that.
My goals: 1) Working with spring-data to manage two data-stores Mongo,Neo4j.
(correct me if I am wrong but there is no spring-data cross data store support for these two, which mean I will use different domain entities for each store)
2) Working with Neo4J embedded graph.
3) Will have the ability to monitor the graph with some client like the web admin.
So I started with Good Relationship spring-data example
, where using :
POM
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
XML
<neo4j:config storeDirectory="data/graph.db"/>
So my first question is how can I monitor the graph In that configuration, in which client?
So I read more and I got to Neo4j Web Admin for embedded graph configuration
I followed every step tried it and boom!
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.haze.server.repository.mongo.ProfileRepository com.haze.server.services.ProfileServices.profileRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'profileRepository': FactoryBean threw exception on object creation; nested exception is java.lang.NoSuchMethodError: org.springframework.data.repository.core.RepositoryMetadata.getDomainClass()Ljava/lang/Class;
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
... 39 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'profileRepository': FactoryBean threw exception on object creation; nested exception is java.lang.NoSuchMethodError: org.springframework.data.repository.core.RepositoryMetadata.getDomainClass()Ljava/lang/Class;
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1442)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:876)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:735)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
... 41 more
Caused by: java.lang.NoSuchMethodError: org.springframework.data.repository.core.RepositoryMetadata.getDomainClass()Ljava/lang/Class;
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory.getTargetRepository(MongoRepositoryFactory.java:84)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:137)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:125)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:41)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)
... 49 more
Make long story short the only version configuration I find to get spring context to load is
<spring.data.mongo.version>1.0.4.RELEASE</spring.data.mongo.version>
<neo4j.version>1.6</neo4j.version>
<spring-data-neo4j.version>2.0.1.RELEASE</spring-data-neo4j.version>
If I am adding the below dependency like specify in the article it crashed.
<spring-data-commons-core.version>1.3.0.RELEASE</spring-data-commons-core.version>
Ok so I got it working after many hours with the neo4j embedded graph and the server wrapper in order to monitor the graph from the web admin with mongo as my primary datastore.
Kind of happy but sad cause using old version for the neo4j server wrapper (1.6 cause that is the only thing which worked) I was motivated to start working with the graph via spring-data.
So I got the most basic node entity:
#NodeEntity
public class ProfileNode {
#GraphId
private Long id;
#Indexed(unique = true)
private String pid = null;
}
Tried some basic operations:
// save node - OK
ProfileNode node = new ProfileNode();
node.setPid("44ed79b3ea8a99117aa601b16e916ddr");
ProfileNode profile = graphRepo.save(node);
// return NULL
node = graphRepo.findByPropertyValue("pid",
"44ed79b3ea8a99117aa601b16e916ddr");
// throwing exception - java.lang.UnsupportedOperationException:read only index
graphRepo.delete(profile);
Basically almost every basic operation I tried didn't worked for me.
I don't know if the problems occur because of my mishmash configurations or that I am doing something wrong In my code, can someone please help me configure my application or let me know why the most basic operation via spring data doesn't work for me?
Thanks.

Please update to 2.1.RC4 as Lasse said.
Regarding using the embedded server with SDN, it is described in the docs.
What does your repository look like?

You really should upgrade to SDN 2.1.RC4, it will be out as GA in a metter of weeks.
Secondly, here is some code to get you started: https://github.com/SpringSource/spring-data-neo4j/blob/master/spring-data-neo4j/src/test/java/org/springframework/data/neo4j/repository/DerivedFinderTests.java - you can add a test for findByPropertyValue if you are not keen on derived finders, but at least this works out of the box using just that single file, i.e. you can eliminate Spring config as a source of errors.
For cross-store: I see little point in cross-store with MongoDB, to me cross-store is all about transactions across multiple data sources. With MongoDB + Neo, I'd just build different repositories and on the application level do just enough to use them concurrently.

You have to have spring-data-mongodb-1.1.0.RC1 and spring-data-neo4j-2.1.0.RC4. Both of those have the same spring-data-commons-core dependency.
spring-data with neo4j + mongo version conflicts

Related

hibernate.hbm2ddl.auto value="update" issue with Hibernate 3.4 to 5.1 migration

I have an application presently running (without issue) on JBoss 6 that I am attempting to upgrade to run on WildFly 10.1. Much of this upgrade is going well. However, the upgrade from Hibernate 3.4 (on JBoss 6) to Hibernate 5.1 (on WildFly 10.1) is causing a few issues.
Specifically, in my persistence.xml, I include the following property.
<property name="hibernate.hbm2ddl.auto" value="update"/>
Please NOTE: I am NOT making any schema or other DB changes as part of the upgrade. Furthermore, I am pointing at the same database instance that has been successfully running under the JBoss 6/Hibernate 3.4 instance. Therefore, I am confident that inclusion of this property should have no actual work/update to do upon first run with the WildFly 10.1/Hibernate 5.1 version.
However, inclusion of this property appears to 1) erroneously determine that it needs to make updates and 2) fail to do so successfully. It results in the following stack trace:
Failed to start service jboss.persistenceunit."app.ear#PU": org.jboss.msc.service.StartException in service jboss.persistenceunit."PU": javax.persistence.PersistenceException: [PersistenceUnit: PU] Unable to build Hibernate SessionFactory
...
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: PU] Unable to build Hibernate SessionFactory
...
Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to execute schema management to JDBC target [create index company_id_index on APPROVER (COMPANY_ID)]
...
Caused by: org.postgresql.util.PSQLException: ERROR: relation "company_id_index" already exists
Again, the table and index in question already exist (as confirmed by the final error).
Is Hibernate now no longer case sensitive (COMPANY_ID_INDEX being different than company_id_index)?
If so, how can I configure it so that it is case insensitive as it used to be (Postgres defaults all of this to lower....)
TIA!
Doh! Face palm! I recently discovered that similar errors were also occurring related to hbm2ddl index creation with Hibernate 3.4/JBoss 6 as I am now experiencing with Hibernate 5.1/Wildfly 10.1; however, they were NOT preventing successful start up of the persistence module. Essentially, they were being only subtly suppressed. I'm not sure if this is an expected change related to the Hibernate versions or not, as they do prevent it's start up in Hibernate 5.1/Wildfly 10.1?
The underlying issue here turned out to be that index names must be unique across the entire schema in Postgres. So multiple entities each having a FK to a COMPANY_ID column must each have a unique name for the index. Indices are relations in Postgres (driving the unique across schema requirement).
Thank you for the suggestions and apologies for the confusion.

EclipseLink adding a Converter causes ValidationException intermittently

I am building a new application using EclipseLink for the first time.
Everything was going okay until I added an entity that uses JSR310 Instant for a timestamp column.
So I created a converter class and mapped it to the the associated field like so:
#Convert(converter = JSR310InstantTypeConverter.class)
private Instant pwdChangeCodeExpiresOn = null;
However since I added that converter the application has started throwing the following exception:
SEVERE: Servlet.service() for servlet [APIJerseyServlet] in context with path [/Sclera] threw exception [org.glassfish.jersey.server.ContainerException: java.lang.ExceptionInInitializerError] with root cause
Local Exception Stack:
Exception [EclipseLink-7351] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.ValidationException
Exception Description: The converter class [com.sclera.utils.JSR310InstantTypeConverter] specified on the mapping attribute [pwdChangeCodeExpiresOn] from the class [com.sclera.entity.Admin] was not found. Please ensure the converter class name is correct and exists with the persistence unit definition.
at org.eclipse.persistence.exceptions.ValidationException.converterClassNotFound(ValidationException.java:2317)
at org.eclipse.persistence.internal.jpa.metadata.converters.ConvertMetadata.process(ConvertMetadata.java:248)
This will start happening after a code change (when Eclipse restarts the server). I have to stop and start (and/or restart) the server manually a few times until it finally starts working again. Then it will work fine until a code change or two later when it will start throwing the exception again.
This is an enormous pain. Anyone know the cause and how to fix it?
Okay solution found. Adding the converter class to the persistence.xml file - as suggested by the error message - seems to have resolved the problem.
<persistence-unit name="example" transaction-type="RESOURCE_LOCAL">
....
<class>com.example.utils.JSR310InstantTypeConverter</class>
...
</persistence-unit>
I should have tried that earlier. The fact that is working some of the time without this made me think it wouldn't make a difference.

Injecting EJB within JAX-RS resource in JBoss 5

Although there already are quite some StackOverflow questions, blog entries, etc. on the web, I still cannot figure out a solution to the problem stated below.
Similar to this question (Injecting EJB within JAX-RS resource on JBoss7) I'd like to inject a EJB instance into a JAX-RS class. I tried with JBoss 5, JBoss 7, and WildFly 8. I either get no injection at all (field is null), or the server does not deploy (as soon as I try to combine all sorts of annotations).
Adding #Stateless to the JAX-RS makes the application server know both classes as beans. However, no injection takes place.
Is there a way to inject EJBs into a REST application? What kind of information (in addition to that contained in the question linked to above) could I provide to help?
EDIT: I created a Github project showing code that works (with Glassfish 4.0) and does not work (with JBoss 5).
https://github.com/C-Otto/beantest
Commit 4bf2f3d23f49d106a435f068ed9b30701bbedc9d works using Glassfish
4.0.
Commit 50d137674e55e1ceb512fe0029b9555ff7c2ec21 uses Jersey 1.8, which does not work.
Commit 86004b7fb6263d66bda7dd302f2d2a714ff3b939
uses Jersey 2.6, which also does not work.
EDIT2:
Running the Code which I tried on JBoss 5 on Glassfish 4.0 gives:
Exception while loading the app : CDI deployment failure:WELD-001408 Unsatisfied dependencies for type [Ref<ContainerRequest>] with qualifiers [#Default] at injection point [[BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] #Inject org.glassfish.jersey.server.internal.routing.UriRoutingContext(Ref<ContainerRequest>, ProcessingProviders)]
org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied dependencies for type [Ref<ContainerRequest>] with qualifiers [#Default] at injection point [[BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] #Inject org.glassfish.jersey.server.internal.routing.UriRoutingContext(Ref<ContainerRequest>, ProcessingProviders)]
at org.jboss.weld.bootstrap.Validator.validateInjectionPointForDeploymentProblems(Validator.java:403)
EDIT3: The crucial information might be that I'd like a solution that works on JBoss 5
If you don't want to make your JAX-RS resource an EJB too (#Stateless) and then use #EJB or #Resource to inject it, you can always go with JNDI lookup (I tend to write a "ServiceLocator" class that gets a service via its class.
A nice resource to read about the topic:
https://docs.jboss.org/author/display/AS71/Remote+EJB+invocations+via+JNDI+-+EJB+client+API+or+remote-naming+project
A sample code:
try {
// 1. Retreive the Home Interface using a JNDI Lookup
// Retrieve the initial context for JNDI. // No properties needed when local
Context context = new InitialContext();
// Retrieve the home interface using a JNDI lookup using
// the java:comp/env bean environment variable // specified in web.xml
helloHome = (HelloLocalHome) context.lookup("java:comp/env/ejb/HelloBean");
//2. Narrow the returned object to be an HelloHome object. // Since the client is local, cast it to the correct object type.
//3. Create the local Hello bean instance, return the reference
hello = (HelloLocal)helloHome.create();
} catch(NamingException e) {
} catch(CreateException e) {
}
This is not "injecting" per-se, but you don't use "new" as-well, and you let the application server give you an instance which is managed.
I hope this was useful and I'm not telling you something you already know!
EDIT:
This is an excellent example: https://docs.jboss.org/author/display/AS72/EJB+invocations+from+a+remote+client+using+JNDI
EDIT 2:
As you stated in your comment, you'd like to inject it via annotations.
If the JNDI lookup is currently working for you without problems, and
If you're using Java EE 6+ (which I'm guessing you are), you can do the following:
#EJB(lookup = "jndi-lookup-string-here")
private RemoteInterface bean;

Spring data jpa: No property flush found for type void

I am using Spring Data JPA to develop a Spring MVC app. I built a JPA repository.
public interface AccessReportRepository extends JpaRepository<AccessReport, Long> {
}
I also use Spring Data Mongo along with JPA in my project.
When I run the project, I get this error.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'lastDateController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.innolabmm.software.mongotest.springrest.ReadingService com.innolabmm.software.mongotest.springrest.LastDateController.readingService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'readingService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.innolabmm.software.mongotest.springrest.AccessReportRepository com.innolabmm.software.mongotest.springrest.ReadingService.reportRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'accessReportRepository': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property flush found for type void
Does anyone have an idea what is going on? I am ready to provide more information if this will help solve the problem. Thanks in advance.
Are you using Spring Boot?
I had the same exception thrown when trying to use JPA and Mongo together, in a Spring Boot application. I found that the Repositories were always being interpreted by both JPA and Mongo, leading to problems as my repositories specifically extend JpaRepository.
I only wanted JPA repositories generated, so added the following to the Application entry point.
#EnableAutoConfiguration(exclude={MongoRepositoriesAutoConfiguration.class})
If you are using MongoDB with JPA (my JPA db is mysql) do this:
create separete packages for domain objects and for repositories (DAOs): i.e. demo.mysql.domain, demo.mysql.dao, demo.mongo.domain, demo.mongo.dao
in your configuration class use these annotations:
#EntityScan(basePackages={"demo.mysql.domain"})
#EnableJpaRepositories(basePackages={"demo.mysql.dao"})
#EnableMongoRepositories(basePackages={"demo.mongo.dao"})
annotate your JPA entities with #Entity and put them all in demo.mysql.domain package
annotate your MongoDB entities with #Document and put them all in demo.mongo.domain package
keep all of MongoDB reporitories in demo.mongo.dao package
keep all JPA repositories in demo.mysql.dao package
all mongo repositories should extend MongoRepository (i.e. public interface TransactionRepository extends MongoRepository)
all JPA repositories should extend JpaRepository (i.e. public interface CityRepository extends JpaRepository)
This is by far the easiest and the cleanest way that I have found (after digging through docs, btw.) to have MongoDB and Mysql working in one Spring Boot app.
That is what I have tried and what works. You could probably use MongoTemplate or extend CrudRepository instead of JpaRepository, or whatever else, try it, it might work.

using an EJB 3.1 bean through a remote java stand-alone application

I have been trying to use Java EE 6 to create an Application Server based app which is to receive Job objects from a GWT Web Application and those Jobs would be pulled from a Java stand-alone application. I have been thinking that the EJB model would provide me with easy way to do remoting because my client app should be able to run on a different machine.
I am using Glassfish 3.1 and Netbeans 7.0.1 as my IDE, I have also used eclipse Java EE to reproduce same problem.
I have been facing the next difficulties:
It was not easy to find out about InitialContext, is it the only way to use the remoting? shouldn't I be able to use #EJB/#Inject for some automatic code injection from my stand alone app using some configuration file?
I have discovered some how (not found directly in documentation) that for the InitialContext mechanism to work I need to add to my Libraries: gf-client-module.jar and weld-osgi-bundle.jar
I have been receiving the next error:
Exception in thread "main" javax.ejb.EJBException: java.rmi.MarshalException: CORBA MARSHAL 1330446343 No; nested exception is:
org.omg.CORBA.MARSHAL: FINE: IOP00810007: Underflow in BufferManagerReadStream after last fragment in message vmcid: OMG minor code: 7 completed: No
at ContentCreator._JobDispatcherRemote_Wrapper.getSimpleJobForProcessing(ContentCreator/_JobDispatcherRemote_Wrapper.java)
at javaapplication1.JavaApplication1.main(JavaApplication1.java:35)
Caused by: java.rmi.MarshalException: CORBA MARSHAL 1330446343 No; nested exception is:
org.omg.CORBA.MARSHAL: FINE: IOP00810007: Underflow in BufferManagerReadStream after last fragment in message vmcid: OMG minor code: 7 completed: No
at com.sun.corba.ee.impl.javax.rmi.CORBA.Util.mapSystemException(Util.java:267)
at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:213)
at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:152)
at com.sun.corba.ee.impl.presentation.rmi.codegen.CodegenStubBase.invoke(CodegenStubBase.java:227)
at ContentCreator.__JobDispatcherRemote_Remote_DynamicStub.getSimpleJobForProcessing(ContentCreator/__JobDispatcherRemote_Remote_DynamicStub.java)
... 2 more
Caused by: org.omg.CORBA.MARSHAL: FINE: IOP00810007: Underflow in BufferManagerReadStream after last fragment in message vmcid: OMG minor code: 7 completed: No
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at com.sun.corba.ee.spi.orbutil.logex.corba.CorbaExtension.makeException(CorbaExtension.java:248)
at com.sun.corba.ee.spi.orbutil.logex.corba.CorbaExtension.makeException(CorbaExtension.java:95)
at com.sun.corba.ee.spi.orbutil.logex.WrapperGenerator.handleFullLogging(WrapperGenerator.java:387)
at com.sun.corba.ee.spi.orbutil.logex.WrapperGenerator.access$400(WrapperGenerator.java:107)
at com.sun.corba.ee.spi.orbutil.logex.WrapperGenerator$2.invoke(WrapperGenerator.java:511)
at com.sun.corba.ee.spi.orbutil.proxy.CompositeInvocationHandlerImpl.invoke(CompositeInvocationHandlerImpl.java:99)
at $Proxy24.endOfStream(Unknown Source)
at com.sun.corba.ee.impl.encoding.BufferManagerReadStream.underflow(BufferManagerReadStream.java:128)
at com.sun.corba.ee.impl.encoding.CDRInputStream_1_1.grow(CDRInputStream_1_1.java:113)
at com.sun.corba.ee.impl.encoding.CDRInputStream_1_2.alignAndCheck(CDRInputStream_1_2.java:126)
at com.sun.corba.ee.impl.encoding.CDRInputStream_1_0.read_long(CDRInputStream_1_0.java:496)
at com.sun.corba.ee.impl.encoding.CDRInputStream_1_0.readValueTag(CDRInputStream_1_0.java:1810)
at com.sun.corba.ee.impl.encoding.CDRInputStream_1_0.read_value(CDRInputStream_1_0.java:1040)
at com.sun.corba.ee.impl.encoding.CDRInputObject.read_value(CDRInputObject.java:531)
at com.sun.corba.ee.impl.presentation.rmi.DynamicMethodMarshallerImpl$14.read(DynamicMethodMarshallerImpl.java:384)
at com.sun.corba.ee.impl.presentation.rmi.DynamicMethodMarshallerImpl.readResult(DynamicMethodMarshallerImpl.java:483)
at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:203)
... 5 more
I have been facing problems with using Java EE 6 remotely in my scenario and have found little help for those problems any assistance would be greatly appreciated.
In my case i have the same stack message "Underflow in BufferManagerReadStream after last fragment in message vmcid: OMG minor code: 7"
In my project i had a nested object which not implements the Serializable interface. Implementing the Inteface solved my problem.
Did you take care that the class of the object that you pass as argument while rmi-invoking the method getSimpleJobForProcessing implements the interface java.io.Serializable?
This was one reason, why I had such exceptions, too.
The #EJB annotation is processed and interpreted by a Java EE compliant container, so as far as I know there is no simple way to do this from a Java application that doesn't run in a container.
However, if you are running from a Glassfish container, you might be able to get rid of the JNDI lookup.
You can try the following:
#EJB(name = "RemoteService")
private StatusService remoteService;
and then in your sun-web.xml:
<ejb-ref>
<ejb-ref-name>RemoteService</ejb-ref-name>
<jndi-name>java:comp/env/com//RemoteService</jndi-name>
</ejb-ref>
With the exception, I cannot help.