I'm using ormlite 4.48 and this is my classes:
Table:
@DatabaseTable(tableName="client", daoClass=ClientDAOImpl.class)
public class Client
Inteface:
public interface ClientDAO extends Dao<Client, String> {
public List<Client> getAll();
}
BaseDaoImpl:
public class ClientDAOImpl extends BaseDaoImpl<Client, String> implements ClientDAO
Helper:
public class Helper extends OrmLiteSqliteOpenHelper{
public Dao<Client, String> getClientDAO() throws SQLException {
return getDao(Client.class);
}
}
I made this based here: http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_2.html#DAO-Setup
When I tryed instantiate my custom dao class is the problem. The getDao simply is ignoring the annotation in the table class (daoClass=ClientDAOImpl.class).
i can not access the getAll (and anothers in the ClientDAOImpl) method.
Here is the usage:
Helper helper = OpenHelperManager.getHelper(getActivity().getApplicationContext(),Helper.class);
Dao<Client, String> clientDAO = databaseHelper.getClientDAO();
And so I thought I could do this:
List<Client> listClient = clientDAO.getAll();
Does anyone have any idea how to solve this? Or indicate that I'm forgetting to do (or doing wrong)?
3 years later, if someone is still using here we go:
Ludiaz the problem was that you were trying to use a 'Dao' in your 'Helper' class, while you should be using your custom Dao: 'ClientDAO'.
public class Helper extends OrmLiteSqliteOpenHelper{
public Dao<Client, String> getClientDAO() throws SQLException {
return getDao(Client.class);
}
}
instead of that should be this:
public class Helper extends OrmLiteSqliteOpenHelper{
public ClientDAO getClientDAO() throws SQLException {
return new ClientDAO(getConnectionSource());
}
}
Also your Dao lacks of constructors.
In my implentation im doing something like this:
/**
* Asistencia.class
*/
@DatabaseTable(daoClass = AsistenciaDaoImpl.class)
public class Asistencia implements Parcelable {
@SerializedName("id_app")
@Expose
@DatabaseField(id = true, index = true)
private Integer id_app;
@SerializedName("id_usuario")
@Expose
@DatabaseField
private Integer id_usuario;
@SerializedName("asistencia")
@Expose
@DatabaseField
private Boolean asistencia;
@SerializedName("fecha")
@Expose
@DatabaseField
private String fecha;
@SerializedName("id_supervisor")
@Expose
@DatabaseField
private String id_supervisor;
@SerializedName("id_motivo")
@Expose
@DatabaseField
private Integer id_motivo;
@DatabaseField
private Integer status = STATUS_NORMAL;
public static final int STATUS_NORMAL = 0;
public static final int STATUS_MODIFICADO = 1;
public static final int STATUS_CREADO = 2;
/**
* No args constructor for use in serialization
*/
public Asistencia() {
}
/**
* @param id_app
* @param id_motivo
* @param fecha
* @param id_usuario
* @param asistencia
* @param id_supervisor
*/
public Asistencia(Integer id_app, Integer id_usuario, Boolean asistencia, String fecha, String id_supervisor, Integer id_motivo, Integer status) {
super();
this.id_app = id_app;
this.id_usuario = id_usuario;
this.asistencia = asistencia;
this.fecha = fecha;
this.id_supervisor = id_supervisor;
this.id_motivo = id_motivo;
this.status = status;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public Integer getId_app() {
return id_app;
}
public void setId_app(Integer id_app) {
this.id_app = id_app;
}
public Integer getId_usuario() {
return id_usuario;
}
public void setId_usuario(Integer id_usuario) {
this.id_usuario = id_usuario;
}
public Boolean getAsistencia() {
return asistencia;
}
public void setAsistencia(Boolean asistencia) {
this.asistencia = asistencia;
}
public String getFecha() {
return fecha;
}
public void setFecha(String fecha) {
this.fecha = fecha;
}
public String getId_supervisor() {
return id_supervisor;
}
public void setId_supervisor(String id_supervisor) {
this.id_supervisor = id_supervisor;
}
public Integer getId_motivo() {
return id_motivo;
}
public void setId_motivo(Integer id_motivo) {
this.id_motivo = id_motivo;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(this.id_app);
dest.writeValue(this.id_usuario);
dest.writeValue(this.asistencia);
dest.writeString(this.fecha);
dest.writeString(this.id_supervisor);
dest.writeValue(this.id_motivo);
}
protected Asistencia(Parcel in) {
this.id_app = (Integer) in.readValue(Integer.class.getClassLoader());
this.id_usuario = (Integer) in.readValue(Integer.class.getClassLoader());
this.asistencia = (Boolean) in.readValue(Boolean.class.getClassLoader());
this.fecha = in.readString();
this.id_supervisor = in.readString();
this.id_motivo = (Integer) in.readValue(Integer.class.getClassLoader());
}
public static final Parcelable.Creator<Asistencia> CREATOR = new Parcelable.Creator<Asistencia>() {
@Override
public Asistencia createFromParcel(Parcel source) {
return new Asistencia(source);
}
@Override
public Asistencia[] newArray(int size) {
return new Asistencia[size];
}
};
}
In the AsistenciaDao interface will be declared all the methods that your are going to use in your custom dao, but tbh, i dont know if this is necessary.
/**
* AsistenciaDao.class
*/
public interface AsistenciaDao extends Dao<Asistencia, Integer> {
void dummyTestMethod();
}
I suggest to use singleton pattern to getInstance from the Dao.
/**
* AsistenciaDaoImpl.class
*/
public class AsistenciaDaoImpl extends BaseDaoImpl<Asistencia, Integer> implements AsistenciaDao {
/**
* Reference to singleton instance
*/
private static AsistenciaDaoImpl instance;
/**
* Constructor necessary for table creation.
*
* @param connectionSource
* @param tableConfig
* @throws SQLException
*/
public AsistenciaDaoImpl(ConnectionSource connectionSource, DatabaseTableConfig<Asistencia> tableConfig) throws SQLException {
super(connectionSource, tableConfig);
}
/**
* Private constructor for singleton.
*
* @param connectionSource
* @throws SQLException
*/
private AsistenciaDaoImpl(ConnectionSource connectionSource) throws SQLException {
super(connectionSource, Asistencia.class);
}
/**
* Singleton
*
* @param connectionSource
* @return
*/
@NonNull
public static AsistenciaDaoImpl getInstance(ConnectionSource connectionSource) {
if (instance == null) {
try {
instance = new AsistenciaDaoImpl(connectionSource);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
return instance;
}
/**
* Custom method
*/
@Override
public void dummyTestMethod() {
System.out.println("Dummy test Method");
}
/**
* Just for handle exception one time.
*
* @param data
* @return
*/
@Override
public CreateOrUpdateStatus createOrUpdate(Asistencia data) {
try {
return super.createOrUpdate(data);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
/**
* Just for handle exception one time.
*
* @param integer
* @return
*/
@Override
public Asistencia queryForId(Integer integer) {
try {
return super.queryForId(integer);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
So you will use your custom dao like this
public AsistenciaDaoImpl getAsistenciaRuntimeDao() {
if (asistenciaDao == null) {
asistenciaDao = AsistenciaDaoImpl.getInstance(getConnectionSource());
}
return asistenciaDao;
}
Now you can access to your custom methods :)
mDb.getAsistenciaRuntimeDao().dummyTestMethod();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.