【问题标题】:Initialize dependent object in simple unidirectional OneToOne mapping在简单的单向 OneToOne 映射中初始化依赖对象
【发布时间】:2011-09-15 08:04:27
【问题描述】:

我对简单的单向映射有疑问。这是我的实体:

@Entity
public class Account extends UUIDBase {
    private Profile profile;

    @OneToOne(cascade = CascadeType.ALL, optional = false)
    public Profile getProfile() {
        return profile;
    }

    public void setProfile(Profile profile) {
        this.profile = profile;
    }
}

@Entity
public class Profile extends UUIDBase {
   ...
}

必须为每个帐户分配一个配置文件。该帐户应该是映射的拥有方。初始化dependendt Profile属性的最佳位置在哪里?我尝试在 Account 实体的构造函数中初始化配置文件,但这不起作用。

【问题讨论】:

    标签: jpa mapping one-to-one


    【解决方案1】:

    您可以在构造函数中对其进行初始化,但在应用程序或工厂中设置它可能会更好。

    什么不起作用,你得到什么错误?

    【讨论】:

    • 好的,谢谢您的回答。我认为在工厂中初始化引用确实是更好的解决方案。构造函数中的初始化也将起作用。我的逻辑有错误。
    猜你喜欢
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多