对于 Connector/J 8.0.21 及更高版本,当使用 MySQL Server 8.0.19 及更高版本时:可以为 a 配置架构验证Collection,以便在Collection插入或更新文档之前根据架构验证其中的文档。这是通过创建或修改期间指定JSON Schema来完成的;Collection模式验证随后由服务器在文档创建或更新时执行,如果文档未针对分配的模式进行验证,则会返回错误。有关 MySQL 中 JSON 模式验证的更多信息,请参阅
        JSON 模式验证函数。本节介绍如何为
        Collection带连接器/J。
      
        要在创建 a 期间配置模式验证,请将
        对象Collection传递给
        createCollection()方法
        CreateCollectionOptions,该对象具有以下字段:
      
- reuse:由方法设置的布尔值- setReuseExisting。如果是- true,当- Collection要创建的对象已经存在于- Schema要包含它的对象中时,Connector/J 返回成功(不尝试将 JSON 模式应用于现有对象- Collection);在相同的情况下,如果参数设置为 Connector/J,则返回错误- false。如果- reuse未设置,则视为- false.
- 
validation:Validation由方法设置的对象setValidation()。一个Validation对象依次包含这些字段:- 
level: 类的枚举ValidationLevel,由setLevel()方法设置;它可以是以下两个值之一:- STRICT: 严格验证。尝试插入或修改违反验证模式的文档会导致出现服务器错误。
- OFF: 没有验证。架构验证已关闭。
 如果 level未设置,则将其视为OFFMySQL Server 8.0.19 和STRICT8.0.20 及更高版本。
- 
schema: 表示用于验证中的JSON 模式的字符串 ;通过 方法设置。DocumentCollectionsetSchema()如果 schema未提供但level设置为 STRICT,Collection则会根据默认模式验证{"type" : "object"}。
 
- 
        这是一个如何在创建时配置架构验证的示例Collection:
      
Collection coll = this.schema.createCollection(collName,
    new CreateCollectionOptions()
        .setReuseExisting(false)
        .setValidation(new Validation()
           .setLevel(ValidationLevel.STRICT)
           .setSchema(
               "{\"id\": \"http://json-schema.org/geo\","
               + "\"$schema\": \"http://json-schema.org/draft-06/schema#\","
               + "       \"description\": \"A geographical coordinate\","
               + "       \"type\": \"object\","
               + "       \"properties\": {"
               + "          \"latitude\": {"
               + "             \"type\": \"number\""
               + "          },"
               + "          \"longitude\": {"
               + "             \"type\": \"number\""
               + "          }"
               + "       },"
               + "       \"required\": [\"latitude\", \"longitude\"]"
               + "  }"
)));set 字段可以通过相应的 getter 方法访问。
        要修改 a 的架构验证配置
        Collection,请使用该
        modifyCollection()方法并向其传递一个
        ModifyCollectionOptions对象,该对象具有与CreateCollectionOptions
        对象相同的reuse字段,但对象不存在该字段ModifyCollectionOptions
        。对于Validation对象的
        ModifyCollectionOptions对象,用户可以设置其level或
        schema,或两者。下面是使用modifyCollection()更改架构验证配置的示例:
      
schema.modifyCollection(collName,
    new ModifyCollectionOptions()
        .setValidation(new Validation()
           .setLevel(ValidationLevel.OFF)
           .setSchema(
               "{\"id\": \"http://json-schema.org/geo\","
               + "\"$schema\": \"http://json-schema.org/draft-06/schema#\","
               + "       \"description\": \"NEW geographical coordinate\","
               + "       \"type\": \"object\","
               + "       \"properties\": {"
               + "          \"latitude\": {"
               + "             \"type\": \"number\""
               + "          },"
               + "          \"longitude\": {"
               + "             \"type\": \"number\""
               + "          }"
               + "       },"
               + "       \"required\": [\"latitude\", \"longitude\"]"
               + "  }"
)));
        如果 Collection 包含的文档未根据所提供的新 JSON 架构进行验证
        ModifyCollectionOptions,则服务器将拒绝架构修改并显示错误ERROR
        5180 (HY000) Document is not valid according to the schema
        assigned to collection。
      
          createCollection()和
          modifyCollection()被重载:它们可以在不分别传递给它们
          CreateCollectionOptions或 的
          情况下被调用ModifyCollectionOptions,在这种情况下,架构验证将不会应用于
          Collection.