I have the following entity:
@Entity
public class A{
...
@ManyToOne( optional=false, fetch=FetchType.LAZY )
@NotFound( action=NotFoundAction.EXCEPTION )
@JoinColumn( name="ID_CLASSB", nullable=false, insertable=true, updatable=true )
private B b;
...
private String anIdentifier;
...
@OneToMany(fetch=FetchType.LAZY)...
private List<C> manyObjects;
...
@OneToMany(fetch=FetchType.LAZY)...
private List<D> soMany;
}
As you can see entity B is an attribute in entity A.
B entity has no reference to A entity and is made by simple attributes:
@Entity
public class B{
@Column(...)
private String field1;
@Column(...)
private int field2;
...
}
Now, I'd like to retrieve all B entities from A entities where the "anIdentifier" attribute matches a specific value. My query, for now, would be:
"select a.b from A a where a.anIdentifier='identifier'"
Problem is that this second query is particularly slow...and I guess it is because hibernate still creates the whole entity A (and thus all its attributes) before extracting the entity B.
So, is there another, most efficient, way to accomplish my goal?