Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Access child data via mapped by?

 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a top level Entity that contains a set of parent entities (@OneToMany).

I also want in that top level entity a set of child entities. The mapped by field is stored in the parent table so when I do this on the child i get

Top level entity with sets


Text Item entity (Parent class)


Child entity of TextItem




Any ideas on how to get a set of my child items. Seems like this should work because a TextItemCharValue IS A TextItem and TextItem does have a doc field.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In
@Entity
@Table(name="TEXTITEM_CHR_VALUE")
public class TextItemCharValue extends TextItem {
private String value;

I don't see an attribute called "doc".

mapped by is the field/attribute in the class that is being referenced, not the parent. So it fails because it is looking for "doc" in your TextItemCharValue.

Mark
 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class TextItemCharValue extends TextItem

TextItemCharValue is a TextItem

TextItem is the parent class and has a field called doc. Through inheritance TextItemCharValue does hava a doc field.

The question isn't really why this is failing (as its seems hibernate cannot handle fields through inheritance).

The question really is how do I work around the fact that hibernate will not look in parent classes for a field in mappedby. So how can I reference a Set of TextItemCharValues in the Doc entity???
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The question isn't really why this is failing (as its seems hibernate cannot handle fields through inheritance).


Actually you have to map the inheritance. There is mapping that is specific for inheritance in JPA and in Hibernate. You haven't mapped it as such.

http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#d0e829

Mark
 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
From the above code in the TextItem class:


It is already there.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tedd Williams wrote:From the above code in the TextItem class:


It is already there.


But that class doesn't extend anything. I would think, and note that I avoid inheritance 95% of the time, that it would need to be on public class TextItemCharValue extends TextItem {

Mark
 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope nothing needs to go on the child class, unless of course your PK for the child table is different than the PK for the parent table. In my case it is not so no annotations are needed on the child class
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, what about using @MappedSuperclass instead?

http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#d0e939

It looks more like what you are doing than the Inheritance mapping.

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic