Repository navigation
Entity.builder().set(String, long) doesn't exist? #36
Description
Activity
- addedtype: questionRequest for information or clarification. Not an issue.Request for information or clarification. Not an issue.api: datastoreIssues related to the Datastore API.Issues related to the Datastore API.
on Mar 19, 2015 You can put this in EntityTest and see that it works (I just did).
Entity entity = Entity.builder(KEY1) .set("name", "John Doe") .set("age", 30) // I also tried 30 the int value, which failed similarly. .set("updated", false) .build();
what java version are you using? Also, might be related to the error above it...
Can you provide the whole file?Java version:
$ java -version openjdk version "1.7.0-google-v6" OpenJDK Runtime Environment (build 1.7.0-google-v6-86819614-77620724) OpenJDK 64-Bit Server VM (build 24.76-b04, mixed mode)The whole file is:
package org.geewax.hellodatastore; import com.google.gcloud.datastore.DatastoreService; import com.google.gcloud.datastore.DatastoreServiceFactory; import com.google.gcloud.datastore.DatastoreServiceOptions; import com.google.gcloud.datastore.Entity; import com.google.gcloud.datastore.Key; import com.google.gcloud.datastore.KeyFactory; public class HelloDatastore { private static final String DATASET = "gcloud-datastore-demo"; public static void main(String[] args) { DatastoreServiceOptions options = DatastoreServiceOptions.builder().dataset(DATASET).build(); DatastoreService datastore = DatastoreServiceFactory.getDefault(options); KeyFactory keyFactory = (KeyFactory) (datastore.newKeyFactory()).kind("Person"); Key key = keyFactory.newKey("Jimmy"); System.out.println("Trying to get the entity by its key!"); Entity entity = datastore.get(key); if (entity == null) { System.out.println("Entity not found! Creating it!"); entity = Entity.builder(key) .set("age", 30L) .build(); } } }
What I'm seeing from my end is that chaining things together seems to break...
So for example, the following compiles without any issues:
Entity.Builder builder = Entity.builder(key); builder.set("age", 30L); builder.set("updated", false);//.set("name", "John Doe"); entity = builder.build();
But the following fails:
Entity.Builder builder = Entity.builder(key); builder.set("age", 30L); builder.set("updated", false).set("name", "John Doe"); entity = builder.build();
The trace is:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project hello-datastore: Compilation failure [ERROR] /usr/local/google/home/jjg/gcdjavasample/hello-datastore/src/main/java/org/geewax/hellodatastore/HelloDatastore.java:[29,35] error: no suitable method found for set(String,String) [ERROR] -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project hello-datastore: Compilation failure /usr/local/google/home/jjg/gcdjavasample/hello-datastore/src/main/java/org/geewax/hellodatastore/HelloDatastore.java:[29,35] error: no suitable method found for set(String,String) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59) at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156) at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537) at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196) at org.apache.maven.cli.MavenCli.main(MavenCli.java:141) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289) at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415) at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356) Caused by: org.apache.maven.plugin.CompilationFailureException: Compilation failure /usr/local/google/home/jjg/gcdjavasample/hello-datastore/src/main/java/org/geewax/hellodatastore/HelloDatastore.java:[29,35] error: no suitable method found for set(String,String) at org.apache.maven.plugin.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:516) at org.apache.maven.plugin.CompilerMojo.execute(CompilerMojo.java:114) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209) ... 19 moreHowever, with some casting, this compiles without issue:
Entity.Builder builder = Entity.builder(key); builder.set("age", 30L); ((Entity.Builder) builder.set("updated", false)).set("name", "John Doe"); entity = builder.build();
Seems almost like
.set()is returning something that is almost the right type, but needs to be explicitly cast to the type (Entity.Builder)...
Similarly, with the
KeyFactory, I need to do some magical casting to make this all work. For example:The following fails
KeyFactory keyFactory = datastore.newKeyFactory().kind("Person")
However the following compiles without issue
KeyFactory keyFactory = (KeyFactory) (datastore.newKeyFactory()).kind("Person")
Is there some magical thing that makes type casting "just work" ? Or am I just doing something really really stupid.... ?
Taking this down to the simplest example I can construct....
This compiles:
package org.geewax.hellodatastore; import com.google.gcloud.datastore.Entity; import com.google.gcloud.datastore.Key; public class HelloDatastore { public static void main(String[] args) { Key key = Key.builder("dataset", "Person", "Jim").build(); Entity.Builder builder = Entity.builder(key); System.out.println(builder.set("f", 30L)); } }
When run, it outputs
[INFO] --- exec-maven-plugin:1.3.2:java (default-cli) @ hello-datastore --- [WARNING] Warning: killAfter is now deprecated. Do you need it ? Please comment on MEXEC-6. com.google.gcloud.datastore.Entity$Builder@79089b11
This fails to compile (the only addition is
.set("name", "Jim")chained onto things, which should be possible give the previous object was anEntity.Builderaccording to the prior output.package org.geewax.hellodatastore; import com.google.gcloud.datastore.Entity; import com.google.gcloud.datastore.Key; public class HelloDatastore { public static void main(String[] args) { Key key = Key.builder("dataset", "Person", "Jim").build(); Entity.Builder builder = Entity.builder(key); System.out.println(builder.set("f", 30L).set("name", "Jim")); } }
With the following trace:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project hello-datastore: Compilation failure [ERROR] /usr/local/google/home/jjg/gcdjavasample/hello-datastore/src/main/java/org/geewax/hellodatastore/HelloDatastore.java:[19,44] error: no suitable method found for set(String,String) [ERROR] -> [Help 1] org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project hello-datastore: Compilation failure /usr/local/google/home/jjg/gcdjavasample/hello-datastore/src/main/java/org/geewax/hellodatastore/HelloDatastore.java:[19,44] error: no suitable method found for set(String,String) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:213) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84) at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59) at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183) at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161) at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320) at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156) at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537) at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196) at org.apache.maven.cli.MavenCli.main(MavenCli.java:141) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289) at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229) at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415) at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356) Caused by: org.apache.maven.plugin.CompilationFailureException: Compilation failure /usr/local/google/home/jjg/gcdjavasample/hello-datastore/src/main/java/org/geewax/hellodatastore/HelloDatastore.java:[19,44] error: no suitable method found for set(String,String) at org.apache.maven.plugin.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:516) at org.apache.maven.plugin.CompilerMojo.execute(CompilerMojo.java:114) at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101) at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209) ... 19 moreOK I think I found the problem.
BaseEntity.Builderis a package-private class. So the tests would work (as they're in the same package) but something from another package (like anyone's project....) won't work :(Can we make
BaseEntity.Builderpublic?I don't think this is a problem in this case.
Any public method M in a packaged scope class B that is extend by a public class A in the same package would be visible to all via A.
However, because indeed unit-tests in Java are typically using the same package as the class they test such errors could happen (which should raise the priority for examples/integration-test...).I am still not sure what is the problem is in your case (though I start to wonder if the gcloud-java version that is being pulled and used in your example is not the version we are expecting 0.0.3)....
Running maven in verbose mode (-X) could help with that.Also, I added examples package to gcloud-java, so maybe you want to give it a try (or improve on it)...
15 remaining items
- added a commit that references this issue
on Sep 15, 2022 - added 10 commits that reference this issue
on Sep 30, 2022 - added a commit that references this issue
on Jan 22, 2026 - added a commit that references this issue
on Mar 23, 2026 - added a commit that references this issue
on Mar 30, 2026 - added a commit that references this issue
on Apr 1, 2026
I have the following code:
This fails with the following error:
If I take away the
.set("name", "John Doe")I get a different error...