2017-10-13 66 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