문제

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!

도움이 되었습니까?

해결책

For your base class you would add the following annotation:

@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.
}

And for your children classes you would do the following:

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

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

And you could do the second class in the same manner:

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

   @Coulmn (name="FIELD2_COLUMN_NAME")
   private String field2;
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top