Sitelet https://github.com/googleapis/google-cloud-java/issues/36
Skip to content

Entity.builder().set(String, long) doesn't exist? #36

Description

@jgeewax

I have the following code:

entity = Entity.builder(key)
  .set("name", "John Doe")
  .set("age", 30L) // I also tried 30 the int value, which failed similarly.
  .set("updated", false)
  .build();

This fails with the following error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project hello-datastore: Compilation failure: Compilation failure:
[ERROR] /usr/local/google/home/jjg/gcdjavasample/hello-datastore/src/main/java/org/geewax/hellodatastore/HelloDatastore.java:[18,58] error: incompatible types
[ERROR] 
[ERROR] could not parse error message:   required: KeyFactory
[ERROR] found:    Builder
[ERROR] /usr/local/google/home/jjg/gcdjavasample/hello-datastore/src/main/java/org/geewax/hellodatastore/HelloDatastore.java:29: error: no suitable method found for set(String,long)
[ERROR] .set("age", 30L)
[ERROR] ^
[ERROR] 
[ERROR] method Builder.set(String,Blob) is not applicable
[ERROR] (actual argument long cannot be converted to Blob by method invocation conversion)
[ERROR] method Builder.set(String,List) is not applicable
[ERROR] (actual argument long cannot be converted to List by method invocation conversion)
[ERROR] method Builder.set(String,FullEntity) is not applicable
[ERROR] (actual argument long cannot be converted to FullEntity by method invocation conversion)
[ERROR] method Builder.set(String,Key) is not applicable
[ERROR] (actual argument long cannot be converted to Key by method invocation conversion)
[ERROR] method Builder.set(String,DateTime) is not applicable
[ERROR] (actual argument long cannot be converted to DateTime by method invocation conversion)
[ERROR] method Builder.set(String,boolean) is not applicable
[ERROR] (actual argument long cannot be converted to boolean by method invocation conversion)
[ERROR] method Builder.set(String,String) is not applicable
[ERROR] (actual argument long cannot be converted to String by method invocation conversion)
[ERROR] method Builder.set(String,Value) is not applicable
[ERROR] (actual argument long cannot be converted to Value by method invocation conversion)
[ERROR] /usr/local/google/home/jjg/gcdjavasample/hello-datastore/src/main/java/org/geewax/hellodatastore/HelloDatastore.java:[32,15] error: cannot find symbol
[ERROR] 

If I take away the .set("name", "John Doe") I get a different error...

Activity

  1. added
    type: questionRequest for information or clarification. Not an issue.
    api: datastoreIssues related to the Datastore API.
    on Mar 19, 2015
  2. added this to the milestone on Mar 19, 2015
  3. aozarov commented on Mar 19, 2015

    @aozarov
    Contributor

    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?

  4. assigned and unassigned on Mar 19, 2015
  5. jgeewax commented on Mar 20, 2015

    @jgeewax
    Author

    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 more
    

    However, 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.... ?

  6. jgeewax commented on Mar 20, 2015

    @jgeewax
    Author

    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 an Entity.Builder according 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 more
    
  7. assigned and unassigned on Mar 20, 2015
  8. jgeewax commented on Mar 20, 2015

    @jgeewax
    Author

    OK I think I found the problem.

    BaseEntity.Builder is 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.Builder public?

  9. aozarov commented on Mar 20, 2015

    @aozarov
    Contributor

    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)...

  10. assigned and unassigned on Mar 20, 2015
  11. 15 remaining items

  12. added a commit that references this issue on Sep 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: datastoreIssues related to the Datastore API.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions