MongoDB updates with java driver version 3.0 - java

I'm migrating my aplications to MongoDb 3.0.2. I have no problems with inserts, finds and deletes. But,Problems with the update. Specially with the eq().
In this sentence:
coll.updateOne(eq("_id", id), new Document("$set", new Document("name", name)));
The id variable is defined ObjectId. Eclipse gives me an error:
The method eq(String, ObjectId) is undefined for the
type SystemDAO (my java class).
What am I doing wrong? I followed the examples in the Mongo java driver documents.

you need to import the static method eq from the package com.mongodb.client.model.Filters.
add this infront of your class to your other imports:
import static com.mongodb.client.model.Filters.*;
In Eclipse it should give a quick-fix to import the right package if you do a mouse over on your error. But for static imports this does not work all the time.

Related

import libraries on eclipse 2021 fails Example: org.apache.commons.dbcp.BasicDataSource

I am migrating a Java code that is running on Java 8 and I need to compile it to run on Java 17, so Just downloaded eclipse 2021-09 import the code from Git, change project facet selecting Java, add to Java Build Path the libraries.
libraries added
This is the code:
package itksoluciones.avl.lst.dboperation;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
public class PoolConexionUC {
public static DataSource dataSourceUC;
public PoolConexionUC() {
inicializaDataSourceUC();
}
private void inicializaDataSourceUC() {
BasicDataSource basicDataSourceUC = new BasicDataSource();
basicDataSourceUC.setDriverClassName(DataConexion.driverUC);
basicDataSourceUC.setUsername(DataConexion.userUC);
basicDataSourceUC.setPassword(DataConexion.passUC);
basicDataSourceUC.setUrl(DataConexion.urlUC);
basicDataSourceUC.setMaxActive(Integer.parseInt(DataConexion.maxActiveUC));
basicDataSourceUC.setMaxIdle(DataConexion.defaultMaxIdleUC);
basicDataSourceUC.setMaxWait(DataConexion.defaultMaxWaitUC);
basicDataSourceUC.setRemoveAbandoned(DataConexion.removeAbandonedUC);
basicDataSourceUC.setRemoveAbandonedTimeout(DataConexion.removeAbandonedTimeoutUC);
basicDataSourceUC.setValidationQuery(DataConexion.validationQueryUC);
basicDataSourceUC.setTestOnBorrow(DataConexion.testOnBorrowUC);
dataSourceUC = basicDataSourceUC;
}
}
This are the two errors:
Import org.apache could not be resolved
and
BasicDatasourceCore could not be resolved to a type
Errors
As you could see from the error the library seems available, also it autocompletes when typing.
Also one of the solutions suggested by eclipse is to import org.apache.commons.dbcp.BasicDataSource. (after doing it by double clicking on the suggestion first error is resolved but the second one persists).
What is also very curious is that if I copy paste the class PoolConexionCore to PoolConexionCore2. The problem is gone in PoolConexionCore2.
BUT I HAVE MANY CLASSES WITH THE SAME PROBLEM so I really appreciate any suggestion to avoid having to be dedicated to copy pasted for several days.
After doing copy paste

gremlin order by query with incr

g.V().has(label,"request_job").order().by("submit_time",incr).valueMap("submit_time")
==>[submit_time:[1330647978000]]
==>[submit_time:[1330652298000]]
When I use, order() query in Gremlin console, it is working fine. But when I try the same query in Java, I am not able to use "incr", it is showing that undefined symbol "incr".
How to use "incr" with order() in Java?
Kindly help me to resolve this problem.
It is defined in the enum Order (javadoc) which is automatically imported by the Gremlin Console. You should add the following import to your Java class:
import static org.apache.tinkerpop.gremlin.process.traversal.Order.*;

In GWT is the 2.5 interface RpcService replaced by RemoteService interface?

I'm currently going through the book GWT in Action 2nd Edition and its example code. In chapter 5 under the discussions on ClientBundle usage they have example code where there is an interface that extends com.google.gwt.rpc.client.RpcService. When I loaded this example project into my Eclipse IDE, the code shows red as the package com.google.gwt.rpc does not exist. This is most likely because I'm using GWT 2.7 and the book was written back in GWT 2.5. I attempted to look into the JavaDoc to see when it was removed, and what its replacement should be, but the only JavaDoc is for the latest, and downloads for 2.5 from the website returns no page found (404) errors. My IDE is suggesting that I change the requested interface to com.google.gwt.user.client.rpc.RemoteService but without knowing if this is the correct replacement, it seems a bit odd.
The code example they provide is as follows:
package com.manning.gwtia.ch05.client.cssresource;
import java.util.HashMap;
import java.util.List;
import com.google.gwt.rpc.client.RpcService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
#RemoteServiceRelativePath("CSSResourceService")
public interface ResourceService extends RpcService {
List<String> getThemes();
HashMap<String, String> getTheme(String name);
}
Does anyone know what the proper replacement interface for RpcService and maybe also tell me in which version it was removed?
com.google.gwt.rpc was an experiment aimed at replacing RPC from com.google.gwt.user. It didn't met expectations and was ultimately removed in 2.7. So yes, use RemoteService, like you should have actually always done.

undefined method - geoBoundingBoxQuery(String location)

I am trying to create a geoBoundingBoxQuery("pin.location") in Java for Elastic Search. Eclipse is not able to resolve imports for this method although the Elastic Search Java API documentation specifically suggests to use this method.
What class do I need to import which has this method?
https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-geo-queries.html#java-query-dsl-geo-bounding-box-query
geoBoundingBoxQuery("pin.location") is defined as a static method in class org.elasticsearch.index.query.QueryBuilders, so you can use it this way:
QueryBuilder q = QueryBuilders.geoBoundingBoxQuery("pin.location");
Elasticsearch documentation is assuming you're using a static import like:
import static org.elasticsearch.index.query.QueryBuilders.geoBoundingBoxQuery;
so you don't need qualify the method with class name:
QueryBuilder q = geoBoundingBoxQuery("pin.location");
For anyone who is having the same issue, I have found the answer.
Its the version of Elastic search java API that I am using is the issue. The documentation is for version 2.1 whereas I am using an older version. For the older version, you need to do the following:
FilterBuilders.geoBoundingBoxFilter("pin.location")

CompilationTestHelper is either not found or its access discouraged

I want to write some tests for my compiler but can't get past an error.
I'm following an example from 'Implementing DSL with Xtext and Xtend' by L. Bettini (great book btw). I've downloaded the code for the 'entities' DSL from https://github.com/LorenzoBettini/packtpub-xtext-book-examples and the tests in EntitiesGenerator.xtend work great.
If I write a test for the default DSL (MyDsl) using the same code, I've got an error:
org.eclipse.xtext.xbase.compiler.CompilationTestHelper cannot be resolved to a type.
or, if I add org.eclipse.xtext.xbase.junit (2.4.1) to the list of required plug-ins, I get
Discouraged access: The type CompilationTestHelper is not accessible due to restriction on required project org.xtext.example.myDsl.tests
I can allow access to it, but then get a runtime error anyway. If I try to add org.eclipse.xtext.xbase.lib as well, only org.eclipse.xtext.xbase.lib.source appears in the list. I don't know it that matters. In any case, adding it doesn't change anything.
What do I need to do to make it work?
I'm using Juno with Xtext 2.4.1., Java 1.7.
The content of the test class:
package org.xtext.example.myDsl.tests
import com.google.inject.Inject
import org.eclipse.xtext.junit4.InjectWith
import org.eclipse.xtext.junit4.XtextRunner
import org.eclipse.xtext.xbase.compiler.CompilationTestHelper // error here
import org.xtext.example.myDsl.MyDslInjectorProvider
import org.junit.Test
import org.junit.runner.RunWith
#RunWith(typeof(XtextRunner))
#InjectWith(typeof(MyDslInjectorProvider))
class MyDslGeneratorTest {
#Inject extension CompilationTestHelper
#Test
def void testGeneratedCode() {
'''
Hello some1!
Hello some2!
'''.assertCompilesTo(
'''some content''')
}
}
Thank you in advance!
the xtext guys mark stuff that may be changed NOT as api. this is why you get this warning.
it should work anyway. (although it is meant to be used for xbase languages only)
P.S: you have to add a dependency to jdt.core too

Categories