java - Generic class for the Dao classes -


i trying setting generic class serve base class dao classes in application, facing error right now. generic class defined way;

public class dao<e> {      private final e entity;      @autowired     sessionfactory sessionfactory;      protected session getcurrentsession(){         return sessionfactory.getcurrentsession();     }      public dao(e entity) {           this.entity = entity;     }      public e getentity() {         return this.entity;     }      @transactional     public boolean persist(e transientinstance) {         try {             sessionfactory.getcurrentsession().persist(transientinstance);             return true;         } catch (runtimeexception re) {             return false;         }     }      @transactional     public boolean remove(e transientinstance) {         try {             sessionfactory.getcurrentsession().delete(transientinstance);             return true;         } catch (runtimeexception re) {             return false;         }     }      @suppresswarnings("unchecked")     @transactional     public e merge(e detachedinstance) {         try {             e result = (e) sessionfactory.getcurrentsession().merge(detachedinstance);             return result;         } catch (runtimeexception re) {             return null;         }     }      @transactional     public e findbyid(int id) {         try {             e instance = (e) sessionfactory.getcurrentsession().get(e, id);             return instance;         } catch (runtimeexception re) {             return null;         }     }      @suppresswarnings("unchecked")     @transactional     public e findbyusername(string username) {         try {             e instance = (e) sessionfactory.getcurrentsession().createcriteria(e, username).add(restrictions.like("login", username)).list().get(0);             return instance;         } catch (runtimeexception re) {             return null;         }     }      @suppresswarnings("unchecked")     @transactional     public list<e> findall() {         try {             list<e> instance = sessionfactory.getcurrentsession().createcriteria(e).list();             return instance;         } catch (runtimeexception re) {             return null;         }     }  } 

the methods error last three, , is related referente e get() , createcriteria() in implementation of them (the error e cannot resolved variable). usually, use 'usuario.class' when don't use generic-based approach.

anyone knows how fix error in generic class (if approach possible).

first of, type parameter e not equal instance of class. , second, due type erasure in java e lost @ runtime. seams entity passed in parameter constructor type token use when trying ask class. hence change methods follows:

@transactional public e findbyid(int id) {     try {         e instance = (e) sessionfactory.getcurrentsession().get(entity.getclass(), id);         return instance;     } catch (runtimeexception re) {         return null;     } }  @suppresswarnings("unchecked") @transactional public e findbyusername(string username) {     try {         e instance = (e) sessionfactory.getcurrentsession().createcriteria(entity.getclass(), username).add(restrictions.like("login", username)).list().get(0);         return instance;     } catch (runtimeexception re) {         return null;     } }  @suppresswarnings("unchecked") @transactional public list<e> findall() {     try {         list<e> instance = sessionfactory.getcurrentsession().createcriteria(entity.getclass()).list();         return instance;     } catch (runtimeexception re) {         return null;     } } 

or if entity not type token, add parameter class<e> clazz constructor this:

private final e entity; private final class<e> clazz;  public dao(e entity, class<e> clazz) {       this.entity = entity;     this.clazz = clazz; } 

and use clazz instead of entity.getclass() in suggested methods.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -