2017-10-13 64 views
-1

有一個名爲Product(id,name)的表。產品中的每一行都是不同類型的產品。每個產品都有不同的信息數據。例如:像「互聯網」這樣的產品將具有「當前計劃」,「賬戶號碼」,「賬戶狀態」等信息數據。而像「保險」這樣的產品將擁有「收款人」,「受益人」,「存款總額」,「總額索賠」等信息數據。 還有另一個叫做Customer的表。JPA繼承:一個表中的每行對應於Postgres中的表的DB設計

CREATE TABLE customer 
(
    id serial NOT NULL, 
    first_name text NOT NULL, 
    last_name text NOT NULL, 
    street_add text NOT NULL, 
    city text NOT NULL, 
    state text NOT NULL, 
    zip text NOT NULL, 
    phone text NOT NULL, 
    ssn text, 
    customer_since timestamp without time zone, 
    product_id integer NOT NULL, 
    intro_audio text NOT NULL, 
    search_params text, 
    verification_params text, 
    CONSTRAINT customer_pkey PRIMARY KEY (id), 
    CONSTRAINT fk_product_id FOREIGN KEY (product_id) 
     REFERENCES product (id) MATCH SIMPLE 
     ON UPDATE NO ACTION ON DELETE NO ACTION 
) 

我想創建一個表中的每個產品: 例如互聯網:

CREATE TABLE internet_product_info 
(
    id serial NOT NULL, 
    customer_id integer NOT NULL, 
    current_plan text NOT NULL, 
    acc_type text NOT NULL, 
    acc_no text NOT NULL, 
    CONSTRAINT internet_product_info_pkey PRIMARY KEY (id), 
    CONSTRAINT fk_customer_id FOREIGN KEY (customer_id) 
     REFERENCES customer(id) MATCH SIMPLE 
     ON UPDATE NO ACTION ON DELETE NO ACTION 
) 

爲了保險:

CREATE TABLE insurance_product_info 
(
    id serial NOT NULL, 
    customer_id integer NOT NULL, 
    payee text NOT NULL, 
    beneficiary text NOT NULL, 
    total_deposits integer NOT NULL, 
    total_claims integer NOT NULL, 
    CONSTRAINT insurance_product_info_pkey PRIMARY KEY (id), 
    CONSTRAINT fk_customer_id FOREIGN KEY (customer_id) 
     REFERENCES customer(id) MATCH SIMPLE 
     ON UPDATE NO ACTION ON DELETE NO ACTION 
) 

我想問,如果這是一個正確的做法去尋找這樣的場景或者有更好的選擇。

在java中,我將檢索同類產品性能:

public class Customer implements Serializable{ 
    private int id; 
    private String firstName; 
    private String lastName; 
    private String streetAdd; 
    private String city; 
    private String state; 
    private String zip; 
    private String phone; 
    private String ssn; 
    private Date customerSince; 
    private Object internetProdInfo; 
    private Product product;  
} 

if(customer.getInternetProdInfo() instanceOf InternetProdInfo){ 
/* get the info for it*/ 
} 

此外,我怎麼能動態動態加載JSP頁面上這些產品的具體領域。

+0

覆蓋'約束fk_product_id外鍵(PRODUCT_ID)'列'product_id'不存在。有一個列'sim_product_id'(我認爲它不應該存在,或者) – joop

+0

我將sim_product_id重命名爲product_id – Deepika

回答

0

您可以在Hibernate中使用Java繼承,正如您在此處提到的。 if(customer.getInternetProdInfo() instanceOf InternetProdInfo){ /* get the info for it*/ } 在這裏,我們可以有幾個鏈接,你可以通過它不是一個簡單的解決方案,它有自己的方面,還有更多。

  1. Documentation If You have Plenty of Time
  2. Blog Short and Helpful
  3. Another Blog Short and Helpful
  4. A Helpful Question
  5. Another Helpful Question
  6. https://stackoverflow.com
  7. Another Question

  8. Another Short Doc

    映射超具有爲它定義(使用@MappedSuperclass)沒有單獨的表。

    映射信息可以在這些亞類通過使用@AttributeOverride@AssociationOverride註釋或相應的XML元素

+1

Shailesh Singh !!非常感謝..這正是我想要的.. – Deepika