如何在字段级别创建元注释? [英] How to create meta annotations on field level?

查看:79
本文介绍了如何在字段级别创建元注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个带有注释的休眠类:

I have this hibernate class with annotations:

@Entity
public class SimponsFamily{

  @Id
  @TableGenerator(name = ENTITY_ID_GENERATOR,
                table = ENTITY_ID_GENERATOR_TABLE,
                pkColumnName = ENTITY_ID_GENERATOR_TABLE_PK_COLUMN_NAME,
                valueColumnName = ENTITY_ID_GENERATOR_TABLE_VALUE_COLUMN_NAME)
  @GeneratedValue(strategy = GenerationType.TABLE, generator = ENTITY_ID_GENERATOR)
  private long id;

  ...
}

由于我不会这样注释类中的每个 id 字段,因此我尝试创建自定义注释:

Since I don´t won´t to annotate every id field of my classes that way, I tried to create a custom anotation:

@TableGenerator(name = ENTITY_ID_GENERATOR,
            table = ENTITY_ID_GENERATOR_TABLE,
            pkColumnName = ENTITY_ID_GENERATOR_TABLE_PK_COLUMN_NAME,
            valueColumnName = ENTITY_ID_GENERATOR_TABLE_VALUE_COLUMN_NAME)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface EntityId {

    @GeneratedValue(strategy = GenerationType.TABLE, generator = ENTITY_ID_GENERATOR)
    public int generator() default 0;

    @Id
    public long id() default 0;
}

以便我可以在课堂上使用此批注:

so that I can use this annotation in my class:

 @Entity
 public class SimponsFamily{


 @EntityId
 private long id;

  ...
}

我确实必须在字段级别上编写@Id@GeneratedValue注释,因为它们不支持TYPE RetentionPolicy.此解决方案似乎有效.

I do have to write the @Id and the @GeneratedValue annotions on field level since they do not support the TYPE RetentionPolicy. This solutions seems to work.

我的问题:

  • 自定义注释(和值)中的字段级别注释如何传输到我使用的EntityId注释?

  • How are the field level annotations in my custom annotations(and values) transferred to my usage of EntityId annotation?

我在自定义批注中设置的默认值如何?由于我未在用法时指定属性,因此使用了默认值吗?

What about the default values which I set in my custom annotation, are they used since I do not specify attributes at the usage?

在注释中在字段级别使用注释是一种首选方法吗?

It is a preferred way to use annotations on field level in annotations?

推荐答案

我想我可以提出您的第三个问题.

I think I can aswer your third question.

做您想要做的一种常见方法(避免重复的ID映射)是创建一个通用的超类,该超类保存带注释的idversion(用于乐观锁定)字段,然后让所有持久对象扩展此超类. . 为确保超类本身不被视为实体,必须使用@MappedSuperclass对其进行注释.

One common way to do what you want (avoid duplicating ID mapping) is to create a common superclass that holds the annotated id and version (for optimistic locking) fields, and then have all persistent objects extend this superclass. To ensure the superclass is not considered an Entity on its own, it must be annotated with @MappedSuperclass.

这里是一个示例(很抱歉输入错误,我现在没有IDE):

Here is a sample (sorry for typos, I don't have an IDE at hand right now) :

@MappedSuperclass
public class PersistentObject {

    @Id // Put all your ID mapping here
    private Long id;

    @Version
    private Long version;

}

@Entity
public class SimpsonsFamily extends PersistentObject {        
    // Other SimpsonFamily-specific fields here, with their mappings    
}

这篇关于如何在字段级别创建元注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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