我有一個簡短的問題。我正在嘗試按如下方式制作資料:Auth(email,pass) -> Client(name, surname, ...) Auth(email,pass) -> RepairShop(nip, location, ...)
認證
@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;
存盤庫看起來像 AuthRepository
public interface AuthRepository extends JpaRepository<Long, Auth>{
}
客戶端存盤庫
public interface ClientRepository extends JpaRepository<Client,Long> {
}
維修店倉庫
public interface RepairShopRepository extends JpaRepository<RepairShop, Long> {
}
Auth 不能是抽象類,這只是在我的專案中具有良好 auth 的表(目前我只是手動添加了帶有一些觸發器的表以將資料從 Client 和 RepairShop 寫入 Auth,但我正在尋找更好的解決方案)
我的目標是擁有像 Auth idauth 電子郵件傳遞角色這樣的資料庫
客戶 id 客戶名 姓 idauth
RepairShop idrepairShop nip location idauth
甚至可以像下面那樣做嗎?或者這只是個壞主意,我應該只使用一對一的關系,甚至不要像這樣玩。或者也許在資料庫結構中有一些更好的解決方案。同樣重要的是讓它輕松地與 Angular App 一起使用,以便輕松地訪問從 auth 的資料記錄到管理來自 Client/RepairShop 表的其他屬性
我認為這里的問題在于我的存盤庫配置,但我不確定。
你怎么看待這件事?
uj5u.com熱心網友回復:
如果您從 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;
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/365379.html
