JPA和Hibernate中的@MapKey,@MapKeyColumn和@MapKeyJoinColumn之间的区别 [英] Difference between @MapKey, @MapKeyColumn and @MapKeyJoinColumn in JPA and Hibernate

查看:441
本文介绍了JPA和Hibernate中的@MapKey,@MapKeyColumn和@MapKeyJoinColumn之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Hibernate文档,有多个注释如果我们想使用Map作为我们的实体之间的关联可用。该文档说:


或者,映射键映射到专用的一列或多列。
为了自定义映射,使用以下
注释之一:


$ b @MapKeyColumn如果map键是基本类型。如果您未指定
列名称,则使用该属性的名称,后跟紧跟着
(通过KEY)的下划线(例如orders_KEY)。 @MapKeyEnumerated /
@MapKeyTemporal如果地图键类型分别是枚举或日期。
@ MapKeyJoinColumn / @ MapKeyJoinColumns如果地图键类型是另一个
实体。 @ AttributeOverride / @ AttributeOverrides当map键是
可嵌入对象时。使用键。作为可嵌入对象
属性名称的前缀。你也可以使用@MapKeyClass来定义
的类型,如果你不使用泛型。

通过做一些例子我能够理解@MapKey只是用来将键映射到目标实体的属性,并且这个键仅用于获取记录。 @MapKeyColumn用于将键映射到目标实体的属性,该键用于保存和提取记录。请让我知道如果这是正确的?



另外,请让我知道,当我需要使用@ MapKeyJoinColumn / @ MapKeyJoinColumns& @MapKeyEnumerated / @MapKeyTemporal



感谢!

解决方案

一个 Map 你总是需要关联至少两个实体。假设我们有一个与 Car 实体相关的 Owner 实体( Car 有一个FK到所有者)。 因此,所有者将有一个 Map Car(s)

 地图< X,Car> 




  1. @MapKey 将为您提供 Car's 字段,用于将多个 Car(s)分组到 $ <$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ Car ,例如 WheelDrive

      @Entity 
    公共类所有者{
    @Id
    私人长ID;

    @OneToMany(mappedBy =owner)
    @MapKeyEnumerated(EnumType.STRING)
    私人地图< WheelDrive,Car> carMap;
    }

    @实体
    公共类汽车{
    @Id
    私人长ID;

    @ManyToOne
    私人所有者;

    @Column(name =wheelDrive)
    @Enumerated(EnumType.STRING)
    私人WheelDrive wheelDrive;

    }

    public enum WheelDrive {
    2WD,
    4WD;

    这会按照他们的WheelDrive类型对汽车进行分类。


  2. @MapKeyTemporal 将使用日期 / 日历分组字段,例如 createdOn

      @实体
    公共类所有者{
    @Id
    私人长ID;

    @OneToMany(mappedBy =owner)
    @MapKeyTemporal(TemporalType.TIMESTAMP)
    private Map< Date,Car> carMap;
    }

    @实体
    公共类汽车{
    @Id
    私人长ID;

    @ManyToOne
    私人所有者;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name =created_on)
    私人日历createdOn;


  3. @MapKeyJoinColumn 需要第三个实体,例如制造商,以便您拥有从所有者的关联。 Car 和car也与制造商有关联,以便您可以将所有所有者 Cars by 制造商

      @Entity 
    公共类所有者{
    @Id
    私人长ID;

    @OneToMany(mappedBy =owner)
    @MapKeyJoinColumn(name =manufacturer_id)
    private Map< Manufacturer,Car> carMap;
    }

    @实体
    公共类汽车{
    @Id
    私人长ID;

    @ManyToOne
    私人所有者;

    @ManyToOne
    @JoinColumn(name =manufacturer_id)
    私人制造商制造商;
    }

    @实体
    公共类制造商{
    @Id
    私人长ID;

    私人字符串名称;
    }



As per Hibernate documentation, there are multiple annotations available if we want to use Map as an association between our entities. The doc says:

Alternatively the map key is mapped to a dedicated column or columns. In order to customize the mapping use one of the following annotations:

@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.

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?

Also please let me know when I need to use @MapKeyJoinColumn/@MapKeyJoinColumns & @MapKeyEnumerated / @MapKeyTemporal

Thanks!

解决方案

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).

So, the Owner will have a Map of Car(s):

Map<X, Car>

  1. The @MapKey will give you the Car's field used to group multiple Car(s) to an Owner:

  2. @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.

  3. @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;         
    }   
    

  4. @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之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆