【问题标题】:Writing a HashMap to database using Hibernate使用 Hibernate 将 HashMap 写入数据库
【发布时间】:2013-10-31 11:52:06
【问题描述】:

我正在尝试使用 Hibernate 将 hashmap 的值写入数据库。

我的 POJO 类 包org.rahul;

import java.util.HashMap;
import java.util.Map;

import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.MapKeyColumn;
import javax.persistence.Table;

@Entity
@Table(name = "hash")
public class HashMapExample {

  @Id
  @GeneratedValue
  private int id;

  @ElementCollection
  @MapKeyColumn(name = "key_key")
  @JoinTable(name = "example_attributes", joinColumns =
                                  @JoinColumn(name"example_id"))
  private Map<String, String> map = new HashMap<String, String>();

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public Map<String, String> getMap() {
    return map;
  }

  public void setMap(Map<String, String> map) {
    this.map = map;
  }

}

主类

package org.rahul.hibernate;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.rahul.HashMapExample;

public class HashMapMain {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        SessionFactory sessionFactory = new AnnotationConfiguration()
                .configure("hibernate.cfg.xml").buildSessionFactory();
        Session session = sessionFactory.openSession();
        session.beginTransaction();

        String name = "Rahul";
        String company = "Memoir";

        Map<String, String> map = new HashMap<String, String>();
        map.put(name, company);
        Map<String, String> map1 = new HashMap<String, String>();
        map1.put("XYZ", "ABC");

        HashMapExample hash = new HashMapExample();
        HashMapExample hash1 = new HashMapExample();

        // hash.setId(2);
        hash1.setMap(map);
        hash.setMap(map1);
        session.getTransaction();
        session.save(hash);
        session.save(hash1);
        session.getTransaction().commit();

    }
}

我希望获得包含键值对的列的表,但是我将键值作为不同的列。

example_attribut table description : 
  CREATE TABLE `example_attributes` 
  ( 
     `example_id` INT(11) NOT NULL, 
     `map`        VARCHAR(255) DEFAULT NULL, 
     `key_key`    VARCHAR(255) NOT NULL, 
     PRIMARY KEY (`example_id`, `key_key`), 
     KEY `fkea50c74c5cd44e54` (`example_id`), 
     CONSTRAINT `fkea50c74c5cd44e54` FOREIGN KEY (`example_id`) REFERENCES 
     `hash` (`id`) 
  ) 
engine=innodb 
DEFAULT charset=utf8

哈希表描述:

CREATE TABLE `hash` (
  `id` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

为什么我无法将字段 map 和 key_key 描述为 hashmap ?我希望它们像键值对一样位于单个字段中。谁能帮帮我?

【问题讨论】:

    标签: mysql hibernate


    【解决方案1】:

    所以有几种方法可以做到这一点。您可以将表示键/值对的 json 字符串保存到数据库中。 如果您需要从 2013 年开始查询,请不要使用 json

    {key: 'myKey', values:[{'1'},{'2'},{'3'}]}
    

    您可以将 java 对象持久化为 blob(二进制对象)。

    id number,
    map blob
    

    这将允许您将数据存储在一列中。我推荐 json 字符串,因为它不会强迫您在余生中使用 java,并且其他人也可以使用您的数据。

    【讨论】:

    • @Newbie 在我这样做之前,您打算查询这些数据吗?地图数据是什么?
    • 是的,我会尝试查询既有键又有值的列。我们可以这样做吗?
    • @Newbie 使这一点与众不同,并且存储 json 的建议消失了。如果我没记错的话,您需要将其存储为 blob。
    • 如何将其存储为 blob?
    • 帮我做两件事。使用 json 更新表并使用 blob 更新表并检索元素
    猜你喜欢
    • 2012-07-22
    • 2014-11-20
    • 2015-10-26
    • 2010-10-14
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 2019-02-03
    • 1970-01-01
    相关资源
    最近更新 更多