问题描述
根据 Hibernate 文档,有多个注释如果我们想使用 Map 作为我们实体之间的关联,则可用.医生说:
As per Hibernate documentation, there are multiple annotations available if we want to use Map as an association between our entities. The doc says:
或者,映射键被映射到一个或多个专用列.为了自定义映射使用以下之一注释:
@MapKeyColumn 如果映射键是基本类型.如果您不指定列名,属性名后跟下划线使用 by KEY(例如 orders_KEY).@MapKeyEnumerated/@MapKeyTemporal 如果映射键类型分别是枚举或日期.@MapKeyJoinColumn/@MapKeyJoinColumns 如果地图键类型是另一个实体.@AttributeOverride/@AttributeOverrides 当映射键是一个可嵌入对象.使用钥匙.作为可嵌入对象的前缀属性名称.您还可以使用@MapKeyClass 来定义类型如果你不使用泛型,关键是.
@MapKeyColumn if the map key is a basic type. If you don't specify the column name, the name of the property followed by underscore followed by KEY is used (for example orders_KEY). @MapKeyEnumerated / @MapKeyTemporal if the map key type is respectively an enum or a Date. @MapKeyJoinColumn/@MapKeyJoinColumns if the map key type is another entity. @AttributeOverride/@AttributeOverrides when the map key is a embeddable object. Use key. as a prefix for your embeddable object property names. You can also use @MapKeyClass to define the type of the key if you don't use generics.
通过一些例子,我能够理解@MapKey 只是用于将键映射到目标实体的属性,而此键仅用于获取记录.@MapKeyColumn 用于将键映射到目标实体的属性,此键用于保存和获取记录.请告诉我这是否正确?
By doing some examples I am able to understand that @MapKey is just used to map the key to a property of target entity and this key is used only for fetching records. @MapKeyColumn is used to map the key to a property of target entity and this key is used to save as well as fetching records. Please let me know if this is correct?
另外请让我知道何时需要使用@MapKeyJoinColumn/@MapKeyJoinColumns &@MapKeyEnumerated/@MapKeyTemporal
Also please let me know when I need to use @MapKeyJoinColumn/@MapKeyJoinColumns & @MapKeyEnumerated / @MapKeyTemporal
谢谢!
推荐答案
当您使用 Map
时,您总是需要关联至少两个实体.假设我们有一个与 Car
实体相关的 Owner
实体(Car
有一个到 Owner
的 FK).
When you use a Map
you always need to associate at least two entities. Let's say we have an Owner
entity that relates to the Car
entity (Car
has a FK to Owner
).
因此,Owner
将拥有 Car(s)
的 Map
:
So, the Owner
will have a Map
of Car(s)
:
Map<X, Car>
@MapKey
@MapKey
将为您提供 Car's
属性,用于将 Car
分组到其 Owner
.例如,如果我们在 Car
中有一个 vin
(车辆识别号)属性,我们可以将它用作 carMap
键:
@MapKey
The @MapKey
will give you the Car's
property used to group a Car
to its Owner
. For instance, if we have a vin
(Vehicle Identification Number) property in Car
, we could use it as the carMap
key:
@Entity
public class Owner {
@Id
private long id;
@OneToMany(mappedBy="owner")
@MapKey(name = "vin")
private Map<String, Car> carMap;
}
@Entity
public class Car {
@Id
private long id;
@ManyToOne
private Owner owner;
private String vin;
}
@MapKeyEnumerated
@MapKeyEnumerated
将使用来自 Car
的枚举,例如 WheelDrive
:
@MapKeyEnumerated
The @MapKeyEnumerated
will use an Enum from Car
, like WheelDrive
:
@Entity
public class Owner {
@Id
private long id;
@OneToMany(mappedBy="owner")
@MapKeyEnumerated(EnumType.STRING)
private Map<WheelDrive, Car> carMap;
}
@Entity
public class Car {
@Id
private long id;
@ManyToOne
private Owner owner;
@Column(name = "wheelDrive")
@Enumerated(EnumType.STRING)
private WheelDrive wheelDrive;
}
public enum WheelDrive {
2WD,
4WD;
}
这将按车轮驱动类型对汽车进行分组.
This will group cars by their WheelDrive type.
@MapKeyTemporal
将使用 Date
/Calendar
字段进行分组,例如 createdOn
.
The @MapKeyTemporal
will use a Date
/Calendar
field for grouping, like createdOn
.
@Entity
public class Owner {
@Id
private long id;
@OneToMany(mappedBy="owner")
@MapKeyTemporal(TemporalType.TIMESTAMP)
private Map<Date, Car> carMap;
}
@Entity
public class Car {
@Id
private long id;
@ManyToOne
private Owner owner;
@Temporal(TemporalType.TIMESTAMP)
@Column(name="created_on")
private Calendar createdOn;
}
@MapKeyJoinColumn
@MapKeyJoinColumn
需要第三个实体,例如 Manufacturer
,以便您拥有从 Owner
到 Car
的关联> 并且 car 还与 Manufacturer
关联,因此您可以按 Manufacturer
将所有 Owner's
Cars
分组:
@MapKeyJoinColumn
The @MapKeyJoinColumn
requires a third entity, like Manufacturer
so that you have an association from Owner
to Car
and car has also an association to a Manufacturer
, so that you can group all Owner's
Cars
by Manufacturer
:
@Entity
public class Owner {
@Id
private long id;
@OneToMany(mappedBy="owner")
@MapKeyJoinColumn(name="manufacturer_id")
private Map<Manufacturer, Car> carMap;
}
@Entity
public class Car {
@Id
private long id;
@ManyToOne
private Owner owner;
@ManyToOne
@JoinColumn(name = "manufacturer_id")
private Manufacturer manufacturer;
}
@Entity
public class Manufacturer {
@Id
private long id;
private String name;
}
这篇关于JPA和Hibernate中@MapKey、@MapKeyColumn和@MapKeyJoinColumn的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!