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

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

问题描述

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

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天全站免登陆