开发者

Issue With @OneToMany and Abstraction in JPA

I have an issue with one-to-many relationship. I have an abstract class Artifact.java. This is not mapped to a table. There are other concrete classes extending from this, and they are mapped to different tables. Now I have another class, mapped to a table, and this class can have a collection of any of these classes, i.e. collection of type Artifact.java. I need to map this using JPA, and have done it as follows.

@OneToMany(mappedBy="artifactId",targetEntity=Artifact_Item.class,
        fetch=FetchType.EAGER, cascade=CascadeType.ALL)
private List<Artifact_Item> artifactItemList;

This results in the following exception.

org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: dao.model.Artifact.artifactItemList[dao.model.Artifact_Item]

Can anyone please help me solve this issue?

Thanks.

Alright, I am rephrasing the question. I have a class, Artifact.java, which is mapped to table Artifact. Now each artifact can have multiple sub items (one-to-many), which aren't related in anyway. So, we decided to have an abstract class ArtifactItem.java, so that all 开发者_如何学编程the sub items can extend this. Thus, Artifact has List of ArtifactItems.

ArtifactItem.java, and is not mapped to table. This contains 2 elements, an id and and artifactId, used as the foreign key. Id has been marked as @GeneratedValue(strategy = GenerationType.AUTO) and artifactId as @ManyToOne(targetEntity = Artifact.class) @JoinColumn(name = "artifact_id", referencedColumnName = "id"). And the class has been annotated as @MappedSuperclass.

There are numerous sub-items, extending ArtifactItem. Each has its own table, eg, ArtifactType1.java has its own fields and is mapped to ArtifactType1 table and so on.

Now, since Artifact.java's list needs to be mapped to ArtifactItem, as the list is a collection of ArtifactItems. But when I try to map it in the manner mentioned above, I am getting the exception given above.

Hope its clearer now :).

Thanks.


Use @MappedSuperclass on ArtifactItem (get rid of the underscore)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜