【问题标题】:Hibernate: How to persist Class that contains Map<String, List<String>>Hibernate:如何持久化包含 Map<String, List<String>> 的类
【发布时间】:2010-09-27 01:25:54
【问题描述】:

我想坚持以下类,包括'bar'

Foo 类 { Map> 栏; // ... // 其他的东西..... }

如何在 Hibernate 中执行此操作? 如果可能的话,如何在 Hibernate 中使用注解来做到这一点?

哦,Map key String 的本质是它们的数量很少(5-40),并且它们在 Foo 实例之间是相同的。 List 中的字符串在 List 和 Foo 实例之间都是唯一的。

谢谢。

【问题讨论】:

  • 那么数据库的表现形式是什么?

标签: hibernate


【解决方案1】:

String 和 List 都不是实体,你应该创建一个包装类来封装你的 List

别忘了二传手

@Entity
public class Foo {

    private MutableInt id = new MutableInt();

    private Map<String, CustomList> customListMap = new HashSet<String, CustomList>();

    @Id
    @GeneratedValue
    public Integer getId() {
        return this.id.intValue();
    }

    public void setId(Integer id) {
        return this.id.setValue(id);
    }

    @OneToMany
    @MapKey(name="key")
    public Map<String, CustomList> getCustomListMap() {
        return customListMap;
    }

    // add convenience method
    public void addBar(String key, String bar) {
        if(customListMap.get(key) == null)
            customListMap.put(key, new CustomList(new CustomListId(id, key)));

        customListMap.get(key).getBar().add(bar);
    }

}

还有您的自定义 CustomList(不要忘记 setter 的)

@Entity
public class CustomList {

    private CustomListId customListId;

    private List<String> bar;

    private String key;

    @EmbeddedId
    public CustomListId getCustomListId() {
        return customListId;
    }

    @Column(insertable=false, updatable=false)
    public String getKey() {
        return this.key;
    }

    @CollectionOfElements
    @JoinTable(name="BAR")
    public List<String> getBar() {
        return this.bar;
    }

    @Embeddable
    public static class CustomListId implements Serializable {

        private MutableInt fooId = new MutableInt();
        private String key;

        // required no-arg construtor
        public CustomList() {}
        public CustomList(MutableInt fooId, String key) {
            this.fooId = fooId;
            this.key   = key;
        }

        public Integer getFooId() {
            return fooId.intValue();
        }

        public void setFooId(Integer fooId) {
            this.fooId.setValue(fooId);
        }

        // getter's and setter's

        public boolean equals(Object o)  {
            if(!(o instanceof CustomListId))
                return false;

            CustomListId other = (CustomList) o;
            return new EqualsBuilder()
                       .append(getFooId(), other.getFooId())
                       .append(getKey(), other.getKey())
                       .isEquals();
        }

        // implements hashCode

    }

} 

您甚至可以创建一个名为 getBar 的自定义方法透明地封装您的 customListMap,如下所示

@Entity
public class Foo {

    ...

    public Map<String, List<String>> getBar() {
        Map<String, List<String>> bar = new HashMap<String, List<String>>();

        for(Entry<String, CustomList> e: customListMap())
            bar.put(e.getKey(), e.getValue().getBar());

        return bar;
    }

}

【讨论】:

  • CustomList类在CustomList类里面,给我一个编译错误:
  • CustomList.java:33: CustomList 已经在未命名的包中定义 public static class CustomList implements Serializable {
猜你喜欢
  • 1970-01-01
  • 2012-03-06
  • 2016-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-16
  • 2020-08-01
相关资源
最近更新 更多