Kotlin JPA:如何处理@OneToOne初始化

时间:2017-01-22 12:07:07

标签: kotlin

在Java中,我这样做:

public class User {

    //one-to-one
    private Profile profile = new Profile(this);        

}

public class Profile {

    private User user;

    public Profile(User user) {
        user.setProfile(this);
        this.user = user;
    }

}

所以我只是在创建一个用户,而个人资料正在自动创建 。现在 我无法理解如何使用Kotlin的主要构造函数来实现它:

class User(

    var profile: Profile = Profile(this) //error, since `this` is not in that context

)

class Profile (

    var user: User?

) {

    constructor(user: User): this(user)

}

你会怎么处理这个?

1 个答案:

答案 0 :(得分:2)

这有效:

\"<none>\"