如何映射地图< String,Double> [英] How to map a Map<String,Double>

查看:97
本文介绍了如何映射地图< String,Double>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试过

  @ManyToMany(cascade = CascadeType.ALL)
Map< String,Double> data = new HashMap< String,Double>();

但会产生错误:

  org.hibernate.AnnotationException:使用@OneToMany或@ManyToMany来定位未映射的类:com.company.Klass.data [java.lang.Double] 
,位于org.hibernate。 cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1016)
at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:567)
at org.hibernate.cfg.annotations。 MapBinder $ 1.secondPass(MapBinder.java:80)
at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java :1130)
at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296)
at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115)

有什么想法?

解决方案

<那么,错误消息我很清楚: Double 不是一个实体。如果要映射基本元素的集合,请使用 CollectionOfElement 批注(来自Hibernate)或 ElementCollection 批注(从JPA 2.0开始)。
$ b $ p因此,假设你使用Hibernate Annotations 3.4,试试这个:

  @CollectionOfElements(targetElement = Double.class)
@ org.hibernate.annotations.MapKey(targetElement = String.class)
地图数据;

或者,当使用泛型时:

  @CollectionOfElements 
Map< String,Double>数据;

如果您使用Hibernate Annotations 3.5+,则更喜欢JPA 2.0注释:

  @ElementCollection(targetClass = Double.class)
@MapKeyClass(String.class)
地图数据;

或者,当使用泛型时:

  @ElementCollection 
Map< String,Double>数据;



参考文献









你知道如何自定义ELEMENT和MAPKEY列名?

您可以完全自定义结果。我认为下面的示例演示了一切:

  @CollectionOfElements(targetElement = Double.class)
@JoinTable(name = COLLECTION_TABLE,
joinColumns = @JoinColumn(name =PARENT_ID))
@ org.hibernate.annotations.MapKey(targetElement = String.class,
columns = @Column(name = SOME_KEY))
@Column(name =SOME_VALUE)
私人地图数据;




  • 的集合表名称Map 是使用 JoinTable


    • 定义的。在 JoinTable

    • JoinColumn 来设置父项的关键字>
    • 映射键的列名在 MapKey
    • 中定义
    • 映射值的列名使用 Column


    来定义

    I tried

    @ManyToMany(cascade = CascadeType.ALL)
    Map<String, Double> data = new HashMap<String, Double>();
    

    but it produces the error :

       org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: com.company.Klass.data[java.lang.Double]
    at org.hibernate.cfg.annotations.CollectionBinder.bindManyToManySecondPass(CollectionBinder.java:1016)
    at org.hibernate.cfg.annotations.CollectionBinder.bindStarToManySecondPass(CollectionBinder.java:567)
    at org.hibernate.cfg.annotations.MapBinder$1.secondPass(MapBinder.java:80)
    at org.hibernate.cfg.CollectionSecondPass.doSecondPass(CollectionSecondPass.java:43)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1130)
    at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:296)
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1115)
    

    any idea?

    解决方案

    Well, the error message is pretty clear: Double isn't an entity. If you want to map a collection of basic elements, use the CollectionOfElement annotation (from Hibernate) or the ElementCollection annotation (from JPA 2.0).

    So, assuming you're using Hibernate Annotations 3.4, try this:

    @CollectionOfElements(targetElement = Double.class)
    @org.hibernate.annotations.MapKey(targetElement = String.class)
    Map data;
    

    Or, when using generics:

    @CollectionOfElements
    Map<String, Double> data;
    

    And if you're using Hibernate Annotations 3.5+, prefer the JPA 2.0 annotations:

    @ElementCollection(targetClass = Double.class)
    @MapKeyClass(String.class)
    Map data;
    

    Or, when using generics:

    @ElementCollection
    Map<String, Double> data;
    

    References


    Do you know how to customize the "ELEMENT" and "MAPKEY" column names ?

    You can fully customize the result. I think the sample below demonstrates everything:

    @CollectionOfElements(targetElement = Double.class)
    @JoinTable(name = "COLLECTION_TABLE", 
        joinColumns = @JoinColumn(name = "PARENT_ID"))
    @org.hibernate.annotations.MapKey(targetElement = String.class, 
        columns = @Column(name = "SOME_KEY"))
    @Column(name = "SOME_VALUE")
    private Map data;
    

    • The name of the collection table for the Map is defined using the JoinTable
      • The name of the column for the key to the parent is set using a JoinColumn in the JoinTable
    • The name of the column for the key of the map is defined in the MapKey
    • The name of the column for the value of the map is defined using the Column

    这篇关于如何映射地图&lt; String,Double&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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