简体   繁体   English

Hibernate-如何持久化HashMap

[英]Hibernate - How to persist a HashMap

Searching Stackoverflow, I saw that many people have my same problem, but I can't find out the solution looking at the other posts. 在搜索Stackoverflow时,我看到很多人都遇到了同样的问题,但是在其他帖子中我找不到解决方案。 So: 所以:

I am trying to persist a Hibernate class, but without using an Entity Object. 我试图持久化一个Hibernate类,但不使用Entity Object。 Instead I am using a Map. 相反,我正在使用地图。

This is my map: 这是我的地图:

Map<String, Object> record;
record = new HashMap<String, Object>();

...

record.put("key1", "value");
record.put("key2", "value");
record.put("field1", "value");
record.put("field2", "value");
record.put("field3", "value");
record.put("field4", "value");
record.put("field5", "value");
record.put("field6", value);
record.put("field7", value);

and this is the hbm.xml 这是hbm.xml

<class entity-name="entity_name" dynamic-update="true">
<composite-id name="Key1Key2" class="classname">
    <key-property name="Key1" column="Key1" type="string"/>
    <key-property name="Key2" column="Key2" type="string"/>
</composite-id>
    <property name="field1" column="field1" type="string"/>
    <property name="field2" column="field2" type="string"/>
    <property name="field3" column="field3" type="string"/>
    <property name="field4" column="field4" type="string"/>
    <property name="field5" column="field5" type="string"/>
    <property name="field6" column="field6" type="double"/>
    <property name="field7" column="field7" type="double"/>
</class>

When I try to persist the record: 当我尝试保留记录时:

super.session.persist("entity_name", record)

This error is returned: 返回此错误:

org.hibernate.id.IdentifierGenerationException: ids for this class must be manually assigned before calling save() org.hibernate.id.IdentifierGenerationException:此类的ID必须在调用save()之前手动分配

Can anyone help me? 谁能帮我? Thank in advance! 预先感谢!

I solved the question changing the composite-id properties like this: 我解决了更改复合ID属性的问题,如下所示:

<composite-id class="CompositeId" mapped="true">

( mapped="true" is the key of the solution) 映射的“ true”是解决方案的关键)

and then assigning the key-properties values in the hashmap. 然后在哈希图中分配键属性值。

Short answer: your class needs to be a POJO. 简短答案:您的课程需要是POJO。

Long answer: you need to create a separate class for composite IDs, something like 长答案:您需要为复合ID创建一个单独的类,例如

public final class CompositeId implements Serializable {
  private String first;
  private String second;

  // getters, setters, hashCode, equals
}

Your persistent object would be 您的持久对象将是

public class YourClass {
  private CompositeId id;
  private Map map;

  public YourClass() {
    this.map = new HashMap();
  }

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

  public CompositeId getId() {
    return id;
  }

  public void setField1(String field1) {
    this.map.put("field1", field1);
  }  

  public String getField1() {
    return map.get("field1");
  }  
  // and so forth
}

And, finally, your .hbm.xml: 最后,您的.hbm.xml:

<class entity-name="entity_name" dynamic-update="true">
<composite-id name="id" class="CompositeId">
    <key-property name="first" column="Key1" type="string"/>
    <key-property name="second" column="Key2" type="string"/>
</composite-id>
    <property name="field1" column="field1" type="string"/>
    <property name="field2" column="field2" type="string"/>
    <property name="field3" column="field3" type="string"/>
    <property name="field4" column="field4" type="string"/>
    <property name="field5" column="field5" type="string"/>
    <property name="field6" column="field6" type="double"/>
    <property name="field7" column="field7" type="double"/>
</class>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM