hibernate - Force lazy loading of usually eager attributes -
how force specific query lazy load attribute, has eager loading? entitygraphtype.fetch isn't working.
@namedentitygraph(name="neggraphname",attributenodes={ @namedattributenode("name"), @namedattributenode("grup")}) class user{ private string name; @manytoone private usergroup grup; @manytomany(fetch=fetchtype.eager) protected set<authority> authoritieslist; } interface userrepository extends crudrepository<string, user>{ @entitygraph(value="neggraphname", type=entitygraphtype.fetch) @query("<custom query here>") private collection<user> getusersinspecialquery(@param("paramname") string paramname); }
basically, when call userrepository.getusersinspecialquery()
initial query joins in object, , doesn't join in authority, hibernate anyway initializes authorities list separate sql query per user result, highly inefficient. also, initializes domainobject object graph parent not marked eager.
disabling fetch=fetchtype.eager
on authoritylist disabled additional per-user queries, still want eagerly fetched other queries.
entitygraphtype.fetch
's javadoc says:
when javax.persistence.fetchgraph property used specify entity graph, attributes specified attribute nodes of entity graph treated fetchtype.eager , attributes not specified treated fetchtype.lazy
however not case me
hibernate : force lazy-loadding on eager field <-- doesn't have solution
fetch type lazy still causes eager loading hibernate spring data <-- claims problem lazy attribute loaded viewing, in case, stepped through in hibernate, , during materialization of user object
you cant make eager assosciation lazy. suggest make lazy , in queries want eagerly join fetch authoritieslist
eager then.
another option subentity same current user entity, mapped same db table without relationship dont want:
@entity @table(name="same_table_as_user_entity") class basicuser{ private string name; @manytoone private usergroup grup; }
another option inheritance.
Comments
Post a Comment