2010-12-28 8 views
0

Javaマップとして返​​されるHibernateで名前と値のペアを作成しようとしています。しかし、私はマップに返された行のうちの1つだけを取得しています。私はこのような単純なマップの本当の明確な例は見つけられておらず、私が間違っていることを見ることはできません。HibernateでJavaマップを使用して名前、値のペアを返す方法(マッピングファイルを使用)

制限された環境のため、テーブルを生成するために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) 

答えて

0

<key>それがあるべきよう<map>内部要素は、外部キーを指し

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

idzoomPropertyのテーブルはまったく必要ありません。

+0

はい、実際、必要な情報でした。ありがとう、ありがとう、ありがとう!!! – Ilane

関連する問題