如何在 Hibernate 中通过 Java Map 返回名称、值对(使用映射文件)

发布于 2024-10-09 15:00:46 字数 4971 浏览 0 评论 0原文

我正在尝试在 Hibernate 中创建一个名称、值对,它作为 Java 映射返回。但我只得到地图中返回的 1 行。我还没有找到任何像这样的简单地图的真正清晰的例子,我看不出我做错了什么。

由于环境限制,我无法运行hbm2dll来生成表,所以我手动生成了它,可能会出现错误。

这是我的父表和子表:

mysql> select * from zoomProperties;
+----+---------------+
| id | entityVersion |
+----+---------------+
|  1 |             0 | 
+----+---------------+
1 row in set (0.00 sec)

mysql> select * from zoomProperty;
+----+-------+-------+----------------------+
| id | name  | value | parentZoomProperties |
+----+-------+-------+----------------------+
|  1 | prop1 | val1  |                    1 | 
|  2 | prop2 | val2  |                    1 | 
|  3 | prop3 | val3  |                    1 | 
+----+-------+-------+----------------------+
3 rows in set (0.00 sec)

但我只返回一个值:

2010-12-28 16:45:58,437 ERROR [STDERR] setProperties with:
2010-12-28 16:45:58,453 ERROR [STDERR]  Key: prop1
2010-12-28 16:45:58,453 DEBUG [com.mycompany.zoom] Query returns 1 ZoomProperties
2010-12-28 16:45:58,453 ERROR [STDERR] getProperties returning:
2010-12-28 16:45:58,454 ERROR [STDERR]  Key: prop1
2010-12-28 16:45:58,454 DEBUG [com.mycompany.zoom] Key: prop1, Value: val1
2010-12-28 16:45:58,454 DEBUG [com.mycompany.zoom] ZoomProperies returned 1 properties

这是我的 Hibernate 查询:

List resultList = em.createQuery("from ZoomProperties").getResultList();
log.debug("Query returns " + resultList.size() + " ZoomProperties");

这是我的映射文件:

<hibernate-mapping>
  <class name="com.mycompany.zoom.domain.ZoomProperties" table="zoomProperties">
    <id name="id">
      <generator class="increment"/>
    </id>
    <version name="entityVersion"/>

    <map name="properties" table="zoomProperty">
      <key column="id"/>
      <map-key column="name" type="string"/>
      <element column="value" type="string"/>
    </map>
  </class>
</hibernate-mapping>

和我的 pojo:

package com.mycompany.zoom.domain;

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

public class ZoomProperties
{
    private Long id;
    private Map<String, String> properties = new HashMap<String, String>();
    private Integer entityVersion;

    public ZoomProperties() {}

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public Map<String, String> getProperties() //{ return properties; }
    {
        System.err.println("getProperties returning:");
        java.util.Set<String> propertyKeySet = properties.keySet();
        for (String key : propertyKeySet)
        {
            System.err.println("\tKey: " + key);
        }
        return properties;
    }
    public void setProperties(Map<String, String> properties) //{ this.properties = properties; }
    {
        this.properties = properties;
        System.err.println("setProperties with:");
        java.util.Set<String> propertyKeySet = properties.keySet();
        for (String key : propertyKeySet)
        {
            System.err.println("\tKey: " + key);
        }
    }

    public Integer getEntityVersion() { return entityVersion; }
    public void setEntityVersion(Integer entityVersion) { this.entityVersion = entityVersion; }

    public boolean equals(Object other) {
        if (other == this)
        {
            return true;
        }
        if (other instanceof ZoomProperties)
        {
            return true;
        }
        return false;
    }

    public int hashCode() {
        return "This is the one and only ZoomProperties".hashCode();
    }
}

和我的表创建信息:

mysql> show create table zoomProperties;
+----------------+---------------------------------+
| Table          | Create Table                    |
+--------------------------------------------------+
| zoomProperties | CREATE TABLE `zoomProperties` (
  `id` bigint(20) NOT NULL auto_increment,
  `entityVersion` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1             | 
+----------------+---------------------------------+
1 row in set (0.00 sec)

mysql> show create table zoomProperty;
+--------------+-----------------------------------+
| Table        | Create Table                      |
+--------------+-----------------------------------+
| zoomProperty | CREATE TABLE `zoomProperty` (
  `id` bigint(20) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `value` varchar(255) default NULL,
  `parentZoomProperties` bigint(20) default NULL,
  PRIMARY KEY  (`id`),
  KEY `parentZoomProperties` (`parentZoomProperties`),
  CONSTRAINT `zoomProperty_ibfk_1` 
    FOREIGN KEY (`parentZoomProperties`) 
    REFERENCES `zoomProperties` (`id`) 
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1             | 
+--------------+-----------------------------------+
1 row in set (0.00 sec)

I am trying to create a name,value pair in Hibernate which gets returned as a Java Map. But I am only getting 1 of the rows returned in the Map. I haven't found any really clear examples of a simple Map such as this and I can't see what I am doing wrong.

Due to a constrained environment, I am unable to run hbm2dll to generate the table, so I generated it by hand and it is possible that I have errors there.

Here are my parent and child tables:

mysql> select * from zoomProperties;
+----+---------------+
| id | entityVersion |
+----+---------------+
|  1 |             0 | 
+----+---------------+
1 row in set (0.00 sec)

mysql> select * from zoomProperty;
+----+-------+-------+----------------------+
| id | name  | value | parentZoomProperties |
+----+-------+-------+----------------------+
|  1 | prop1 | val1  |                    1 | 
|  2 | prop2 | val2  |                    1 | 
|  3 | prop3 | val3  |                    1 | 
+----+-------+-------+----------------------+
3 rows in set (0.00 sec)

But I only get one value back:

2010-12-28 16:45:58,437 ERROR [STDERR] setProperties with:
2010-12-28 16:45:58,453 ERROR [STDERR]  Key: prop1
2010-12-28 16:45:58,453 DEBUG [com.mycompany.zoom] Query returns 1 ZoomProperties
2010-12-28 16:45:58,453 ERROR [STDERR] getProperties returning:
2010-12-28 16:45:58,454 ERROR [STDERR]  Key: prop1
2010-12-28 16:45:58,454 DEBUG [com.mycompany.zoom] Key: prop1, Value: val1
2010-12-28 16:45:58,454 DEBUG [com.mycompany.zoom] ZoomProperies returned 1 properties

Here is my Hibernate query:

List resultList = em.createQuery("from ZoomProperties").getResultList();
log.debug("Query returns " + resultList.size() + " ZoomProperties");

Here is my mapping file:

<hibernate-mapping>
  <class name="com.mycompany.zoom.domain.ZoomProperties" table="zoomProperties">
    <id name="id">
      <generator class="increment"/>
    </id>
    <version name="entityVersion"/>

    <map name="properties" table="zoomProperty">
      <key column="id"/>
      <map-key column="name" type="string"/>
      <element column="value" type="string"/>
    </map>
  </class>
</hibernate-mapping>

And my pojo:

package com.mycompany.zoom.domain;

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

public class ZoomProperties
{
    private Long id;
    private Map<String, String> properties = new HashMap<String, String>();
    private Integer entityVersion;

    public ZoomProperties() {}

    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }

    public Map<String, String> getProperties() //{ return properties; }
    {
        System.err.println("getProperties returning:");
        java.util.Set<String> propertyKeySet = properties.keySet();
        for (String key : propertyKeySet)
        {
            System.err.println("\tKey: " + key);
        }
        return properties;
    }
    public void setProperties(Map<String, String> properties) //{ this.properties = properties; }
    {
        this.properties = properties;
        System.err.println("setProperties with:");
        java.util.Set<String> propertyKeySet = properties.keySet();
        for (String key : propertyKeySet)
        {
            System.err.println("\tKey: " + key);
        }
    }

    public Integer getEntityVersion() { return entityVersion; }
    public void setEntityVersion(Integer entityVersion) { this.entityVersion = entityVersion; }

    public boolean equals(Object other) {
        if (other == this)
        {
            return true;
        }
        if (other instanceof ZoomProperties)
        {
            return true;
        }
        return false;
    }

    public int hashCode() {
        return "This is the one and only ZoomProperties".hashCode();
    }
}

And my table creation info:

mysql> show create table zoomProperties;
+----------------+---------------------------------+
| Table          | Create Table                    |
+--------------------------------------------------+
| zoomProperties | CREATE TABLE `zoomProperties` (
  `id` bigint(20) NOT NULL auto_increment,
  `entityVersion` int(11) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1             | 
+----------------+---------------------------------+
1 row in set (0.00 sec)

mysql> show create table zoomProperty;
+--------------+-----------------------------------+
| Table        | Create Table                      |
+--------------+-----------------------------------+
| zoomProperty | CREATE TABLE `zoomProperty` (
  `id` bigint(20) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `value` varchar(255) default NULL,
  `parentZoomProperties` bigint(20) default NULL,
  PRIMARY KEY  (`id`),
  KEY `parentZoomProperties` (`parentZoomProperties`),
  CONSTRAINT `zoomProperty_ibfk_1` 
    FOREIGN KEY (`parentZoomProperties`) 
    REFERENCES `zoomProperties` (`id`) 
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1             | 
+--------------+-----------------------------------+
1 row in set (0.00 sec)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抹茶夏天i‖ 2024-10-16 15:00:46

中的 元素引用外键,因此应该是

<map name="properties" table="zoomProperty">
    <key column="parentZoomProperties"/>
    <map-key column="name" type="string"/>
    <element column="value" type="string"/>
</map>

zoomProperty 中的 id > 根本不需要表。

<key> element inside <map> refers to the foreign key, so it should be

<map name="properties" table="zoomProperty">
    <key column="parentZoomProperties"/>
    <map-key column="name" type="string"/>
    <element column="value" type="string"/>
</map>

id in zoomProperty table is not needed at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文