codecamp

Spring Cloud Datastore 嵌入式实体

类型也用@Entity注释的字段将转换为EntityValue并存储在父实体中。

这是一个Cloud Datastore实体的示例,其中包含JSON中的嵌入式实体:

{
  "name" : "Alexander",
  "age" : 47,
  "child" : {"name" : "Philip"  }
}

这对应于一对简单的Java实体:

import org.springframework.cloud.gcp.data.datastore.core.mapping.Entity;
import org.springframework.data.annotation.Id;

@Entity("parents")
public class Parent {
  @Id
  String name;

  Child child;
}

@Entity
public class Child {
  String name;
}

Child实体不是以其自己的类型存储的。它们全部存储在parents类型的child字段中。

支持多个级别的嵌入式实体。

 嵌入式实体不需要具有@Id字段,只有顶级实体才需要。

例:

实体可以容纳自己类型的嵌入式实体。我们可以使用此功能将树存储在Cloud Datastore中:

import org.springframework.cloud.gcp.data.datastore.core.mapping.Embedded;
import org.springframework.cloud.gcp.data.datastore.core.mapping.Entity;
import org.springframework.data.annotation.Id;

@Entity
public class EmbeddableTreeNode {
  @Id
  long value;

  EmbeddableTreeNode left;

  EmbeddableTreeNode right;

  Map<String, Long> longValues;

  Map<String, List<Timestamp>> listTimestamps;

  public EmbeddableTreeNode(long value, EmbeddableTreeNode left, EmbeddableTreeNode right) {
    this.value = value;
    this.left = left;
    this.right = right;
  }
}

地图

地图将存储为嵌入式实体,其中键值成为嵌入式实体中的字段名称。这些映射中的值类型可以是任何常规支持的属性类型,并且将使用配置的转换器将键值转换为String。

同样,可以嵌入实体的集合。写入时将转换为ListValue

例:

代替上一个示例中的二叉树,我们想在Cloud Datastore中存储一棵普通树(每个节点可以有任意数量的子级)。为此,我们需要创建一个类型为List<EmbeddableTreeNode>的字段:

import org.springframework.cloud.gcp.data.datastore.core.mapping.Embedded;
import org.springframework.data.annotation.Id;

public class EmbeddableTreeNode {
  @Id
  long value;

  List<EmbeddableTreeNode> children;

  Map<String, EmbeddableTreeNode> siblingNodes;

  Map<String, Set<EmbeddableTreeNode>> subNodeGroups;

  public EmbeddableTreeNode(List<EmbeddableTreeNode> children) {
    this.children = children;
  }
}

由于地图是作为实体存储的,因此它们可以进一步保存嵌入式实体:

  • 值中的单个嵌入式对象可以存储在嵌入式Map的值中。
  • 值中嵌入对象的集合也可以存储为嵌入Map的值。
  • 值中的映射进一步存储为嵌入式实体,并对其值进行递归应用相同的规则。


Spring Cloud Datastore 用于集合的自定义转换器
Spring Cloud Datastore 祖辈关系
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

三、Spring Cloud Netflix

SpringCloud Hystrix超时和Ribbon客户

SpringCloud 重试失败的请求

五、Spring Cloud Stream

六、SpringCloud Binder实现

SpringCloud 重试RabbitMQ Binder

SpringCloud Dead-Letter队列处理

八、Spring Cloud Sleuth

SpringCloud 当前Span

十二、Spring Cloud for Cloud Foundry

十三、Spring Cloud Contract

Spring Cloud Contract验证程序设置

SrpingCloud Gradle项目

十五、Spring Cloud网关

Spring Cloud 配置路由谓词工厂和网关过滤工厂

Spring Cloud TLS / SSL

Spring Cloud网关配置

SpringCloud 故障排除

十八、Spring Cloud GCP

Spring Cloud GCP Spring资源

Spring Cloud Spring JDBC

Spring Cloud Redis的Cloud Memorystore

Spring Cloud 云身份识别代理(IAP)身份验证

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }