0

I am trying to use Hibernate with hbm2ddl.auto set to validate so that Hibernate must respect my DB schema, instead of auto-generating tables around my Java classes.

A lot of my tables are so-called "lookup" or "reference" tables, that essentially consist of id, name and tag fields:

credit_card_types
    credit_card_type_id           Ex: "12"
    credit_card_type_name         Ex: "Visa"
    credit_card_type_tag          Ex: "VISA"

payment_types
    payment_type_id               Ex: "2"
    payment_type_name             Ex: "Google Checkout"
    payment_type_tag              EX: "GOOGLE_CHECKOUT"

etc.

I'd like to model these as follows:

public class BaseLookup {
    private Long id;
    private String name;
    private String tag;

    // ...getters, setters and ctors, etc.
}

public class CreditCardTypes extends BaseLookup {
    // .. perhaps a few other fields, methods, etc...
}

public class PaymentTypes extends BaseLookup {
    // .. perhaps a few other fields, methods, etc...
}

The problem is I don't want Hibernate to search for (validate against) a lookups table: I want it to validate aginst 2 lookup tables called credit_card_types and payment_types respectively.

What annotations/configs do I need to allow this type of Java inheritance but to only create my 2 lookup tables (and not 3)? Thanks in advance!

4

1 回答 1

1

对于您的基类,您将添加以下注释:

@MappedSuperclass
public class BaseLookup implements Serializable {

    @Id
    @Column (name="ID")
    private Long id;

    @Column (name="NAME")
    private String name;

    @Column
    private String tag;

    // ...getters, setters and ctors, etc.
}

对于您的孩子课程,您将执行以下操作:

@Table(name = "CREDITCARDTYPES")
public class CreditCardTypes extends BaseLookup {
   // .. perhaps a few other fields, methods, etc...

   @Coulmn (name="FIELD1_COLUMN_NAME")
   private String field1;
}

你可以用同样的方式做第二堂课:

@Table(name = "PAYMENTTYPES")
public class PaymentTypes extends BaseLookup {
   // .. perhaps a few other fields, methods, etc...

   @Coulmn (name="FIELD2_COLUMN_NAME")
   private String field2;
}
于 2013-09-09T19:13:49.660 回答