具有接口属性的JPA实体,是否可能? [英] JPA entity with a interface attribute, is it possible?

查看:117
本文介绍了具有接口属性的JPA实体,是否可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下实体:

@Entity
public class TestCaseStep implements JPAEntity<Integer> {

        ...

    @Column(name="STEP_NUMBER")
    private Integer stepNumber;

    @Enumerated(EnumType.STRING)
    @Column(name="ACTION")
    private Action action;

    **@ManyToOne
    @JoinColumn(name="connector")
    private ScriptItem connector;**

我的属性ScriptItem是其他3个类的接口。是否可以配置JPA在运行时执行中设置正确的类ID?

My attribute ScriptItem is a interface for 3 other classes. Is it possible to configure JPA to set the correct class id in runtime execution?

其他资源:

public interface ScriptItem {

    String getValue();
    ScriptItemType getType();
}

@Entity
@Table(name="DAT_FEED_XML")
public class FeedXml implements JPAEntity<Integer>, ScriptItem {
    ...
}

@Entity
@Table(name="DAT_DB_STMT")
public class DbStatement implements JPAEntity<Integer>, ScriptItem {
       ...
}

我应该使用哪些注释让JAP理解我要保存id 3个班级中的一个?

Which annotations should I use to let JAP understand that I want to save the id of one of the 3 classes?

提前致谢,

推荐答案

这是一个好主意但不幸的是,JPA不支持将接口直接映射为实体属性。

It is really a good idea but unfortunately directly mapping interfaces as an entity attribute is not supported by JPA.

您只能映射直接用<$ c注释的顶级类$ C> @Entity 。这个顶级类可以实现一个接口。

You can only map top level classes directly annotated with @Entity. This top level class may implement an interface though.

此功能已被请求并且讨论了很长一段时间。

This feature has been requested and discussed for a long time.

另请参阅这个这个

根据您要完成的工作,使用每个表的策略 @Inheritance 注释可能是一种选择。

Depending on what you're trying to accomplish, @Inheritance annotation with table-per-class strategy could be an option.

我希望它有所帮助。

这篇关于具有接口属性的JPA实体,是否可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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