Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue With @OneToMany and Abstraction in JPA
    text
    copied!<p>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.</p> <pre><code>@OneToMany(mappedBy="artifactId",targetEntity=Artifact_Item.class, fetch=FetchType.EAGER, cascade=CascadeType.ALL) private List&lt;Artifact_Item&gt; artifactItemList; </code></pre> <p>This results in the following exception.</p> <pre><code>org.hibernate.AnnotationException: Use of @OneToMany or @ManyToMany targeting an unmapped class: dao.model.Artifact.artifactItemList[dao.model.Artifact_Item] </code></pre> <p>Can anyone please help me solve this issue?</p> <p>Thanks.</p> <p>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.</p> <p>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.</p> <p>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. </p> <p>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.</p> <p>Hope its clearer now :).</p> <p>Thanks. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload