【发布时间】:2012-11-24 22:08:41
【问题描述】:
我有一个正在尝试实现的类,(类似于“Java Persistence with Hibernate”6.3.3 中的示例)
public class Property
{
...
@ElementCollection(fetch=FetchType.EAGER)
@CollectionId(
columns=@Column(name="Property_image_id"),
type=@Type(type="long"),
generator="native"
)
private Collection<FileAttachment> images = new ArrayList<FileAttachment>();
...
}
单元测试失败,抛出以下异常:
java.lang.ClassCastException: org.hibernate.id.IdentifierGeneratorHelper$2
cannot be cast to java.lang.Long
我不太确定“发电机”的最佳价值,我认为这会影响结果。
另外,当我开始工作时,FileAttachment 对象可以访问 Property_image_id 吗?以及如何将其分配给属性,因为它是在 Property 类中定义的?
我希望 Property_images 表有一个复合键 [Property_id-Image_index],其中每个新 Property_id 的 Image_index 从 1 开始,但我不知道如何使用 @ElementCollection 和 @CollectionId 来实现它发电机。也许我必须将 FileAttachment 作为 @Entity 而不是 @Embeddable,但 id 而不是作为它仅在 Property 类中使用的。
干杯! NFV
【问题讨论】: