Hibernate @OneToOne with superclass, only retrieve superclass fields on join?

A secondary table is normally used to map the content of a single entity to two tables. It doesn't allow for lazy/select fetching using standard JPA annotations. You may use a proprietary bernate annotation to load it using a separate select, and only if necessary, though.

See docs.jboss.org/hibernate/core/3.6/refere... : fetch: If set to JOIN, the default, bernate will use an inner join to retrieve a secondary table defined by a class or its superclasses and an outer join for a secondary table defined by a subclass. If set to SELECT then bernate will use a sequential select for a secondary table defined on a subclass, which will be issued only if a row turns out to represent an instance of the subclass. Inner joins will still be used to retrieve a secondary defined by the class and its superclasses So setting the fetch attribute of the bernate Table annotation to SELECT will do what you want : an additional select clause will be issued to select the values from just the appropriate secondary table If you want lazy fetching, then a secondary table is not what you want.

You'll have to do it using associations.

A secondary table is normally used to map the content of a single entity to two tables. It doesn't allow for lazy/select fetching using standard JPA annotations. You may use a proprietary bernate annotation to load it using a separate select, and only if necessary, though.

See docs.jboss.org/hibernate/core/3.6/refere... fetch: If set to JOIN, the default, bernate will use an inner join to retrieve a secondary table defined by a class or its superclasses and an outer join for a secondary table defined by a subclass. If set to SELECT then bernate will use a sequential select for a secondary table defined on a subclass, which will be issued only if a row turns out to represent an instance of the subclass. Inner joins will still be used to retrieve a secondary defined by the class and its superclasses.So setting the fetch attribute of the bernate @Table annotation to SELECT will do what you want : an additional select clause will be issued to select the values from just the appropriate secondary table.

If you want lazy fetching, then a secondary table is not what you want. You'll have to do it using associations.

Thanks for the quick response. So in adding the bernate @Table annotation, I remove all joins. I'm still not sure I understand why bernate has to perform the additional select in the first place?

As I said in my original post, I'm only interested in working with the fields of the superclass. Can you help me understand why bernate automatically fetches the fields of the subclass even though I'm not interested in using them? – Rob Blake Oct 25 at 11:22 Because The entity has to be of a given type.

If its discriminator says it's a Child1, the entity type must be Child1. And a Child1 has a name property, so bernate must populate the name property to avoid breaking the invariants from the Child1 class. Even if you'll only use the fields of the parent, you might invoke a polymorphic method that does something differently for Child1.

– JB Nizet Oct 25 at 11:29 Ah ok, that makes sense. Thanks very much. – Rob Blake Oct 25 at 11:42.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions