java - Creating BaseDAO for Each Dao class -
i created spring application decided add basedao eliminate redundant create, update,delete,findbyid,and findall methods every dao. created basedao , every dao should extend basedao.
basedaoimpl
public class basedaoimpl implements basedao{ sessionfactory sessionfactory; public void setsessionfactory(sessionfactory sf){ this.sessionfactory = sf; } @override public void create(modelbase modelbase) { session session = this.sessionfactory.getcurrentsession(); session.persist(modelbase); } @override public void update(modelbase modelbase) { session session = this.sessionfactory.getcurrentsession(); session.update(modelbase); } @override public collection findall(class aclass) { session session = this.sessionfactory.getcurrentsession(); collection modelcols = session.createquery("from "+aclass.getsimplename()).list(); return modelcols; } @override public modelbase findbyid(class aclass, integer id) { session session = this.sessionfactory.getcurrentsession(); modelbase modelbase = (modelbase) session.load(aclass, new integer(id)); return modelbase; } }
then extends dao each dao
employeedaoimp
public class employeedaoimpl extends basedaoimpl implements employeedao{ private sessionfactory sessionfactory; public void setsessionfactory(sessionfactory sf){ this.sessionfactory = sf; } }
i created baseservice this. when try access basedao methods employeedao returns null pointer exception. why happen. dont want use genericdao google. because should create daos each model. want eliminate this. follow method.
your overriding setsessionfactory
base class no reason, available extending class employeedaoimpl
, either remove or try below:
public class employeedaoimpl extends basedaoimpl implements employeedao{ //this reference should base class, // extending class ref hiding base ref. // private sessionfactory sessionfactory; public void setsessionfactory(sessionfactory sf){ super.setsessionfactory(sf); } }
Comments
Post a Comment