Hibernate JPA场景解决?
Hibernate JPA scenario to solve?
有一位 class 顾问。顾问可以有多种经验,如受薪、个体户、自由职业者。对于每种类型的体验,在数据库中保存不同的数据。
受薪:
总经验
公司名称
经验年数
Offer/Reveling字母Link
个体经营者:
公司名称
总经验
CIN_Number
GST_Number
公司证书LinkUrl
自由职业者:
总经验
一个用户可以有不止一种职业类型的经验,比如顾问既是受薪者又是自由职业者,或者是个体经营者加受薪者和自由职业者。所以我很困惑如何为这种类型的用例制作@Entity class。
My Solution
@Entity
class Consultant{
@Id
int id;
@OneToOne
Salaried semp;
@OneToOne
SelfEmployed selfemp;
@OneToOne
Freelancer femp;
}
但我认为这不是好的做法,因为它会导致数据库中出现许多空字段。
任何更好的解决方案
你可以用两个 类 consultant
和 profession
(id,name ) 和关系 OneToMany
,ManyToOne
顾问实体
@Entity
class Consultant{
@Id
private int id;
@OneToMany(mappedBy = "consultant",cascade = CascadeType.All)
List<ConsultantProfession> cp;
}
职业实体
@Entity
class Profession{
@Id
private int id;
private String name;
@OneToMany(mappedBy = "profession", cascade = CascadeType.All)
private List<ConsultantProfession> cp;
}
ConsultantProfession 的实体
@Entity
@Table(name="consultant_profession")
public class ConsultantProfession{
@Id
private int id;
// this is the link to the consultant class
@ManyToOne
@JoinColumn(name="consultant_id")
private Consultant consultant; // this name is specified in the class seeing patients as value of parameter `mappedBy`
// this is the link to the profession class
@ManyToOne
@JoinColumn(name="profession_id")
private Profession profession;
}
我觉得你的做法很好。 @OneToOne
字段默认是可选的,因此可以为空。这意味着相应的 table 中不会有一行,因此在 Consultant table.
中每行最多只有两个空值
如果您真的很关心数据库中的空值,那么您可以用另一种方式映射关系,因此:
@Entity
class Consultant{
@Id
int id;
@OneToOne(mappedBy = "consultant")
Salaried semp;
@OneToOne(mappedBy = "consultant")
SelfEmployed selfemp;
@OneToOne(mappedBy = "consultant")
Freelancer femp;
}
这样,如果受薪 table 中没有与顾问相关的行,semp
字段在 Consultant
对象中将为空。
有一位 class 顾问。顾问可以有多种经验,如受薪、个体户、自由职业者。对于每种类型的体验,在数据库中保存不同的数据。
受薪:
总经验
公司名称
经验年数
Offer/Reveling字母Link
个体经营者:
公司名称
总经验
CIN_Number
GST_Number
公司证书LinkUrl
自由职业者:
总经验
一个用户可以有不止一种职业类型的经验,比如顾问既是受薪者又是自由职业者,或者是个体经营者加受薪者和自由职业者。所以我很困惑如何为这种类型的用例制作@Entity class。
My Solution
@Entity
class Consultant{
@Id
int id;
@OneToOne
Salaried semp;
@OneToOne
SelfEmployed selfemp;
@OneToOne
Freelancer femp;
}
但我认为这不是好的做法,因为它会导致数据库中出现许多空字段。
任何更好的解决方案
你可以用两个 类 consultant
和 profession
(id,name ) 和关系 OneToMany
,ManyToOne
顾问实体
@Entity
class Consultant{
@Id
private int id;
@OneToMany(mappedBy = "consultant",cascade = CascadeType.All)
List<ConsultantProfession> cp;
}
职业实体
@Entity
class Profession{
@Id
private int id;
private String name;
@OneToMany(mappedBy = "profession", cascade = CascadeType.All)
private List<ConsultantProfession> cp;
}
ConsultantProfession 的实体
@Entity
@Table(name="consultant_profession")
public class ConsultantProfession{
@Id
private int id;
// this is the link to the consultant class
@ManyToOne
@JoinColumn(name="consultant_id")
private Consultant consultant; // this name is specified in the class seeing patients as value of parameter `mappedBy`
// this is the link to the profession class
@ManyToOne
@JoinColumn(name="profession_id")
private Profession profession;
}
我觉得你的做法很好。 @OneToOne
字段默认是可选的,因此可以为空。这意味着相应的 table 中不会有一行,因此在 Consultant table.
如果您真的很关心数据库中的空值,那么您可以用另一种方式映射关系,因此:
@Entity
class Consultant{
@Id
int id;
@OneToOne(mappedBy = "consultant")
Salaried semp;
@OneToOne(mappedBy = "consultant")
SelfEmployed selfemp;
@OneToOne(mappedBy = "consultant")
Freelancer femp;
}
这样,如果受薪 table 中没有与顾问相关的行,semp
字段在 Consultant
对象中将为空。