Question: I have an entity with `@OneToOne` mapped subentity:

```
@Entity @Table
public class BaseEntity {

    @Id
    private String key;

    @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private InnerEntity inner;

}

@Entity @Table
public class InnerEntity {

    private String data;
}

```

It was working perfectly on persist and merge operations until I decided to fetch all records in a named query (`SELECT e FROM BaseEntity e`). Problems are that after calling it, Hibernate fetches all records from BaseEntity and then executes distinct queries for each InnerEntity. Because table is quite big it takes much time and takes much memory.

First, I started to investigate if `getInner()` is called anywhere in running code. Then I tried to change `fetchType` to `EAGER` to check if Hibernate it's going to fetch it all with one query. It didn't. Another try was to change mapping to `@ManyToOne`. Doing this I've added `updatable/insertable=false` to `@JoinColumn` annotation. Fetching started to work perfectly - one `SELECT` without any `JOIN` (I changed `EAGER` back to `LAZY`), but problems with updating begun. Hibernate expects `InnerEntity` to be persisted first, but there's no property with primary key. Of course I can do this and explicity persist `InnerEntity` calling `setKey()` first, but I would rather solve this without this.

Any ideas?

Answer: 
If you want `inner` field to be loaded on demand and your relation is `@OnToOne`you can try this

`@OneToOne(fetch = FetchType.LAZY, optional = false)`
When using HQL hibernate doesn't consider the annotations, so you should tell it how to work.
In your case you should right the HQL like this:
SELECT e FROM BaseEntity as e left join fetch e.inner