I know hibernate has lazy as default fetching strategy, but there are some things unclear to me so i hope you can explain it to me. What i want to do is to get a tile marked as start tile.
Query:
@NamedQuery(name = "Tile.findStartTileByGame",
query = "SELECT t FROM Tile t WHERE t.game = :game " +
"and t.startTile = true and t.blockWalkable = false")
Tile:
public class Tile{
@OneToOne(mappedBy="tile")
private GameCharacter character;
@OneToOne(mappedBy="tile")
private GameObject gameObject;
Game:
@OneToMany(mappedBy="game")
private List<Tile> tiles;
When i run my query and never use the object hibernate still joins me character and gameobject. So i have 3 Queries. I know i can solve this by a fetch join, but my question is why does hibernate fetch both entites at all? Even when i annotate them with fetch=FetchType.LAZY it will be queried.
My DAO:
public static Tile getFreeStartTile(EntityManager em, Game game) {
TypedQuery<Tile> query = em.createNamedQuery("Tile.findStartTileByGame", Tile.class);
query.setParameter("game", game);
List<Tile> result = query.getResultList();
...
Before i fix this i would like to understand why it happens. Thanks in advance m