简体   繁体   中英

How to map fetched Google cloud datastore Entity (com.google.cloud.datastore.Entity) to Custom Java object

I'm fetching a list of Datastore Entity (com.google.cloud.datastore.Entity) using below query (com.google.cloud.datastore.Query). I'm unable to map the Datastore Entity object to my custom Java object. Any suggestions would be of great help. Thank you.

Query<Entity> query = Query.newEntityQueryBuilder()
                .setNamespace("abc")
                .setKind("ca")
                .setFilter(StructuredQuery.PropertyFilter.eq("name", "xyz"))
                .build();

QueryResults<Entity> tasks = datastore.run(query);

for (QueryResults<Entity> it = tasks; it.hasNext(); ) {
            Entity entity = it.next();
            User user = entity.toUser();//How to map to my custom Java class
        }

Not sure what is relation between User and entity in Datastore, and what exactly you want to do, however there is getPropertyMap method ( reference ) in the Datastore Java API. The method seems to be ideal for you task as it returns Map<String,Value> .

All you need to do is to implement new constructor in User class that will create User instance from this Map .

Than the line should look like this: User user = new User(entity.getPropertyMap());

I am sure this is just one of tones of ways to do it:)

I hope it will help!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM