JPA Map< String,String []>映射

编程入门 行业动态 更新时间:2024-10-11 09:20:14
本文介绍了JPA Map< String,String []>映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何在不使用Hibernate的类的情况下在JPA中映射地图?

How can I map a Map in JPA without using Hibernate's classes?

public Map<String, String[]> parameters = new HashMap<String, String[]>();

谢谢.

推荐答案

示例实现:

@Entity @Table(name = "MAP") //optional public class Parameters { @Id @Column(name = "\"KEY\"") //optional private String id; @ElementCollection @CollectionTable( //optional name = "MAP_VALUES", joinColumns = { @JoinColumn(name="MAP_KEY") } ) private Collection<String> collection; public Parameters() { } public Parameters(String key, Collection<String> values) { this.id = key; this.collection = values; } public Collection<String> values() { return collection; } // ... }

可以将实体实例插入数据库,如下所示:

The entity instances can be inserted into the database as follows:

em.persist(new Parameters("first", Arrays.asList("a", "b", "c"))); em.persist(new Parameters("second", Arrays.asList("d", "e", "f"))); ...

这将在数据库中产生两个表:

This will produce two tables in the database:

MAP MAP_VALUES KEY MAP_KEY COLLECTION ------ ------- ---------- first first a second first b second c second d

MAP_VALUES表中的

MAP_KEY列是外键,并引用MAP表.

MAP_KEY column in the MAP_VALUES table is the foreign key and refers to the MAP table.

可以按以下方式检索内容:

Contents can be retrieved as follows:

Parameters entry = em.find(Parameters.class, "second"); List<String> values = entry.values(); ...

String query = "SELECT p FROM Parameters p"; List<Parameters> entries = em.createQuery(query, Parameters.class) .getResultList(); List<String> values = entry.values(); ...

更多推荐

JPA Map&lt; String,String []&gt;映射

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

发布评论

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

>www.elefans.com

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