Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Entity Key with Objectify 4

I need to do getKey() with this kind of Entity:

@Entity
public class Value {
    @Id
    private long id;
    private byte[] value;

    com.googlecode.objectify.Key<Value> getKey() {
        return com.googlecode.objectify.Key.create(Value.class, id); // When executed this line throws NullPointerException
    }

        // Code omitted
}

However the pattern I used before with version 3 seems to be not applicable anymore. The @Transient is replaced by @Ignore but when I annotate my getKey() function with @Ignore I get this error:

The annotation `@Ignore` is disallowed for this location 

So I just commented it out. And see if it will work.

Furthermore,

When I run my application the getKey() function throws NullPointerException as commented above.

So, what is the pattern to get a @Entity key?

like image 514
quarks Avatar asked Jun 21 '26 06:06

quarks


1 Answers

You can't create a Key with a null or 0 id. Neither Objectify nor the datastore will allow it.

If you want to create a Key from an entity, make sure it has a valid id first.

like image 154
stickfigure Avatar answered Jun 23 '26 03:06

stickfigure