将注释应用于从 @MappedSuperclass 继承的字段

发布于 2024-09-25 17:20:11 字数 325 浏览 11 评论 0原文

具有:

@MappedSuperclass
class Superclass {

    @Id
    @Column(name = "id")
    protected long id;

    @Column(name="field")
    private long field;

}

以及

@Entity
class Subclass extends Superclass {

}

如何在子类中使用 @GenerateValue 注释继承的 id 和使用 @Index 的字段?

Has:

@MappedSuperclass
class Superclass {

    @Id
    @Column(name = "id")
    protected long id;

    @Column(name="field")
    private long field;

}

and

@Entity
class Subclass extends Superclass {

}

How to annotate inherited id with @GeneratedValue and field with @Index within Subclass?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不语却知心 2024-10-02 17:20:11

如何在子类中使用@GenerateValue注释继承的id并使用@Index注释继承的字段?

AFAIK,你不能。您可以做的是使用 AttributeOverrideAssociationOverride 注释覆盖属性和关联(即更改列或连接列)。但你不能完全按照你的要求去做。

对于 GenerateValue,如果您不想在映射的超类中声明它,请考虑使用 XML 映射来覆盖该策略。

对于 Index (顺便说一句,这不是标准注释),您实际上是否尝试使用 Hibernate 的 Table 注释在表级别声明它(我假设你正在使用休眠)?

@Table(appliesTo="tableName",indexes = { @Index(name="index1",columnNames=
    {“列1”,“列2”} ) } ) 

创建定义的索引
表 tableName 的列。

参考

How to annotate inherited id with @GeneratedValue and field with @Index within Subclass?

AFAIK, you can't. What you can do is overriding attributes and associations (i.e. change the column or join column) using the AttributeOverride and AssociationOverride annotations. But you can't do exactly what you're asking.

For the GeneratedValue, consider using XML mapping to override the strategy if you don't want to declare it in the mapped superclass.

For the Index (which is not a standard annotation by the way), did you actually try to declare it at the table level using Hibernate's Table annotation instead (I'm assuming you're using Hibernate)?

@Table(appliesTo="tableName", indexes = { @Index(name="index1", columnNames=
    {"column1", "column2"} ) } ) 

creates the defined indexes on the
columns of table tableName.

References

要走就滚别墨迹 2024-10-02 17:20:11

至于@GenerateValue,可以这样做:

@MappedSuperclass
class Superclass {

    @Id
    @Column(name = "id")
    @GeneratedValue(generator = "id_generator")
    protected long id;

    @Column(name = "field")
    private long field;

}
@Entity
@SequenceGenerator(name = "id_generator", sequenceName = "id_seq")
class Subclass extends Superclass {

}

As for @GeneratedValue, it is possible to do like this:

@MappedSuperclass
class Superclass {

    @Id
    @Column(name = "id")
    @GeneratedValue(generator = "id_generator")
    protected long id;

    @Column(name = "field")
    private long field;

}
@Entity
@SequenceGenerator(name = "id_generator", sequenceName = "id_seq")
class Subclass extends Superclass {

}
白衬杉格子梦 2024-10-02 17:20:11

如果您将注释应用到访问器方法,您也许能够做到这一点。 (我还没有尝试过,所以我不能保证它会起作用。)

@MappedSuperclass
public class Superclass {

    @Id
    @Column(name = "id")
    public long getId() {
        return id;
    }

@Entity
public class Subclass extends Superclass {

    @GeneratedValue
    public long getId() {
        return super.getId();
    }

You might be able to do this if you apply the annotations to the accessor methods instead. (I haven't tried this, so I can't guarantee that it'll work.)

@MappedSuperclass
public class Superclass {

    @Id
    @Column(name = "id")
    public long getId() {
        return id;
    }

.

@Entity
public class Subclass extends Superclass {

    @GeneratedValue
    public long getId() {
        return super.getId();
    }
哆兒滾 2024-10-02 17:20:11

以防万一其他人搜索此内容,我使用了以下代码,这会增加一些开销,但仅处理字段注释不应添加那么多:

    private List<Field> getAllFields() {
    List<Field> fieldList = new ArrayList<Field>();

    // Add all fields from the current class
    fieldList.addAll(Arrays.asList(mElement.getClass().getDeclaredFields()));

    // Use an index to iterate over mElement's parent types
    Class clazz = mElement.getClass();

    // Get any fields from the parent class(es)
    while (clazz.getSuperclass() != null) {
        fieldList.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields()));
        // Set it to that parent class
        clazz = clazz.getSuperclass();
    }

    return fieldList;
}

返回的列表将包含所有带有 的父类和子类的所有字段mElement 是您要从中搜索注释的对象。希望这有帮助。

Just in case anyone else searches for this, I used the following code which adds in some overhead, but for processing Field annotations only shouldn't add that much:

    private List<Field> getAllFields() {
    List<Field> fieldList = new ArrayList<Field>();

    // Add all fields from the current class
    fieldList.addAll(Arrays.asList(mElement.getClass().getDeclaredFields()));

    // Use an index to iterate over mElement's parent types
    Class clazz = mElement.getClass();

    // Get any fields from the parent class(es)
    while (clazz.getSuperclass() != null) {
        fieldList.addAll(Arrays.asList(clazz.getSuperclass().getDeclaredFields()));
        // Set it to that parent class
        clazz = clazz.getSuperclass();
    }

    return fieldList;
}

The returned list would contain all fields for all parent and child classes with mElement being the object you are searching for annotations from. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文