【问题标题】:Why i can't use a custom DAO class in ormlite?为什么我不能在 ormlite 中使用自定义 DAO 类?
【发布时间】:2015-07-22 11:22:25
【问题描述】:

我正在使用 ormlite 4.48,这是我的课程:

表:

@DatabaseTable(tableName="client", daoClass=ClientDAOImpl.class)
public class Client

界面:

public interface ClientDAO extends Dao<Client, String> {
    public List<Client> getAll();
}

BaseDaoImpl:

public class ClientDAOImpl extends BaseDaoImpl<Client, String> implements ClientDAO

助手:

public class Helper extends OrmLiteSqliteOpenHelper{
    public Dao<Client, String> getClientDAO() throws SQLException {
        return getDao(Client.class);
    }
}

我在这里制作了这个:http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_2.html#DAO-Setup

当我尝试实例化我的自定义 dao 类时,问题就来了。 getDao 只是忽略了表类中的注解(daoClass=ClientDAOImpl.class)。

我无法访问 getAll(以及 ClientDAOImpl 中的其他)方法。

用法如下:

Helper helper = OpenHelperManager.getHelper(getActivity().getApplicationContext(),Helper.class);
Dao<Client, String> clientDAO = databaseHelper.getClientDAO();

所以我想我可以这样做:

List<Client> listClient = clientDAO.getAll();

有人知道如何解决这个问题吗?或者表明我忘记做(或做错了)?

【问题讨论】:

    标签: java android sqlite dao ormlite


    【解决方案1】:

    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();
    

    【讨论】:

      猜你喜欢
      • 2019-07-05
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 2017-09-01
      • 2014-05-29
      • 2020-03-22
      • 1970-01-01
      相关资源
      最近更新 更多