Hibernate查找表/类型的注释? [英] Annotations for Hibernate lookup table/type?

查看:102
本文介绍了Hibernate查找表/类型的注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将Hibernate与 hbm2ddl.auto 设置为 validate ,以便Hibernate必须尊重我的数据库模式,而不是围绕我的Java类自动生成表。



我的很多表是所谓的lookup或reference表,它们基本上由 id name tag 字段:

  credit_card_types 
credit_card_type_id例如:12
credit_card_type_name例如:Visa
credit_card_type_tag例如:VISA

payment_types
payment_type_id例如:2
payment_type_name例如:Google Checkout
payment_type_tag EX:GOOGLE_CHECKOUT


我想对这些模型进行建模,如下所示:

  public class BaseLookup {
private Long id;
私人字符串名称;
私人字符串标签;

// ... getters,setters和ctors等
}

公共类CreditCardTypes扩展BaseLookup {
// ..也许是a其他几个领域,方法等...
}

公共类PaymentTypes扩展BaseLookup {
// ..也许还有其他一些字段,方法等...
}

问题是我不希望Hibernate搜索(验证) lookups table:我希望它能够验证名为 credit_card_types payment_types 的aginst 2查找表。 code>分别。

我需要哪些注释/配置来允许这种类型的Java继承,但仅创建我的2个查找表(而不是3)?提前致谢!

解决方案

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

  @MappedSuperclass 
public class BaseLookup实现Serializable {

@Id
@Col umn(name =ID)
私人长ID;

@Column(name =NAME)
私人字符串名称;

@Column
私人字符串标签;

// ... getters,setters和ctors等
}

对于您的子类,您可以执行以下操作:

  @Table(name =CREDITCARDTYPES)
public class CreditCardTypes扩展BaseLookup {
// ..也许还有其他一些字段,方法等...

@Coulmn(name =FIELD1_COLUMN_NAME)
私人字符串field1;
}

你可以用同样的方式完成第二课:

  @Table(name =PAYMENTTYPES)
public class PaymentTypes extends BaseLookup {
// ..也许一些其他字段,方法等...

@Coulmn(name =FIELD2_COLUMN_NAME)
private String field2;
}


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;
}

这篇关于Hibernate查找表/类型的注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆