java - could not initialize proxy - no Session Exception -
i working on project based on hibernate , spring. have spent 2 days can't figure out why "lazy exception" error coming when call piece of code:
public memberuser sendemail(memberuser user) throws mailsendingexception { // check if password sent mail // hibernate.initialize(user); user = fetchservice.fetch(user, fetch); // user = userdao.load(user.getid(), fetch); final membergroup group = user.getmember().getmembergroup(); final boolean sendpasswordbyemail = group.getmembersettings().issendpasswordbyemail(); string newpassword = null; if (sendpasswordbyemail) { // if send mail, generate new password newpassword = generatepassword(group); } // update user user.setpassword(hashhandler.hash(user.getsalt(), newpassword)); user.setpassworddate(null); userdao.update(user); if (sendpasswordbyemail) { // send password mail mailhandler.sendresetpassword(user.getmember(), newpassword); } return user; }
this code sending email user reset password after getting user information db. exception comes @ "user= fetchservice.fetch(user, fetch)", while fetch :
private static final relationship fetch = relationshiphelper.nested(user.relationships.element, element.relationships.group);
this code of fetch function ,when fetch called executed:
@override public <e extends entity> list<e> fetch(final collection<e> entities, final relationship... fetch) { if (entities == null) { return null; } final list<e> result = new arraylist<e>(entities.size()); (e entity : entities) { entity = fetch(entity, fetch); result.add(entity); } return result; }
these hibernate classes don't have grip on these .
public class hibernatehelper { */ public static class queryparameter { private final object value; private final string operator; public queryparameter(final object value, final string operator) { this.value = value; this.operator = operator; } public string getoperator() { return operator; } public object getvalue() { return value; } } private static map<class<? extends entity>, set<string>> directpropertiescache = new hashmap<class<? extends entity>, set<string>>(); public static void addinelementsparameter(final stringbuilder hql, final map<string, object> namedparameters, final string path, final entity value) { if (value != null && value.ispersistent()) { final string parametername = getparametername(namedparameters, path); hql.append(" , :").append(parametername).append(" in elements(").append(path).append(") "); namedparameters.put(parametername, value); } } public static void addinparametertoquery(final stringbuilder hql, final map<string, object> namedparameters, final string path, final collection<?> values) { if (values != null && !values.isempty()) { final string parametername = getparametername(namedparameters, path); hql.append(" , ").append(path).append(" in (:").append(parametername).append(") "); namedparameters.put(parametername, values); } } /** * adds "in" operator parameter hql query, if given value not empty, appending values named parameters map */ public static void addinparametertoquery(final stringbuilder hql, final map<string, object> namedparameters, final string path, final object... values) { if (values != null && values.length > 0) { addinparametertoquery(hql, namedparameters, path, arrays.aslist(values)); } } /** * adds 'path %value%' parameter hql query if given value not empty, appending value named parameters map */ public static void addlikeparametertoquery(final stringbuilder hql, final map<string, object> namedparameters, final string path, final string value) { doaddlike(hql, namedparameters, path, value, false); } /** * adds equals parameter hql query, if given value not empty, appending value named parameters map */ public static void addparametertoquery(final stringbuilder hql, final map<string, object> namedparameters, final string path, final object value) { addparametertoqueryoperator(hql, namedparameters, path, "=", value); } /** * adds custom parameter hql query, if given parameter not empty, appending value named parameters map */ public static void addparametertoquery(final stringbuilder hql, final map<string, object> namedparameters, final string path, final queryparameter parameter) { if (parameter != null) { addparametertoqueryoperator(hql, namedparameters, path, parameter.getoperator(), parameter.getvalue()); } } /** * adds custom operator parameter hql query, if given value not empty, appending value named parameters map */ public static void addparametertoqueryoperator(final stringbuilder hql, final map<string, object> namedparameters, final string path, final string operator, final object value) { if (value != null && !"".equals(value)) { final string parametername = getparametername(namedparameters, path); hql.append(" , ").append(path).append(" ").append(operator).append(" :").append(parametername).append(" "); namedparameters.put(parametername, value); } } /** * adds period test hql query, if given period not empty, appending value named parameters map. see {@link period}, * controls whether begin , end dates inclusive / exclusive. * */ public static void addperiodparametertoquery(final stringbuilder hql, final map<string, object> namedparameters, final string path, final period period) { addparametertoquery(hql, namedparameters, path, getbeginparameter(period)); addparametertoquery(hql, namedparameters, path, getendparameter(period)); } /** * adds 'path value%' parameter hql query if given value not empty, appending value named parameters map */ public static void addrightlikeparametertoquery(final stringbuilder hql, final map<string, object> namedparameters, final string path, final string value) { doaddlike(hql, namedparameters, path, value, true); } /** * appends join portion on query fetch specified relationships, when appliable */ public static void appendjoinfetch(final stringbuilder hql, final class<? extends entity> entitytype, final string entityalias, final collection<relationship> fetch) { if (fetch != null) { final set<string> directrelationships = getdirectrelationshipproperties(entitytype, fetch); (final string directrelationship : directrelationships) { hql.append(" left join fetch ").append(entityalias).append(".").append(directrelationship).append(" "); } } } /** * appends order portion, given path lists (with optional direction, ie: "e.date desc", "e.name", "x.name") */ public static void appendorder(final stringbuilder hql, final collection<string> paths) { if (collectionutils.isnotempty(paths)) { hql.append(" order " + stringutils.join(paths.iterator(), ",")); } } /** * appends order portion, given path lists (with optional direction, ie: "e.date desc", "e.name", "x.name") */ public static void appendorder(final stringbuilder hql, final string... paths) { if (paths != null && paths.length > 0) { appendorder(hql, arrays.aslist(paths)); } } /** * returns begin date of given period, handling null */ public static queryparameter getbeginparameter(final period period) { if (period == null) { return null; } calendar begin = period.getbegin(); if (begin == null) { return null; } // must consider time when explicitly set if (!period.isusetime()) { // truncate begin date begin = datehelper.truncate(begin); } string operator = period.isinclusivebegin() ? ">=" : ">"; return new queryparameter(begin, operator); } /** * returns end date of given period, handling null */ public static queryparameter getendparameter(final period period) { if (period == null) { return null; } calendar end = period.getend(); if (end == null) { return null; } // must consider time when explicitly set if (!period.isusetime()) { // truncate end date , set next day end = datehelper.getdayend(end); } string operator = period.isinclusiveend() ? "<=" : "<"; return new queryparameter(end, operator); } /** * returns stringbuilder containing begin of single entity select hql * @param entitytype entity type search * @param entityalias entity alias on query * @return stringbuiler */ public static stringbuilder getinitialquery(final class<? extends entity> entitytype, final string entityalias) { return getinitialquery(entitytype, entityalias, null); } /** * returns stringbuilder containing begin of single entity select hql, especified fetch relationships, when appliable * @param entitytype entity type search * @param entityalias entity alias on query * @param fetch relationships fetch * @return stringbuiler */ public static stringbuilder getinitialquery(final class<? extends entity> entitytype, final string entityalias, final collection<relationship> fetch) { final stringbuilder hql = new stringbuilder(" ").append(entitytype.getname()).append(" ").append(entityalias).append(" "); appendjoinfetch(hql, entitytype, entityalias, fetch); hql.append(" 1=1 "); return hql; } private static void doaddlike(final stringbuilder hql, final map<string, object> namedparameters, final string path, string value, final boolean rightonly) { value = stringutils.trimtonull(value); if (value == null) { return; } // remove manually entered '%' value = stringutils.trimtonull(stringutils.replace(value, "%", "")); if (value == null) { return; } // assuming default database collation case insensitive, don't need perform case transformations if (rightonly) { value += "%"; } else { value = "%" + value + "%"; } addparametertoqueryoperator(hql, namedparameters, path, "like", value); } /** * returns set of properties fetched directly on hql */ private static set<string> getdirectrelationshipproperties(final class<? extends entity> entitytype, final collection<relationship> fetch) { // populate direct properties cache entity if not yet exists set<string> cacheddirectproperties = directpropertiescache.get(entitytype); if (cacheddirectproperties == null) { cacheddirectproperties = new hashset<string>(); final propertydescriptor[] propertydescriptors = propertyutils.getpropertydescriptors(entitytype); // scan child -> parent relationships (final propertydescriptor descriptor : propertydescriptors) { if (descriptor.getreadmethod() != null && descriptor.getwritemethod() != null && entity.class.isassignablefrom(descriptor.getpropertytype())) { // child -> parent relationship. add cache cacheddirectproperties.add(descriptor.getname()); } } directpropertiescache.put(entitytype, cacheddirectproperties); } // build properties add hql fetch given relationship set final set<string> propertiestoaddtofetch = new hashset<string>(); (final relationship relationship : fetch) { final string name = propertyhelper.firstproperty(relationship.getname()); if (cacheddirectproperties.contains(name)) { propertiestoaddtofetch.add(name); } } return propertiestoaddtofetch; } /** * generates parameter name */ private static string getparametername(final map<string, object> namedparameters, final string propertyname) { int counter = 1; // transform property in valid identifier final stringbuilder sb = new stringbuilder(propertyname.length()); (int = 0, len = propertyname.length(); < len; i++) { final char c = propertyname.charat(i); if (character.isjavaidentifierpart(c)) { sb.append(c); } else { sb.append('_'); } } final string field = sb.tostring(); string parametername = field.concat("_1"); while (namedparameters.containskey(parametername)) { parametername = field.concat("_").concat(string.valueof(++counter)); } return parametername; } }
this error:
org.hibernate.lazyinitializationexception: not initialize proxy - no session @ org.hibernate.proxy.abstractlazyinitializer.initialize(abstractlazyinitializer.java:167) @ nl.strohalm.cyclos.utils.hibernate.hibernatequeryhandler.initialize(hibernatequeryhandler.java:252) @ nl.strohalm.cyclos.dao.fetchdaoimpl.dofetch(fetchdaoimpl.java:80) @ nl.strohalm.cyclos.dao.fetchdaoimpl.fetch(fetchdaoimpl.java:37) @ nl.strohalm.cyclos.services.fetch.fetchserviceimpl.fetch(fetchserviceimpl.java:45) @ nl.strohalm.cyclos.services.access.accessserviceimpl.sendemail(accessserviceimpl.java:1695) @ nl.strohalm.cyclos.services.access.accessserviceimpl.resetpasswordandsendafterexpire(accessserviceimpl.java:1732) @ com.omnia.payo.scheduling.task.userlogininfoschedulingtask.dorun(userlogininfoschedulingtask.java:43) @ nl.strohalm.cyclos.scheduling.tasks.basescheduledtask.run(basescheduledtask.java:42) @ nl.strohalm.cyclos.utils.tasks.taskrunnerimpl$4.run(taskrunnerimpl.java:193) @ java.util.concurrent.executors$runnableadapter.call(executors.java:511) @ nl.strohalm.cyclos.utils.access.loggeduser.runassystem(loggeduser.java:285) @ nl.strohalm.cyclos.utils.tasks.taskrunnerimpl.dorunscheduledtask(taskrunnerimpl.java:190) @ nl.strohalm.cyclos.utils.tasks.taskrunnerimpl.dorunscheduledtask(taskrunnerimpl.java:170) @ nl.strohalm.cyclos.utils.tasks.taskrunnerimpl$scheduledtaskthreads.process(taskrunnerimpl.java:64) @ nl.strohalm.cyclos.utils.tasks.taskrunnerimpl$scheduledtaskthreads.process(taskrunnerimpl.java:1) @ nl.strohalm.cyclos.utils.paralleltask$1.process(paralleltask.java:43) @ nl.strohalm.cyclos.utils.workerthreads$workerthread$1.call(workerthreads.java:53) @ nl.strohalm.cyclos.utils.workerthreads$workerthread$1.call(workerthreads.java:1) @ nl.strohalm.cyclos.utils.access.loggeduser.runassystem(loggeduser.java:285) @ nl.strohalm.cyclos.utils.workerthreads$workerthread.run(workerthreads.java:49)
code dofetch in fetchdaoimpl is:
private <e extends entity> e dofetch(final e inputentity, final relationship... fetch) { if (inputentity == null || inputentity.getid() == null) { throw new unexpectedentityexception(); } e entity; // discover entity real class , id final class<? extends entity> entitytype = entityhelper.getrealclass(inputentity); final long id = inputentity.getid(); // load , initialize entity try { entity = (e) gethibernatetemplate().load(entitytype, id); entity = (e) hibernatequeryhandler.initialize(entity); } catch (final objectretrievalfailureexception e) { throw new entitynotfoundexception(entitytype, id); } catch (final objectnotfoundexception e) { throw new entitynotfoundexception(entitytype, id); } // ... , fetch each relationship if (!arrayutils.isempty(fetch)) { (final relationship relationship : fetch) { if (relationship == null) { continue; } try { final string name = relationship.getname(); object bean = entity; string first = propertyhelper.firstproperty(name); string nested = propertyhelper.nestedpath(name); while (bean != null && first != null) { final object value = hibernatequeryhandler.initializeproperty(bean, first); bean = value; first = propertyhelper.firstproperty(nested); nested = propertyhelper.nestedpath(nested); } } catch (final propertyexception e) { // ok - nonexisting property. fetching relationship exists in 1 of subclasses, , trying use no // 1 } catch (final exception e) { throw new propertyexception(entity, relationship.getname(), e); } } } return entity; }
please me ,i'll thankful.
this should helpfull :
what opensessioninviewfilter , how
and
hibernate updating different sessions
or experts
Comments
Post a Comment