java - @OneToMany Column Returns LazyInitializationException - Spring -
i have following mapping in user dto store map
of user properties:
@entity(name = "user") public class user{ @id @column(name = "id") private long userid; @onetomany @joincolumn(name = "properties") private map<long, property> properties= new linkedhashmap<long, lkpait>(); } @entity(name = "property") public class property{ @id @column(name = "id") private long propertyid; @column(name = "name") private string propertyname; }
and following @controller method
@requestmapping(value = "/getuser", produces = "application/json", method = requestmethod.get) public @responsebody page<lkpfeed> getlookupfeeds(model model) { page = 1; page<user> users = userrepo.getusers(new pagerequest(page, 100)); //returns 100 users return feeds; }
the @controller method executes fine (all users found) when return statement executes, following error:
org.hibernate.lazyinitializationexception: failed lazily initialize collection of role: com.myapp.dto.properties.properties, not initialize proxy - no session org.hibernate.collection.internal.abstractpersistentcollection.throwlazyinitializationexception(abstractpersistentcollection.java:575) org.hibernate.collection.internal.abstractpersistentcollection.withtemporarysessionifneeded(abstractpersistentcollection.java:214) org.hibernate.collection.internal.abstractpersistentcollection.readsize(abstractpersistentcollection.java:155) org.hibernate.collection.internal.persistentmap.isempty(persistentmap.java:145) com.fasterxml.jackson.databind.ser.std.mapserializer.serialize(mapserializer.java:399) com.fasterxml.jackson.databind.ser.std.mapserializer.serialize(mapserializer.java:27) com.fasterxml.jackson.databind.ser.beanpropertywriter.serializeasfield(beanpropertywriter.java:505) com.fasterxml.jackson.databind.ser.std.beanserializerbase.serializefields(beanserializerbase.java:639) com.fasterxml.jackson.databind.ser.beanserializer.serialize(beanserializer.java:152) com.fasterxml.jackson.databind.ser.impl.indexedlistserializer.serializecontents(indexedlistserializer.java:100)
based on exception properties field what's causing error. i've experimented fetchtypes
, persistencecontexts
@ loss. ideas?
you can several things
add fetch eager in
@onetomany
@entity(name = "user") public class user{ @id @column(name = "id") private long userid; @onetomany(fetch=fetchtype.eager) @joincolumn(name = "properties") private map<long, property> properties= new linkedhashmap<long, lkpait>(); }
or if don't want properties in returned json when want list users. can add
@jsonignore
properties
@entity(name = "user") public class user{ @id @column(name = "id") private long userid; @jsonignore @onetomany @joincolumn(name = "properties") private map<long, property> properties= new linkedhashmap<long, lkpait>(); }
Comments
Post a Comment