JPA 继承:数据库设计,其中一个表中的每一行对应于 Postgres 中的一个表



有一个名为 Product(id, name) 的表。产品中的每一行都是不同类型的产品。每个产品都有不同的信息数据。例如:"互联网"等产品将具有信息数据"当前计划","帐号","帐户状态"等。而像"保险"这样的产品将具有信息数据"收款人"、"受益人"、"总存款"、"总索赔"等。还有一张表叫做客户,就像这样。

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 页面上动态加载这些产品特定的字段。

你可以像这里提到的在Hibernate中使用java继承。 if(customer.getInternetProdInfo() instanceOf InternetProdInfo){ /* get the info for it*/ }在这里,我们可以有几个链接,您可以通过它不是一个简单的解决方案,它有自己的方面,还有更多。

  1. 文档 如果你有足够的时间
  2. 博客简短而有用
  3. 另一个博客简短而有用
  4. 一个有用的问题
  5. 另一个有用的问题
  6. https://stackoverflow.com
  7. 另一个问题

  8. 另一个简短的文档

    映射的超类没有为其定义单独的表(使用 @MappedSuperclass )。

    可以使用@AttributeOverride@AssociationOverride注释或相应的 XML 元素在此类子类中重写映射信息

相关内容