我有两个实体Student和Intsructor。我想通过两个实体的Dao实现来实现Dao接口。我设置了一个类用户作为学生的家长一个讲师:
@MappedSuperclass
public abstract class User {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="id")
private int id;
@Column(name = "name")
private String firstName;
@Column(name = "password")
private String password;
@Column(name = "email")
private String email;
getters and setters ...
}
和儿童。学生
@Entity
@Table(name = "student", schema="els")
public class Student extends User {
@Column(name="achiev")
private String achievment;
public Student() {
}
getter and setter for achievment
}
和教练
@Entity
@Table(name = "instructor", schema="els")
public class Instructor extends User {
@Column(name = "reputation")
private int reputation;
public Instructor() {
}
public int getReputation() {
return reputation;
}
public void setReputation(int reputation) {
this.reputation = reputation;
}
}
Dao接口:
public interface DAO {
List<User> getAllUsers();
...
}
两个实体的DAO实现。
但是有一个问题。我无法保存每个实体的所有属性,因为在User类中我只有其中的一些属性。学生和教员除了继承财产外,还拥有自己的财产。
如何实现DAO和实体。在这种情况下,什么是好的做法?
感谢
您可以尝试使用泛型。
public interface GenericDAO<T> {
List<T> getAll();
}
在需要时,您可以扩展和定义特定的功能。
public interface UserDAO extends GenericDAO<User> {
User getAllWithAvatar();
}
希望这能有所帮助!