java 如何使用 jpa 映射抽象集合?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9873547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to map an abstract collection with jpa?
提问by emt14
I am trying to work out if it is possible get JPA to persist an abstract collection with concrete implementations.
我试图弄清楚是否有可能让 JPA 用具体实现来持久化抽象集合。
So far my code looks like this:
到目前为止,我的代码如下所示:
@Entity
public class Report extends Model {
@OneToMany(mappedBy = "report",fetch=FetchType.EAGER)
public Set<Item> items;
}
@MappedSuperclass
public abstract class OpsItem extends Model {
@ManyToOne
public RetailOpsBranch report;
}
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class AItem extends OpsItem {
...
}
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class BItem extends OpsItem {
...
}
But I keep stumbling on the mapping error below and I don't really know if this is feasible?
但是我一直在下面的映射错误上磕磕绊绊,我真的不知道这是否可行?
JPA error
A JPA error occurred (Unable to build EntityManagerFactory): Use of @OneToMany or
@ManyToMany targeting an unmapped class: models.Report.items[models.OpsItem]
UPDATE
更新
I don't think the problem is with the abstract class but the the @MappedSuperClassannotation. It looks like jpa does not like to map the one to many relationship with a @MappedSuperClass. If I change the abstract class into a concrete class I have the same error.
我认为问题不在于抽象类,而在于@MappedSuperClass注释。看起来 jpa 不喜欢将一对多关系与@MappedSuperClass 映射。如果我将抽象类更改为具体类,则会出现相同的错误。
If I then change to @Entityannotation this seem to work with both the abstract and concrete class.
如果我然后更改为@Entity注释,这似乎适用于抽象类和具体类。
This seems a bit strange to map an abstract class with @Entity. I'm I missing anything?
用@Entity映射抽象类似乎有点奇怪。我错过了什么吗?
SOLUTION
解决方案
Managed to figure it out with the help from rhinds. Two points to note:
在 rhind 的帮助下设法弄清楚了。需要注意的两点:
1) the abstract class needs to be annotated with @Entity and inheritance strategy of table per class for the subclasses to have their own table.
1)抽象类需要用@Entity和每个类的表的继承策略进行注释,以便子类拥有自己的表。
2) Identity Id generation will not work in this scenario and I had to use Table generation type.
2) Identity Id 生成在这种情况下不起作用,我不得不使用 Table 生成类型。
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class OpsItem extends GenericModel {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
public Long id;
public String branchCode;
@ManyToOne
public Report report;
}
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class AItem extends OpsItem {
...
}
采纳答案by rhinds
Yep, it is possible. You should only have the MappedSuperClass on the top abstract class (that isnt going to be persisted in itself) and add Entity annotation on the implementation classes.
是的,这是可能的。您应该只在顶级抽象类上使用 MappedSuperClass(它本身不会持久化)并在实现类上添加实体注释。
Try changing it to something like this:
尝试将其更改为如下所示:
@MappedSuperclass
public abstract class OpsItem extends Model {
@ManyToOne
public RetailOpsBranch report;
}
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class AItem extends OpsItem {
...
}
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class BItem extends OpsItem {
...
}
Check this section of the hibernate docs for details on its use: http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e1168
查看 hibernate 文档的这一部分以获取有关其使用的详细信息:http: //docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/#d0e1168
UPDATE
更新
Sorry, completely missed the table per class bit. Hibernate does not support mapping of abstract objects for table per class (can only map List if all implementations are within a single SQL table, and the TABLE_PER_CLASS uses the "table per concrete class" strategy.
抱歉,完全错过了每个班级的表格。Hibernate 不支持按类为表映射抽象对象(如果所有实现都在单个 SQL 表中,则只能映射 List,并且 TABLE_PER_CLASS 使用“每个具体类的表”策略。
Details of limitations and the strategies here: http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#inheritance-limitations
此处的限制和策略的详细信息:http: //docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#inheritance-limitations