UserTypeCollection出错

时间:2013-07-22 10:12:07

标签: hibernate jpa

我已按照this tutorial实施自定义集合。

我还注释了我的实体的相应属性:

  @OneToMany(cascade = ALL, mappedBy = "plan", fetch = EAGER, orphanRemoval = true)
  @CollectionType(type = "my.namespace.MyCustomCollectionType")
  public MyCustomCollection getThings() {
    return things;
  }

然而,在创建映射时,Hibernate抱怨:

 Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements

根据Hibernate documentation,这应该没问题。我应该能够在方法签名中使用我的自定义集合,因为我使用自定义集合类型注释:

  

实际的接口可能是java.util.Set,java.util.Collection,java.util.List,java.util.Map,java.util.SortedSet,   java.util.SortedMap或任何你喜欢的东西(“你喜欢的任何东西”的意思是   你将不得不写一个实现   org.hibernate.usertype.UserCollectionType)。

我不知道出了什么问题。

1 个答案:

答案 0 :(得分:1)

好的,我发现了我的问题。

首先,为了更清楚,是的,我有2个班级。 第一个是我的自定义集合( MyCustomCollection ),另一个是扩展UserTypeCollection MyCustomCollectionType ),这是Hibernate为支持所需的技术接口自定义集合。

无论如何,我已经弄清楚出了什么问题:

  • 首先,Hibernate仅支持集合的方法签名中的接口,因此我写了 ICustomCollection 。然后我需要声明一个PersistentCollection (我的情况是PersistentSet),扩展了这个界面。
  • 然后 MyCustomCollectionType 应该返回此自定义持久集。

我通过查看Hibernate中的测试套件来解决这个问题。 There are a couple of examples

但遗憾的是, PersistentSet 扩展了非泛型集合,因此,您也必须声明非泛型集合。

这是Jira bug for it。它已经存在了一段时间。