Spring data/mongoDB - NoSuchMethodError: com.mongodb.DBRef - mongodb

I use this dependency in my project:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
and with this dependency tho mongoDB diver artefacts are in my project:
mongodb-driver-3.4.1.jar and
mongodb-driver.core-3.4.1.jar
both do have a package com.mongodb.
I get the following exception:
nested exception is java.lang.NoSuchMethodError: com.mongodb.DBRef.<init>(Lcom/mongodb/DB;Ljava/lang/String;Ljava/lang/Object;)V
Does anyone know what I'm doing wrong?
This is my collection:
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;
#Document(collection = "MyCollection")
public class MyCollection {
#Id
private String id;
#DBRef
private User user;
}

Both legacy driver(2.x) and new driver(3.x) have same package (com.mongodb)
java.lang.NoSuchMethodError: com.mongodb.DBRef.<init>(Lcom/mongodb/DB;Ljava/lang/String;Ljava/lang/Object;)V
https://github.com/mongodb/mongo-java-driver/blob/2.14.x/src/main/com/mongodb/DBRef.java#L67
Notice the first argument is DB which is constructor in 2.x driver and is removed from 3.x version.
So it looks like your project is still referencing 2.x driver version.

Related

Eclipse JBoss Tools EL Autocomplete not working with Jakarta API

I just did fresh install of Eclipse & JBoss Tools but can't get the EL autocomplete to work. I tracked it down to the use of Jakarta API. If I use the older JavaEE API, the EL autocomplete works as expected, but nothing is displayed when using the Jakarta API in the pom.
Eclipse Version 2022-12 (4.26.0) using vm: JDK 17.0.4.1
JBoss Tools Version 4.26.0 Final (Installed from Marketplace)
Example using Jakarta
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>9.1.0</version>
<scope>provided</scope>
</dependency>
.
package test;
import java.io.Serializable;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
#Named
#ViewScoped
public class JakartaNamedObject implements Serializable {
private static final long serialVersionUID = 1L;
//getters and setter
}
Example using Javax
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>8.0</version>
<scope>provided</scope>
</dependency>
.
package test;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import java.io.Serializable;
#Named
#ViewScoped
public class JavaxNamedObject implements Serializable {
private static final long serialVersionUID = 1L;
//getters and setter
}
I used the Eclipse installer to install Eclipse 2022-12, with a brand new workspace and then Eclipse Marketplace to install JBoss Tools 4.26.
I guess the question is does EL autocomplete work with Jakarta and if so why is it not working in my fresh new setup?

Error while creating building a hibernate app without Maven

I am learning to hibernate so I made a small app for crud operation using mySQL as database. However, I am getting some errors and I cannot find the solution anywhere. SDK 17.0.2, I am not using maven , Also all hibernate final jar files have been added
my class:
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.luv2code.hibernate.demo.entity.Student;
public class CreateStudentDemo {
public static void main(String[] args) {
// Create session factory
SessionFactory factory= new Configuration()
.configure("hibernate.cfg.xml")
.addAnnotatedClass(Student.class)
.buildSessionFactory();
//Create session
Session session = factory.getCurrentSession();
try {
//Create a Student object
System.out.println("Creating new student");
Student tempStudent = new Student("Pau;", "Wall", "paul#luv2code.com");
//Start a transaction
session.beginTransaction();
//save the student object
session.save(tempStudent);
//commit transaction
session.getTransaction().commit();
}
finally {
factory.close();
}
}
}
runtime error :
java.security.PrivilegedActionException: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain)
Caused by: java.lang.NoSuchMethodException: sun.misc.Unsafe.defineClass(java.lang.String,[B,int,int,java.lang.ClassLoader,java.security.ProtectionDomain)
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.reflect.Method.invoke(Object, Object[])" because "com.sun.xml.bind.v2.runtime.reflect.opt.Injector.defineClass" is null
java removed java.xml.bind from JAVA 9 and higher editions.
You need to add these jar files manually to your library
basically you can to that in this way.
If you use Intellij this is the easyest way:
jaxb-impl-2.3.0.jar
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.0</version>
</dependency>
jaxb-core-2.3.0.jar
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
jaxb-api-2.3.1.jar
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
javax.activation-api-1.2.0.jar
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
<version>1.2.0</version>
</dependency>
If you use intellij here is a step my step to get these files through IDE
go to project structure:
Libraries, and click + sign
Chick to from Maven:
Paste these dependencies to the search.
It will automatically find exact jar files to download, Kist Paste it and click to the enter that is it.
Also later you can just add these files to the classpath just in case:
java removed java.xml.bind from JAVA 9 and higher editions.
Here is the eclipse solution:
In Eclipse IDE:
Download these jar files and paste them inside lib folder:
Go the project properties: The last line
Done these steps:
Happy Coding! :)

Test is not an annotation type

in my maven project, i have added the testng dependency as below:
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>test</scope>
</dependency>
But, then when i write a simple test as below, it cannot find #Test :
public class Test{
#Test
public void test() {
}
}
it complains with Test is not an annotation type
i tried to add the test manually by the below line:
import org.testng.annotations.Test;
but again it complains with:
The import org.testng.annotations.Test conflicts with a type defined in the same file
I have Forced Update my project by maven several times but still no success.
It is not working because your class name also the "Test" so, you have to change the class name then only #Test annotation will work.
Remove scope tag from respective dependency in pom.xml. It shall work.

Spring #DataNeo4jTest with Procedure Support

I'm writing Spring Data Neo4J repository tests with #DataNeo4jTest and all is well until I write a test against a custom query that uses a procedure, for example apoc.coll.intersection. The error declares procedure apoc.coll.intersection is unknown. I have the APOC JAR on the classpath so am guessing I need to find a way to register the procedure with the embedded datasource/driver that #DataNeo4jTest uses.
Any help would be appreciated. Thanks.
Some background to understand the situation: The #DataNeo4jTest annotation provides you the Spring Boot based auto configuration. It will pick up your Neo4j connection configuration in your application.properties (either test or production if no test properties are defined) and create Neo4j-OGM's SessionFactory with the matching configuration for you.
There are two ways to solve you problem:
Define the SessionFactory bean by yourself with embedded instance setup and configuration:
#Bean
public SessionFactory sessionFactory() {
GraphDatabaseService graphDatabaseService = new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder(Paths.get("pathToDb").toFile()).newGraphDatabase();
registerProcedure(graphDatabaseService, MyProcedure.class);
EmbeddedDriver driver = new EmbeddedDriver(graphDatabaseService);
SessionFactory sessionFactory = new SessionFactory(driver, "package");
}
Or during "runtime" with the already existing SessionFactory bean e.g. in your test setup (make sure to do this just once)
EmbeddedDriver loadedDriver = (EmbeddedDriver) sessionFactory.getDriver();
registerProcedure(loadedDriver.getGraphDatabaseService(), MyProcedure.class);
both will call a method like this
public static void registerProcedure(GraphDatabaseService db, Class<?>...procedures) throws KernelException {
Procedures proceduresService = ((GraphDatabaseAPI) db).getDependencyResolver().resolveDependency(Procedures.class);
for (Class<?> procedure : procedures) {
proceduresService.registerProcedure(procedure,true);
proceduresService.registerFunction(procedure, true);
proceduresService.registerAggregationFunction(procedure, true);
}
}
Update: Added example and version definitions.
GraphDatabaseService graphDatabaseService = new GraphDatabaseFactory()
.newEmbeddedDatabaseBuilder(Paths.get("path/to/db").toFile()).newGraphDatabase();
// Option I
registerProcedure(graphDatabaseService, MyProcedure.class);
EmbeddedDriver driver = new EmbeddedDriver(graphDatabaseService);
SessionFactory sessionFactory = new SessionFactory(driver, "org.neo4j.ogmindex.domain");
// Option II if embedded driver is not directly accessible anymore
EmbeddedDriver loadedDriver = (EmbeddedDriver) sessionFactory.getDriver();
// register the apoc version function
registerProcedure(loadedDriver.getGraphDatabaseService(), Version.class);
// Test call to apoc.version
Session session = sessionFactory.openSession();
session.query("RETURN apoc.version()", emptyMap())
.forEach(System.out::println); // outputs {apoc.version()=3.4.0.2}
pom.xml definition for the example above:
<dependency>
<groupId>org.neo4j.test</groupId>
<artifactId>neo4j-harness-enterprise</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>org.neo4j.procedure</groupId>
<artifactId>apoc</artifactId>
<version>3.4.0.2</version>
</dependency>
After messing around with this for quite some time, trying different dependency versions and also playing with configuration code as suggested by #meistermeier I found a solution, which was to simply use the correct version of the 2 Neo4J test JARs I was referencing. This is a Spring Boot project so here are all the Neo4J dependencies in my Maven POM that solve the issue:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-neo4j</artifactId>
</dependency>
<dependency>
<groupId>org.neo4j.test</groupId>
<artifactId>neo4j-harness</artifactId>
<version>${neo4j.ogm.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-ogm-embedded-driver</artifactId>
<version>${neo4j.ogm.version}</version>
<scope>test</scope>
</dependency>
I set neo4j.ogm.version to match the version specified in the spring-data-neo4j-parent POM (which is brought in transitively).

How to run Neo4j with OSM and Neo4jSpatial?

Hello i'm new in neo4j and i would like to use OSM + Neo4j Spatial.
I have a maven project and my Neo4j version is 2.3.0-M01
I have a simple code just for importing an OSM file but it displays some errors in the import files: GraphDatabaseService, EmbeddedGraphDatabase and BatchInserter.
package testOSM;
import java.nio.charset.Charset;
import org.neo4j.gis.spatial.osm.OSMImporter;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.kernel.EmbeddedGraphDatabase;
import org.neo4j.kernel.impl.batchinsert.BatchInserter;
public class TestOsm {
private static final String DB_PATH = "/community/data/graph.db";
public static void main(final String[] args){
OSMImporter importer = new OSMImporter("clz_map.osm");
importer.setCharset(Charset.forName("UTF-8"));
BatchInserter batchInserter = BatchInserter.inserter(DB_PATH);
try{
importer.importFile(batchInserter, "clz_map.osm", false);
GraphDatabaseService db = new EmbeddedGraphDatabase(DB_PATH);
importer.reIndex(db);
db.shutdown();
}
catch(Exception e){
System.out.println(e.getMessage());
}
batchInserter.shutdown();
}
}
May be my problem is with the versions, because i'm using Neo4j 2.3-M01, but i don't know exactly how should i set the versions e.g. here
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-graph-collections</artifactId>
<version>0.7.1-neo4j-2.0.2-SNAPSHOT</version>
<type>jar</type>
</dependency>
My pom.xml is based on https://github.com/neo4j-contrib/spatial/blob/master/pom.xml
Plus:
<repository>
<id>neo4j</id>
<url>http://m2.neo4j.org/content/repositories/releases/</url>
<releases>
<enabled>true</enabled>
</releases>
</repository>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j</artifactId>
<version>2.3.0-M01</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-kernel</artifactId>
<version>2.3.0-M01</version>
</dependency>
You can have a look into my git repository
https://github.com/amhg/OSM
Thank you in advance!
For anyone else that runs into this, here is how to do it: https://github.com/maxdemarzi/OSM
Notice a small differences between 2.2.x and 2.3 (7/30-currently on M2).
Just needed the right dependencies.
there are API changes since the last released version. Looking at https://github.com/neo4j-contrib/spatial/blob/master/pom.xml#L4 , it seems you can use Neo4j 2.2.3 if you build that project yourself with
mvn install
and then include version 0.15-neo4j-2.2.3 of the spatial plugin into your pom.xml from the local mvn repo.
I took a look at your pom.xml and it looks like you copied the pom.xml from Neo4j Spatial. This is not what you want.
Since you are trying to write a new application that uses Neo4j Spatial, you should have a pom that is new and refers to neo4j-spatial as a dependency, not a pom that is in any way similar to the neo4j-spatial pom. There is a section in the README that describes how to add neo4j-spatial as a dependency to your own pom.
So I would suggest you do the following:
create a new pom.xml - https://maven.apache.org/guides/getting-started/index.html#How_do_I_make_my_first_Maven_project
then add the dependency as described in https://github.com/neo4j-contrib/spatial#using-neo4j-spatial-in-your-java-project-with-maven