我正在尝试使用Hibernate并将hbm2ddl.auto
设置为validate
,以便Hibernate必须尊重我的数据库架构,而不是在我的Java类周围自动生成表。
我的很多表都是所谓的“查找”或“参考”表,基本上由id
,name
和tag
字段组成:
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.
我想将这些建模如下:
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...
}
问题是我不希望Hibernate搜索(验证)lookups
表:我希望它分别验证名为credit_card_types
和payment_types
的aginst 2查找表。
我需要哪些注释/配置来允许这种类型的Java继承,但只创建我的2个查找表(而不是3)?提前致谢!
答案 0 :(得分: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;
}