如何正确管理 Hibernate 中的实体,其中 2 个在一个之后继承
How to correctly manage Entities in Hibernate that 2 of them inherit after one
我有一个简短的问题。我正在尝试制作如下数据:
身份验证(电子邮件,密码)-> 客户端(姓名,姓氏,...)
身份验证(电子邮件,密码)-> RepairShop(nip,位置,...)
授权
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Auth {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long authid;
客户
@Entity
public class Client extends Auth{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idClient;
...
@ManyToOne
private RepairShop repairShop;
维修店
@Entity
public class RepairShop extends Auth{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idRepairShop;
...
@OneToMany(cascade = CascadeType.ALL, mappedBy = "repairShop")
private Set<Client> clients;
存储库看起来像
授权库
public interface AuthRepository extends JpaRepository<Long, Auth>{
}
客户端存储库
public interface ClientRepository extends JpaRepository<Client,Long> {
}
RepairShopRepository
public interface RepairShopRepository extends JpaRepository<RepairShop, Long> {
}
Auth 不能是抽象的 class,这只是 table 在我的项目中有好的 auth
(目前我只是手动添加了 table 和一些触发器来将数据从 Client 和 RepairShop 写入 Auth,但我正在寻找更好的解决方案)
我的目标是让数据库像
授权
身份验证
电子邮件
经过
角色
客户端
客户端
姓名
姓
idauth
修理厂
idrepair商店
掐
地点
idauth
甚至可以像下面那样做吗?或者这只是个坏主意,我应该只使用一对一的关系,甚至不要这样玩。或者也许在数据库结构方面有一些更好的解决方案。同样重要的是让它轻松地与 Angular 应用程序一起使用,以便轻松地从使用 auth 的数据进行日志记录到管理来自 Client/RepairShop tables
的其他属性。
我认为问题出在我的存储库配置上,但我不确定。
你怎么看?
如果您从 OOP 的角度看 client->auth and repairship -> auth 关系,它是一种 has-a 关系,而不是 is-a 关系。所以,onetoone是最好的映射方式。
@Entity
public class Client extends Auth{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idClient;
...
@ManyToOne
private RepairShop repairShop;
@OneToOne
@JoinColumn(name="idauth")
private Auth auth;
修理厂
@Entity
public class RepairShop extends Auth{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idRepairShop;
...
@OneToMany(cascade = CascadeType.ALL, mappedBy = "repairShop")
private Set<Client> clients;
@OneToOne
@JoinColumn(name="idauth")
private Auth auth;
我有一个简短的问题。我正在尝试制作如下数据: 身份验证(电子邮件,密码)-> 客户端(姓名,姓氏,...) 身份验证(电子邮件,密码)-> RepairShop(nip,位置,...)
授权
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Auth {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long authid;
客户
@Entity
public class Client extends Auth{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idClient;
...
@ManyToOne
private RepairShop repairShop;
维修店
@Entity
public class RepairShop extends Auth{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idRepairShop;
...
@OneToMany(cascade = CascadeType.ALL, mappedBy = "repairShop")
private Set<Client> clients;
存储库看起来像 授权库
public interface AuthRepository extends JpaRepository<Long, Auth>{
}
客户端存储库
public interface ClientRepository extends JpaRepository<Client,Long> {
}
RepairShopRepository
public interface RepairShopRepository extends JpaRepository<RepairShop, Long> {
}
Auth 不能是抽象的 class,这只是 table 在我的项目中有好的 auth (目前我只是手动添加了 table 和一些触发器来将数据从 Client 和 RepairShop 写入 Auth,但我正在寻找更好的解决方案)
我的目标是让数据库像 授权 身份验证 电子邮件 经过 角色
客户端 客户端 姓名 姓 idauth
修理厂 idrepair商店 掐 地点 idauth
甚至可以像下面那样做吗?或者这只是个坏主意,我应该只使用一对一的关系,甚至不要这样玩。或者也许在数据库结构方面有一些更好的解决方案。同样重要的是让它轻松地与 Angular 应用程序一起使用,以便轻松地从使用 auth 的数据进行日志记录到管理来自 Client/RepairShop tables
的其他属性。我认为问题出在我的存储库配置上,但我不确定。
你怎么看?
如果您从 OOP 的角度看 client->auth and repairship -> auth 关系,它是一种 has-a 关系,而不是 is-a 关系。所以,onetoone是最好的映射方式。
@Entity
public class Client extends Auth{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idClient;
...
@ManyToOne
private RepairShop repairShop;
@OneToOne
@JoinColumn(name="idauth")
private Auth auth;
修理厂
@Entity
public class RepairShop extends Auth{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idRepairShop;
...
@OneToMany(cascade = CascadeType.ALL, mappedBy = "repairShop")
private Set<Client> clients;
@OneToOne
@JoinColumn(name="idauth")
private Auth auth;