存储 Map<String,String>使用 JPA

编程入门 行业动态 更新时间:2024-10-11 01:12:41
本文介绍了存储 Map&lt;String,String>使用 JPA的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想知道是否可以使用注释将 attributes 映射保存在使用 JPA2 的以下类中

I am wondering if it is possible using annotations to persist the attributes map in the following class using JPA2

public class Example { long id; // .... Map<String, String> attributes = new HashMap<String, String>(); // .... }

因为我们已经有一个预先存在的生产数据库,所以理想情况下 attributes 的值可以映射到以下现有表:

As we already have a pre existing production database, so ideally the values of attributes could map to the following existing table:

create table example_attributes { example_id bigint, name varchar(100), value varchar(100));

推荐答案

JPA 2.0 通过 @ElementCollection 注释支持原语集合,您可以将其与 java 的支持结合使用.util.Map 集合.这样的事情应该可以工作:

JPA 2.0 supports collections of primitives through the @ElementCollection annotation that you can use in conjunction with the support of java.util.Map collections. Something like this should work:

@Entity public class Example { @Id long id; // .... @ElementCollection @MapKeyColumn(name="name") @Column(name="value") @CollectionTable(name="example_attributes", joinColumns=@JoinColumn(name="example_id")) Map<String, String> attributes = new HashMap<String, String>(); // maps from attribute name to value }

另见(在 JPA 2.0 规范中)

  • 2.6 - 可嵌入类和基本类型的集合
  • 2.7 地图集合
  • 10.1.11 - ElementCollection 注释
  • 11.1.29 MapKeyColumn 注释

更多推荐

存储 Map<String,String>使用 JPA

本文发布于:2023-11-25 10:25:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1629388.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:lt   Map   String   JPA   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!