Dependency version issue with Spring, Spring Neo4j and Neo4j - spring

I am trying to setup a Java project which uses Spring-Neo4j and Neo4j but unable to get around with dependency issues. I am using maven for dependency management and have tried several version combinations of Spring, Spring Neo4j and Neo4j.
spring: 3.2.6.RELEASE
spring-data-neo4j: 3.0.0.RELEASE
neo4j: 2.0.1
application-context.xml file
<neo4j:config storeDirectory="data/graph.db" />
Error:
Caused by: org.neo4j.kernel.impl.storemigration.UpgradeNotAllowedByConfigurationException: Failed to start Neo4j with an older data store version. To enable automatic upgrade, please set configuration parameter "allow_store_upgrade=true"
at org.neo4j.kernel.impl.storemigration.ConfigMapUpgradeConfiguration.checkConfigurationAllowsAutomaticUpgrade(ConfigMapUpgradeConfiguration.java:39)
at org.neo4j.kernel.impl.storemigration.StoreUpgrader.attemptUpgrade(StoreUpgrader.java:71)
at org.neo4j.kernel.impl.nioneo.store.StoreFactory.tryToUpgradeStores(StoreFactory.java:144)
at org.neo4j.kernel.impl.nioneo.store.StoreFactory.newNeoStore(StoreFactory.java:119)
at org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource.start(NeoStoreXaDataSource.java:323)
at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:503)
... 64 more
I have enabled allow_store_upgrade=true in my neo4j.properties file.

Your embedded neo4j most likely doesn't pick up the neo4j file (this documentation says you need to set it manually).
Initialise your neo4j like this
<bean id="graphDatabaseService" class="org.neo4j.kernel.EmbeddedGraphDatabase"
destroy-method="shutdown">
<constructor-arg index="0" value="target/config-test"/>
<!-- optionally pass in neo4j-config parameters to the graph database
<constructor-arg index="1">
<map>
<entry key="allow_store_upgrade" value="true"/>
</map>
</constructor-arg>
-->
</bean>
<neo4j:config graphDatabaseService="graphDatabaseService"/>
Source:
http://docs.spring.io/spring-data/data-neo4j/docs/3.0.1.RELEASE/reference/html/setup.html#d0e3597

I'm trying to use spring-data-neo4j 3.1.1.RELEASE and neo4j 2.1.2 and I think this is incomplete. Indeed, at least with these versions, map is not optionnal. Moreover there is a third mandatory argument in constructor of type Dependencies.
The problem is that I don't really know what is this third parameter and moreover EmbeddedGraphDatabase and Dependecies are deprecated. Do you know which is the good way to start a webapp (with these versions) in embedded mode ?

Related

Is it connected to Hibernate or not?

Lately I found an example of Spring Boot CRUD. In the read me there is written :
This project is based on the Spring Boot project and uses these
packages :
Maven
Spring Core
Spring Data (Hibernate & MySQL)
Spring MVC (Tomcat)
Thymleaf
In the source code I do not see anything that would look like this app is somehow connected to the hibernate. Could you help me to solve this little problem? And if it is not connected to the Hibernate how can I connect CRUD like that to the Hibernate?
Thanks for your help :)
In example you've provided you're using spring-boot-starter-data-jpa which already contains predefined hibernate dependencies (see pom.xml).
How to work with SQL databases described in documentation section.
Basically you configure hibernate using application.properties using following prefix:
spring.jpa.properties.hibernate.*
for Spring boot with hibernate you can follow bellow link :-
https://github.com/netgloo/spring-boot-samples
you have to configure hibernate property and datasource property for database connection... but for example i can share some code for Spring hibernate and JPA but Spring boot with hibernate you can follow link:-
<bean id="hibernateJpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="com.amstech.mayal.entity" />
<property name="jpaDialect" ref="hibernateJpaDialect" />
<property name="jpaVendorAdapter" ref="hibernateJpaVendorAdapter" />
<property name="jpaPropertyMap">
<map>
<entry key="hibernate.connection.driver_class" value="${database.jdbc.driver.class}" />
<entry key="hibernate.connection.url" value="${database.jdbc.url}" />
<entry key="hibernate.connection.username" value="${database.user}" />
<entry key="hibernate.connection.password" value="${database.password}" />
<entry key="hibernate.dialect" value="${hibernate.dialect}" />
<entry key="show_sql" value="true" />
<entry key="eclipselink.jdbc.exclusive-connection.is-lazy"
value="true" />
</map>
</property>
</bean>
I would suggest looking at the Spring Boot Data section of the main documentation. There is a lot less configuration that is needed and you can do it fluently and leave the xml behind. JPA + Hibernate is Spring data have become highly interlinked in boot.
There are multiple ways in which spring boot interact with hibernate. In the example you shared it is picking up the db properties from application.properties file and setting up the configuration. Rest of the things it will pick from the dependency provide in pom.xml.
Yes, it is connected with the hibernate. Things you need to do apart from setting up the project is to setting up a database with some username and password. And creating a db schema.Rest of the things will be done by spring boot. Make sure your db username password matches with the application file properties.

MongoDB Batch Job Broken in Spring XD 1.2.0+

We have a batch job running in Spring XD which reads from MongoDB using the standard MongoItemReader which converts mongo records to our domain model. Up to Spring XD version 1.1.3 this worked fine, however in versions 1.2.0 and 1.2.1 the job is failing with the following error (package name shortened)
java.lang.NoClassDefFoundError: c/s/r/b/b/domain/IndexId
at c.s.r.b.b.domain.IndexId_Instantiator_hxmj4p.newInstance(Unknown Source) ~[na:na]
at
org.springframework.data.convert.BytecodeGeneratingEntityInstantiator$EntityInstantiatorAdapter.createInstance(BytecodeGeneratingEntityInstantiator.java:193) ~
[spring-data-commons-1.10.0.RELEASE.jar:na]
at org.springframework.data.convert.BytecodeGeneratingEntityInstantiator.createInstance
(BytecodeGeneratingEntityInstantiator.java:76) ~[spring-data-commons-1.10.0.RELEASE.jar:na]
at
org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:250) ~[spring-data-mongodb-1.7.0.RELEASE.jar:na]
at
org.springframework.data.mongodb.core.convert.MappingMongoConverter.read(MappingMongoConverter.java:231) ~[spring-data-mongodb-1.7.0.RELEASE.jar:na]
Looking into this I found the threads NoClassDefFoundError when making a query in spring-data-solr within a play framework application, and NoClassDefFoundError after upgrading to 1.7.0.RELEASE which suggest this is due to a change in spring-data-mongo 1.7.0 and the underlying spring-data-commons to change the default entity instantiation technique to improve performance.
Based on the suggested fix in those threads I've modified the mongo template in my job module XML definition as follows and this fixes the problem:
<bean id="mappingConverter" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg ref="dbRefResolver"/>
<constructor-arg ref="mongoMappingContext"/>
<property name="instantiators" ref="entityInstantiators" />
</bean>
<bean id="dbRefResolver" class="org.springframework.data.mongodb.core.convert.DefaultDbRefResolver">
<constructor-arg ref="mongoDbFactory"/>
</bean>
<bean id="mongoMappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext"/>
<bean id="entityInstantiators" class="org.springframework.data.convert.EntityInstantiators">
<constructor-arg value="#{T(org.springframework.data.convert.ReflectionEntityInstantiator).INSTANCE}"/>
</bean>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mongoConverter" ref="mappingConverter" />
</bean>
However this is verbose and obviously isn't an ideal fix. The problem doesn't show up in our job module integration test so I have a hunch its caused by a combination of the default entity instantiation change and the fact that when a module executes in Spring XD the domain classes will be in the module's class loader and not visible to the spring data mongo classes in XD's main class loader.
So should this be regarded as a bug in Spring XD or Spring Data Mongo? One fix might be an improvement to the Spring Data Mongo mongo:mapping-converter XML configuration to allow forcing the use of the ReflectionEntityInstantiator which would at least reduce the amount of XML needed above. Alternatively maybe Spring XD should handle this scenario automatically?
I don't think there is anything we can do from the XD side since this is a custom job. We have to rely on the spring-data-mongodb functionality.
It looks like you're running into DATACMNS-710, which is fixed in Fowler SR1 (equivalent to Spring Data MongoDB 1.7.1). You might wanna try the just released Gosling release, too.

Hazelcast with spring namespace - init the node when context is loaded

i have hazelcast instance defined using the hazelcast name space and a map in it. also using spring cache abstraction to define cacheManager.
<bean name="siteAdminPropertyPlaceHolderConfigurer"
class="org.sample.SiteAdminPropertyPlaceHolderConfigurer">
<property name="order" value="1000"/>
<!-- last one-->
</bean>
<!-- hazelcast cache manager -->
<hz:hazelcast id="instance" lazy-init="true">
<hz:config>
<hz:group name="${HAZEL_GROUP_NAME}" password="${HAZEL_GROUP_PASSWORD}"/>
<hz:network port="${HAZEL_NETWORK_PORT}" port-auto-increment="true">
<hz:join>
<hz:multicast enabled="${HAZEL_MULTICAST_ENABLED}"
multicast-group="224.2.2.3"
multicast-port="54327"/>
<hz:tcp-ip enabled="${HAZEL_TCP_ENABLED}">
<hz:members>${HAZEL_TCP_MEMBERS}</hz:members>
</hz:tcp-ip>
</hz:join>
</hz:network>
<hz:map name="oauthClientDetailsCache"
backup-count="1"
max-size="0"
eviction-percentage="30"
read-backup-data="true"
eviction-policy="NONE"
merge-policy="com.hazelcast.map.merge.PassThroughMergePolicy"/>
</hz:config>
</hz:hazelcast>
<bean id="hazelcastCacheManager" class="com.hazelcast.spring.cache.HazelcastCacheManager" lazy-init="true"
depends-on="instance">
<constructor-arg ref="instance"/>
</bean>
The problem is that ,this spring context is also used for other tools we have besides the server and that hazelcast starts listening on the port and the tool actually never exit.
i tried to disable all network join (enabled=false) and i though to enable them programatically only when the server starts. but it does not work hazelcast still starts.
i don't want to give up the spring name space as its very convenient for developers to define new maps(spring caches). also i want as little hazelcast code in there.
any idea how to achieve this ?
thanks
Shlomi
I didn't find a way to do this except telling hazecast to shutdown at the end of each tool run.
i also moved the definition above to separated XML context file so it would not be loaded by the tools (at least not all of them)
Hazelcase.shutdownAll();

Hibernate DDL database generation stopped when I use Maven

Previously, my Java web projects used Eclipse-ordinary structure, and at the start of the container (in case, Tomcat), Hibernate generated the schemes correctly.
Now I'm using Maven infrastructure. I've relocated the needed files and configured all well (I think, because all is working right: Spring is starting, Hibernate is connecting the database - when it was previously created and there's some data to fetch). I've tested all CRUD operations and it's working.
The problem is that Hibernate refuses to generate the schemes (DDL) as it did when over Eclipse-ordinary infrastructure.
Additional information:
My persistence.xml is almost empty (as always) because Spring applicationContext.xml is starting it. I have not changed the file, it continues the same way as before.
<!-- Location: src/main/resources/META-INF/persistence.xml -->
<persistence>
<persistence-unit name="jpa-persistence-unit" transaction-type="RESOURCE_LOCAL"/>
</persistence>
Part of the Spring configuration goes here (applicationContext.xml):
<!-- Location: src/main/webapp/WEB-INF/applicationContext.xml -->
<!-- ... -->
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="database" value="[DATABASE-NAME]" />
<property name="showSql" value="true" />
<property name="generateDdl" value="true" /> <!-- THIS CONFIGURATION WORKED PREVIOUSLY, NOW WITH MAVEN, IT'S IGNORED -->
<property name="databasePlatform" value="[DIALECT]" />
</bean>
<!-- ... -->
I'm not using any Maven Hibernate plugin, because I just want the default behavior that occurred earlier.
Did Maven invalidate this "generateDdl" property!? Why!? What should I do!? I can't find any solution.
I found out the solution.
Maven has any fault about that.
Hibernate was not able to create my database because the "DIALECT" was wrong.
I remembered that I changed the dialect from MySQL to MySQL-InnoDB. Hibernate was logging this problem but I couldn't see it because the slf4j-simple dependency was not explicity imported.
Thank you for your time, Shawn.

Struts tiles to Tiles migration - Changes in Spring support classes

This situation arose when I tried to upgrade Spring from version 2.1 to Spring 3.0.x in my web application. Spring 3.0.x doesn't support struts-tiles 1.3, it requires Apache Tiles 2.x. So, I also have to upgrade Struts-Tiles 1.3 to Apache Tiles 2.x. Apache tiles has this migration guide that helped me with this effort. However, I seem to have hit a wall on this which is not mentioned in the migration guide.Here are the details:
This is the tilesConfigurer we were using,
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles.TilesConfigurer">
<property name="factoryClass">
<value>org.apache.struts.tiles.xmlDefinition.I18nFactorySet</value>
</property>
<property name="definitions">
<list>
<value>/WEB-INF/tiles-defs.xml</value>
</list>
</property>
</bean>
Since, the class tiles.TilesConfigurer is deprecated in Spring 3.0.x, I changed it to use org.springframework.web.servlet.view.tiles2.TilesConfigurer
The tiles2.TilesConfigurer does not have a setFactoryClass(..) method unlike tiles.TilesConfigurer which is now deprecated. And hence my bean initialization fails.
I have looked up the tiles2.TilesConfigurer api, which now has the methods, setDefinitionsFactoryClass(..) and setPreparerFactoryClass(..). Not only I am unable to decide which one is relevant here, I can't find an equivalent class for org.apache.struts.tiles.xmlDefinition.I18nFactorySet. Is there something of this sort directly available in Tiles 2.2, or do I need to revisit some of my existing code with an equivalent that is available in Tiles 2.2?
Any pointer will be appreciated.
A gentleman from the Tiles Community gave the solution. You may find the thread in Apache Tiles user list May 2012 archive. Cross posting it here:
The factoryClass org.apache.struts.tiles.xmlDefinition.I18nFactorySet is supported out of the box in Tiles 2.2. So, we don't need to inject anything special to the new tiles2.TilesConfigurer class. The final XML element will look like this
<bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles2.TilesConfigurer">
<property name="definitions">
<list>
<value>/WEB-INF/tiles-defs.xml</value>
</list>
</property>
</bean>
So, removing the property does the trick.

Resources