簡體   English   中英

如何正確管理 Hibernate 中的 2 個繼承的實體

[英]How to correctly manage Entities in Hibernate that 2 of them inherit after one

我有一個快速的問題。 我正在嘗試制作如下數據: 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 不能是抽象的 class,這是唯一在我的項目中具有良好身份驗證的表(目前我剛剛手動添加了帶有一些觸發器的表,以將數據從 Client 和 RepairShop 寫入 Auth,但我正在尋找更好的解決方案)

我的目標是擁有像 Auth idauth email 這樣的數據庫傳遞角色

客戶 idclient 姓名 idauth

RepairShop idrepairShop nip location idauth

甚至可以像下面那樣做嗎? 或者這只是個壞主意,我應該只使用一對一的關系,甚至不要這樣玩。 或者也許在數據庫結構中有一些更好的解決方案。 此外,重要的是讓它輕松與 Angular 應用程序一起使用,以便輕松訪問從身份驗證的數據記錄到管理客戶端/維修店表中的其他屬性

我認為這里的問題在於我的存儲庫配置,但我不確定。

你怎么看待這件事?

如果您從 OOP 的角度查看 client->auth 和 repairship -> auth 關系,它是一個有關系而不是關系。 所以,onetoone是map的最佳方式吧。

@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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM