I have a pretty stupid question.
Is it possible to have a direct relation that only pulls back a single column from the child relation... example...
@Table(name="AUTH")
public class Magazine {
@Column(length=9)
@Id private String isbn;
@Id private String title;
@OneToOne
@JoinColumn(name="COVER_ID" referencedColumnName="ID")
private string coverArticle; //<--- I want this to just have the Article Name... How can this be done
...
}
See that oneToone annotation above. I want that to just contain the string of the Article name...
@Table(name="ART")
public class Article {
@Id private long id;
@Id private String ArticleName;
}
I know you can have an ElementCollection where you get a collection of Elements from another table, how do you get a single value back into that element?
This is probably not as simple as you were hoping, but you can do this by defining a FetchGroup on your entities. For the example you gave above, you could modify it as follows:
@FetchGroups({
@FetchGroup(name="coverArticleNameOnly", attributes={
@FetchAttribute(name="isbn"),
@FetchAttribute(name="title"),
@FetchAttribute(name="coverArticle"),
...
})
})
@Table(name="AUTH")
public class Magazine {
@Column(length=9)
@Id private String isbn;
@Id private String title;
@OneToOne
@JoinColumn(name="COVER_ID" referencedColumnName="ID")
private string coverArticle;
...
}
@FetchGroups({
@FetchGroup(name="coverArticleNameOnly", attributes={
@FetchAttribute(name="articleName")
})
})
@Table(name="ART")
public class Article {
@Id private long id;
@Id private String ArticleName;
}
Then, before you retrieve your Magazine record, make the following call:
getEntityManager().getFetchPlan().addFetchGroup("coverArticleNameOnly");
The unfortunate part, as you can see above, is that you need to add fetch attributes to your fetch group for all of the fields in the Magazine class that you want to keep.
For more information see http://openjpa.apache.org/builds/1.0.4/apache-openjpa-1.0.4/docs/manual/ref_guide_fetch.html.