0

我在一个项目中使用 ORM Lite,我决定使用该工具使持久性成为 Web 服务的一部分,并且可以在 Android 上重用它。

但是我很痛苦,因为possou复杂的对象有多个ForeignCollectionField和Foreign object,而在坚持这些数据的时候很头疼,因为我要一个一个地输入他们的孩子,我想的想法ORM 让生活更轻松,即你必须坚持对象和父亲,其余的一切都在幕后完成......

好吧,现在放弃lite ORM为时已晚,我想知道是否有办法做上面的sitei ..

我在这里找到了一段代码

试图实现但似乎不起作用,只是不断保存父对象。

遵循我正在尝试使用的功能,但不知道导入是否正确,因为我在上面的链接中找到的代码没有这个数据

public int create(Object entity, Context context) throws IllegalArgumentException, IllegalAccessException, SQLException, SQLException {
    try{
        if (entity!=null){
            // Class type of entity used for reflection
            Class clazz = entity.getClass();

            // Search declared fields and save child entities before saving parent.
            for(Field field : clazz.getDeclaredFields()) {
                field.setAccessible(true);
                // Inspect annotations
                Annotation[] annotations = field.getDeclaredAnnotations();
                try{
                    for(Annotation annotation : annotations) {
                        // Only consider fields with the DatabaseField annotation
                        if(annotation instanceof DatabaseField) {
                            // Check for foreign attribute
                            DatabaseField databaseField = (DatabaseField)annotation;
                            if(databaseField.foreign()) {
                                // Check for instance of Entity
                                Object object = field.get(entity);

                                Dao gDao = getDatabase(context).getDao(object.getClass());
                                gDao.create(object);
                            }
                        }else if (annotation instanceof ForeignCollectionField){
                            Object object = field.get(entity);
                            for(Object obj : new ArrayList<Object>((Collection<?>)object)){
                                Class c = obj.getClass();

                                Dao gDao = getDatabase(context).getDao(obj.getClass());
                                gDao.create(obj);
                            }
                        }
                    }
                }catch (NullPointerException e){
                    e.printStackTrace();
                }
            }

            // Retrieve the common DAO for the entity class
            Dao dao =  getDatabase(context).getDao(entity.getClass());

            // Persist the entity to the database
            return dao.create(entity);
        }else
            return 0;
    }finally {
        if (database != null) {
            OpenHelperManager.releaseHelper();
            database = null;
        }
    }
}

利用同一个帖子,还需要一个 colução 来删除级联,想象一下我有以下表格的情况:

公司>分类>人员>联系方式>电话和邮箱

删除,现在我按照文档中的说明进行操作:

public int deleteCascade(Prefeitura prefeitura, Context context){
        try{
            Dao<Prefeitura, Integer> dao = getDatabase(context).getDao(Prefeitura.class);
            DeleteBuilder db = dao.deleteBuilder();
            db.where().eq("prefeitura_id", prefeitura.getId());
            dao.delete(db.prepare());
            // then call the super to delete the city
            return dao.delete(prefeitura);


        }catch (SQLException e){
            e.printStackTrace();
        }

        return 0;
    }

但是与公司没有直接联系的对象仍然在数据库中,我该怎么办?

但是没有黑客,我想要一个干净的代码......

我知道 ORM Lite 确实是 lite,但是节省子级创建和删除级联的功能对于任何 ORM 都是必不可少的,希望在下一个版本中实现它,遗憾的是没有这些功能,对于简单的项目来说非常好,但是在一个复杂的项目,因为很多头痛,我的皮肤感觉。

欢迎任何帮助!

4

0 回答 0