Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Error saving entity with specified id #519

@chuckwight

Description

@chuckwight

I am trying to write a utility to retrieve API keys and secrets from datastore using Objectify-6.1.0 and make them available as a static entity, Util. This compiles OK, but the ofy().save() in the last line of the static block throws the runtime error below. Please help me understand why this happens and how to fix it. Thanks.

package org.chemvantage;
import static com.googlecode.objectify.ObjectifyService.ofy;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;

@entity
public class Util {
static Util u;
@id static Long id;
private static String HMAC256Secret = "ChangeMe";

static {
	try {  // Load entity from datastore when new software version is deployed
		u = ofy().load().type(Util.class).first().safe();
	} catch (Exception e) {  // Run once when datastore is initiated
		ofy().save().entity(new Util());
	}
}

private Util() {}

protected static String getHMAC256Secret() { 
	return HMAC256Secret; 
}

}

java.lang.invoke.WrongMethodTypeException: cannot convert MethodHandle()Long to (Object)Object
at java.base/java.lang.invoke.MethodHandle.asTypeUncached(MethodHandle.java:903) ~[na:na]
at java.base/java.lang.invoke.MethodHandle.asType(MethodHandle.java:870) ~[na:na]
at java.base/java.lang.invoke.Invokers.checkGenericType(Invokers.java:541) ~[na:na]
at com.googlecode.objectify.impl.FieldProperty.get(FieldProperty.java:89) ~[objectify-6.1.0.jar:na]
at com.googlecode.objectify.impl.KeyMetadata.getId(KeyMetadata.java:318) ~[objectify-6.1.0.jar:na]
at com.googlecode.objectify.impl.KeyMetadata.requiresAutogeneratedId(KeyMetadata.java:330) ~[objectify-6.1.0.jar:na]
at com.googlecode.objectify.impl.Keys.requiresAutogeneratedId(Keys.java:139) ~[objectify-6.1.0.jar:na]
at com.googlecode.objectify.impl.Deferrer.undefer(Deferrer.java:79) ~[objectify-6.1.0.jar:na]
at com.googlecode.objectify.impl.WriteEngine.save(WriteEngine.java:68) ~[objectify-6.1.0.jar:na]
at com.googlecode.objectify.impl.SaverImpl.entities(SaverImpl.java:60) ~[objectify-6.1.0.jar:na]
at com.googlecode.objectify.impl.SaverImpl.entity(SaverImpl.java:35) ~[objectify-6.1.0.jar:na]
at org.chemvantage.Util.(Util.java:18) ~[classes/:na]
at org.chemvantage.HelloWorld.doGet(HelloWorld.java:23) ~[classes/:na]
...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions