我可以在@Embedded中使用@ElementCollection吗? [英] Can I use @ElementCollection inside an @Embedded?

查看:104
本文介绍了我可以在@Embedded中使用@ElementCollection吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有可选子实体的实体,其中包含更多字段.子实体又具有一组元素.

I have an entity with an optional sub-entity containing more fields. The sub-entity again has a collection of elements.

这在Hibernate中通常是不可能的吗?还是我错过了一些东西?

Is this generally impossible with Hibernate or am I missing something?

@Entity @Table(name="my_entity")
public class MyEntity {
  @Id 
  private Long id;

  @Embedded 
  @JoinTable(name="..." joinColumns=@JoinColumn(name="myEntityId"))
  private OptionalFields optionalFields;
}

@Embeddable
public class OptionalFields {
   @ElementCollection
   @CollectionTable(name="...", joinColumns=@JoinColumn(name="myEntityId"))
   private List<OpeningHoursItem> openingHours;
}

@Embeddable
public class OpeningHoursItem {

}

错误消息是:

Could not determine type for: java.util.List, at table: my_entity, for columns: [org.hibernate.mapping.Column(opening_hours)]

推荐答案

这听起来很奇怪,但确实有效:

It may sound strange, but it really works:

将所有注释从字段移至吸气剂,然后错误消失.

Move all your annotations from the fields to the getters, then the error goes away.

这篇关于我可以在@Embedded中使用@ElementCollection吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆