dimanche 22 février 2015

Hibernate 4 second level cache hibernate is not working with association (EhCache)

I'm new to hibernate and I'm trying the second level cache.


My entities are Team : OneToMany : Player


The team entity :



@Entity
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Team {

...

@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
@OneToMany(fetch=FetchType.LAZY, mappedBy="team")
@Cascade(CascadeType.ALL)
private List<Player> players;

...
}


the Player entity :



@Entity
@Cacheable
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class Player extends Person {

...

@Getter @Setter
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
@ManyToOne(fetch=FetchType.LAZY)
@Cascade(CascadeType.SAVE_UPDATE)
private Team team;

....
}


My TeamDao is :



@Repository
public class TeamDao {

@Autowired
private SessionFactory sessionFactory;

...

@Transactional
public Team getTeam(Integer id) {
return (Team) sessionFactory.getCurrentSession().createQuery("from team t inner join fetch t.players p where t.id = :tid")
.setParameter("tid", id)
.setCacheable(true)
.uniqueResult();

}

...
}


When i load a team for example id=1, I find it in the second level cache. But I don't find the players list of the team loaded even if the association is declared cacheable. session.getCache().contains(Team.class, 1); returns true but session.getCache().contains(Player.class, 1) returns false


When I try to get a player of the loaded team,



Player player = playerDao.getPlayer(1);


Hibernate generates a select to get the Player even if The Team loaded contains this player



Player player = team.getPlayer().get(0);


Is there a probleme in my entities annotations ? I don't know why it doesn't work for me.


Thanks for helping


Aucun commentaire:

Enregistrer un commentaire