开发者

How to get inheritance to work in my model layer?

开发者 https://www.devze.com 2023-03-24 05:46 出处:网络
I know that you should extend Model to get all the jpa functionality. However, java does not allow multiple inheritance... Therefore, I would like to know how to architect in play the following design

I know that you should extend Model to get all the jpa functionality. However, java does not allow multiple inheritance... Therefore, I would like to know how to architect in play the following design:

public class Person extends Model {
}

// s开发者_StackOverflow中文版hould be able to extend Person
public class Doctor extends Model {
}

// should be able to extend Person     
public class Patient extends Model {
}

public class Item extends Model {
    // Doens't matter whether it's a Doctor or a Patient
    @ManyToMany
    public List<Person> owners;
}

Thanks in advance.


Your Doctor and Patient objects should inherit from Person. Java does not allow multiple inheritance but you can extend children objects with Person which inherits model already.


public class Person extends Model {
}

public class Doctor extends Person {
}

public class Patient extends Person{
}

public class Item extends Model {
    @ManyToMany
    public List owners;
}
 

What you cannot do in Java is:



public class Doctor extends Person, Model {
}


0

精彩评论

暂无评论...
验证码 换一张
取 消