Overview
在本指南中,您可以学习如何创建表示 MongoDB 集合的 Hibernate ORM 实体。实体是定义数据结构的Java类。使用 Hibernate ORM 扩展时,您可以将每个实体映射到MongoDB集合,并使用这些实体与集合的文档进行交互。
MongoDB BSON字段
MongoDB以称为BSON的二进制表示形式组织和存储文档,从而支持灵活数据处理。本节介绍 Hibernate ORM 扩展对BSON字段的支持,您可以将其包含在实体中。
下表描述了支持的BSON字段类型以及可在 Hibernate ORM 实体中使用的 Hibernate ORM 扩展等效项:
BSON字段类型 | 扩展字段类型 | BSON描述 |
|---|---|---|
|
| Represents a null value or absence of data. |
|
| Stores binary data with subtype 0. |
|
| Stores UTF-8 encoded string values. |
|
| Stores 32-bit signed integers. |
|
| Stores 64-bit signed integers. |
|
| Stores floating-point values. |
|
| Stores true or false values. |
|
| Stores 28-bit decimal values. |
|
| Stores unique 12-byte identifiers that MongoDB uses as primary keys. |
|
| Stores dates and times as milliseconds since the Unix epoch. |
|
| Stores embedded documents with field values mapped according to their respective
types. @Struct aggregate embeddables might also contain array or Collection
attributes. |
|
| Stores array values with elements mapped according to their respective types. Character arrays require setting the hibernate.type.wrapper_array_handling configuration property. |
定义实体
要创建表示MongoDB集合的实体,请在项目的基础包目录中创建一个新的Java文件,并将实体类添加到新文件中。在实体类中,指定要存储的字段和集合名称。@jakarta.persistence.Table 注释的 name 元素表示您的MongoDB集合名称。使用以下语法定义实体:
public class <EntityName> { private ObjectId id; // Include additional fields here private <field type> <field name>; // Parameterized constructor public <EntityName>(<parameters>) { // Initialize fields here } // Default constructor public <EntityName>() { } // Getter and setter methods public <field type> get<FieldName>() { return <field name>; } public void set<FieldName>(<field type> <field name>) { this.<field name> = <field name>; } }
要使用实体,您可以在应用程序文件中查询实体。要学习有关 Hibernate ORM 扩展中的 CRUD 操作的详情,请参阅 执行 CRUD 操作指南。
例子
此示例Movie.java 实体类定义了一个 Movie 实体,其中包含以下信息:
@Entity将该类标记为 Hibernate ORM 实体的注解@Table注释,用于将实体映射到Atlas示例数据集中的movies集合@Id和@ObjectIdGenerator注解,用于将id字段指定为主主键并配置自动ObjectId生成表示电影数据的私有字段
用于实体实例化的默认构造函数和参数化构造函数
提供对实体字段访问权限权限的 Getter 和 setter 方法
package org.example; import com.mongodb.hibernate.annotations.ObjectIdGenerator; import org.bson.types.ObjectId; import java.util.List; import jakarta.persistence.Entity; import jakarta.persistence.Id; import jakarta.persistence.Table; public class Movie { private ObjectId id; private String title; private String plot; private int year; private List<String> cast; public Movie(String title, String plot, int year, List<String> cast) { this.title = title; this.plot = plot; this.year = year; this.cast = cast; } public Movie() { } public ObjectId getId() { return id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getPlot() { return plot; } public void setPlot(String plot) { this.plot = plot; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public List<String> getCast() { return cast; } public void setCast(List<String> cast) { this.cast = cast; } }
提示
要了解有关实体类定义中使用的字段的更多信息,请参阅本指南中MongoDB BSON字段部分。
嵌入式数据
Hibernate ORM 扩展通过 Hibernate ORM @Embeddable 注释支持嵌入式文档。使用嵌入式文档,您可以在MongoDB文档中创建一对多、多对一和一对一关系。这种格式非常适合表示经常一起访问的数据。
要表示嵌入式文档,请在类上使用 @Struct 和 @Embeddable 注解来创建 @Struct 聚合嵌入式文档。然后,将可嵌入类型作为字段包含在父实体中。Hibernate ORM 扩展支持嵌入单个对象、数组和可嵌入对象的集合。
提示
要学习;了解有关@Struct 聚合可嵌入项的更多信息,请参阅 Hibernate ORM 文档中的 @Struct 聚合可嵌入项映射。
一对一关系
A One-to-One relationship is when a record in one database is associated with exactly one record in another database. In MongoDB, you can create a collection with an embedded document field to model a One-to-One relationship. The Hibernate ORM extension allows you to create embedded document fields by using @Struct aggregate embeddables.
The example defines a field with a @Struct aggregate embeddable type in an entity similar to the Define an Entity example in this guide. The sample Movie.java entity class includes the following information:
@Entity和@Table注解,用于定义实体并将其映射到movies集合@Id以及将id字段指定主键的@ObjectIdGenerator注解表示电影标题的字符串字段
@Struct聚合表示电影奖项和工作室信息的可嵌入式字段
The following example represents a One-to-One relationship because each Movie entity is associated with one Awards embeddable and one Studio embeddable:
public class Movie { private ObjectId id; private String title; private Awards awards; private Studio studio; public Movie(String title, Awards awards, Studio studio) { this.title = title; this.awards = awards; this.studio = studio; } public Movie() { } // Getter and setter methods }
以下示例代码创建一个 Awards @Struct 聚合可嵌入对象:
public class Awards { private int wins; private int nominations; private String text; public Awards(int wins, int nominations, String text) { this.wins = wins; this.nominations = nominations; this.text = text; } public Awards() { } // Getter and setter methods }
以下示例代码创建一个 Studio @Struct 聚合可嵌入对象:
public class Studio { private String name; private String location; private int foundedYear; public Studio(String name, String location, int foundedYear) { this.name = name; this.location = location; this.foundedYear = foundedYear; } public Studio() { } // Getter and setter methods }
一对多关系
一对多关系是指一个数据库中的一条记录与另一个数据库中的多条记录相关联。在MongoDB中,您可以定义一个集合字段来存储嵌入式文档列表,以对一对多关系进行建模。 Hibernate ORM 扩展允许您使用 @Struct 聚合可嵌入对象的列表来创建嵌入式文档字段。
The example defines a field that stores a list of @Struct aggregate embeddables in an entity similar to the Define an Entity Example in this guide. The sample Movie.java entity class includes the following information:
@Entity和@Table注解,用于定义实体并将其映射到movies集合@Id以及将id字段指定主键的@ObjectIdGenerator注解表示电影标题的字符串字段
存储多个
Writer@Struct聚合可嵌入对象的列表字段,表示作者信息
The following example represents a One-to-Many relationship because each Movie entity is associated with multiple Writer embeddables:
public class Movie { private ObjectId id; private String title; private List<Writer> writers; public Movie(String title, List<Writer> writers) { this.title = title; this.writers = writers; } public Movie() { } // Getter and setter methods }
以下示例代码创建一个 Writer @Struct 聚合可嵌入对象:
public class Writer { private String name; public Writer() { } public Writer(String name) { this.name = name; } // Getter and setter methods }
更多信息
要学习;了解如何使用实体运行数据库操作,请参阅“与数据交互”部分中的以下指南:
要学习;了解有关 Hibernate ORM 字段的更多信息,请参阅 Hibernate ORM 文档中的映射类型部分。
要学习;了解有关 Hibernate ORM 实体的更多信息,请参阅 Hibernate ORM 文档中的POJO 模型。