3 年后,如果有人还在使用这里,我们就去:
Ludiaz 的问题是您试图在“Helper”类中使用“Dao”,而您应该使用自定义 Dao:“ClientDAO”。
public class Helper extends OrmLiteSqliteOpenHelper{
public Dao<Client, String> getClientDAO() throws SQLException {
return getDao(Client.class);
}
}
应该是这样的:
public class Helper extends OrmLiteSqliteOpenHelper{
public ClientDAO getClientDAO() throws SQLException {
return new ClientDAO(getConnectionSource());
}
}
你的道也缺少构造函数。
在我的实施中,我正在做这样的事情:
/**
* 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];
}
};
}
在 AsistenciaDao 接口中将声明您将在自定义 dao 中使用的所有方法,但是,我不知道这是否有必要。
/**
* AsistenciaDao.class
*/
public interface AsistenciaDao extends Dao<Asistencia, Integer> {
void dummyTestMethod();
}
我建议使用单例模式从 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);
}
}
}
所以你会像这样使用你的自定义 dao
public AsistenciaDaoImpl getAsistenciaRuntimeDao() {
if (asistenciaDao == null) {
asistenciaDao = AsistenciaDaoImpl.getInstance(getConnectionSource());
}
return asistenciaDao;
}
现在您可以访问您的自定义方法 :)
mDb.getAsistenciaRuntimeDao().dummyTestMethod();