JPA Embeddable类可以包含对象引用吗? [英] Can JPA Embeddable class contain an object reference?

查看:51
本文介绍了JPA Embeddable类可以包含对象引用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个类嵌入另一个包含对象引用的类.我总是得到

I'm trying to embed a class to another which contains object reference. I always get

org.hibernate.MappingException: Could not determine type for: ...

例外.我的问题是,有什么方法可以嵌入包含对象引用的类,或者可嵌入的类仅存储基元.

exception. My question is there any way I can embed a class which contains object references, or embeddable classes only stores primitives.

我已经尝试过@Target()注解,但没有帮助.

I'm already tried @Target() annotation but not helps.

推荐答案

是的,这是可能的.查看文档. /p>

Yes, this is possible. Look at the documentation.

@Embeddable
public static class Publisher {

    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    private Country country;

    //Getters and setters, equals and hashCode methods omitted for brevity
}


@Entity(name = "Book")
@AttributeOverrides({
    @AttributeOverride(
        name = "ebookPublisher.name",
        column = @Column(name = "ebook_publisher_name")
    ),
    @AttributeOverride(
        name = "paperBackPublisher.name",
        column = @Column(name = "paper_back_publisher_name")
    )
})
@AssociationOverrides({
    @AssociationOverride(
        name = "ebookPublisher.country",
        joinColumns = @JoinColumn(name = "ebook_publisher_country_id")
    ),
    @AssociationOverride(
        name = "paperBackPublisher.country",
        joinColumns = @JoinColumn(name = "paper_back_publisher_country_id")
    )
})
public static class Book {

    @Id
    @GeneratedValue
    private Long id;

    private Publisher ebookPublisher;

    private Publisher paperBackPublisher;

    //Getters and setters are omitted for brevity
}

这篇关于JPA Embeddable类可以包含对象引用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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