ORMLite 中的集合 [英] Collections in ORMLite

查看:24
本文介绍了ORMLite 中的集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我想在我的 android 应用程序中使用 ORMlite 保留一些集合数据.例如:

Hello I want persist some collections data with ORMlite in my android app. For example :

class Person {
  @DatabaseField(generatedId=true)
  private int id;

  @DatabaseField
  private String name;

  @DatabaseField
  private String surname;

  @ForeignCollectionField
  private Collections<Phone> phones;
}

class Phone {
  @DatabaseField(generatedId=true)
  private int id;

  @DatabaseField
  private String number;

  @DatabaseField
  private String type; 
}

我应该如何坚持这个集合.

How i should do it to persist this collection.

编辑

我按照你说的使用 ForeignCollectionField.但是现在我有另一个问题 - 是否可以这样构造(我稍微改变了课程)

I use ForeignCollectionField as you told me. But now i have another question - is possible construction like that (I changed class a little)

@DatabaseTable
public class Student {

    @DatabaseField(generatedId=true)
    private int id;

    @DatabaseField
    private String name;

    @DatabaseField
    private String surname;

    @DatabaseField
    private String semestr;

    @ForeignCollectionField
    private Collection<Adres> adres;
}

public class Adres implements Serializable {

    @DatabaseField(generatedId=true)
    private int id;

    @DatabaseField(foreign=true, columnName="student_id")
    private Student student;

    @DatabaseField
    private String miasto;

    @DatabaseField
    private String ulica;

    @DatabaseField
    private String nr;

    @ForeignCollectionField
    private Collection<Phone> phone;
}

public class Phone {

    @DatabaseField(generatedId=true)
    private int id;

    @DatabaseField
    private String number;

    @DatabaseField(foreign=true, columnName="adres_id" )
    private Adres adres;
}

我建造了这个结构,我把它放在 3 个 tabelas 中.

I build this structure, i have it in 3 tabelas.

Student queryForStudentMarcin = simpleDao.queryForId(studentMarcin.getId());
Collection<Adres> adres = queryForStudentMarcin.getAdres();

for (Adres a : adres) {
    int id = a.getId();
    String miasto = a.getMiasto();
    String ulica = a.getUlica();
    String nr = a.getNr();
}

然后我这样做并出现错误:

Then i do this and error appear:

04-03 15:55:58.392: ERROR/AndroidRuntime(6506): FATAL EXCEPTION: main
04-03 15:55:58.392: ERROR/AndroidRuntime(6506): java.lang.NullPointerException
04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at com.j256.ormlite.field.FieldType.buildForeignCollection(FieldType.java:579)
04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at com.j256.ormlite.stmt.mapped.BaseMappedQuery.mapRow(BaseMappedQuery.java:58)
04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at com.j256.ormlite.stmt.SelectIterator.nextThrow(SelectIterator.java:107)
04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at com.j256.ormlite.stmt.SelectIterator.next(SelectIterator.java:120)
04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at com.example.orm.MainActivity.dodajDoBazyCollection(MainActivity.java:140)
04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at com.example.orm.MainActivity$2.onClick(MainActivity.java:66)
04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at android.view.View.performClick(View.java:2408)
04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at android.view.View$PerformClick.run(View.java:8816)
04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at android.os.Handler.handleCallback(Handler.java:587)
04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at android.os.Looper.loop(Looper.java:123)
04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at java.lang.reflect.Method.invokeNative(Native Method)
04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at java.lang.reflect.Method.invoke(Method.java:521)
04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-03 15:55:58.392: ERROR/AndroidRuntime(6506):     at dalvik.system.NativeStart.main(Native Method)

有什么建议吗?

Edit2

问题是从数据库读取数据.这个程序工作正常,现在很好,但我只是用它来理解 ORMlite.非常感谢格雷的帮助.

Problem was read data from db. This procedure work ok, it's now nice, but i just use it to understand ORMlite. Thx very much Gray for help.

protected void czytajZBazy() throws SQLException {

    Dao<Student, Integer> simpleDao = getHelper().getSimpleDao();
    Dao<Adres, Integer> adresDao = getHelper().getAdresDao();
    Dao<Phone, Integer> phoneDao = getHelper().getPhoneDao();

    /**
     * wyswietl adresy dla studenta Marcin
     */
    QueryBuilder<Student, Integer> qbStudent = simpleDao.queryBuilder();;
    Where<Student, Integer> where = qbStudent.where();
    where.eq("name", "Marcin");
    PreparedQuery<Student> prepare = qbStudent.prepare();
    CloseableIterator<Student> iterator = simpleDao.iterator(prepare);
    while(iterator.hasNext()){
        Log.v("inside ", "loop");
        Student next = iterator.next();
        Log.v("sudent", ""+next.getId()+" "+next.getName()+" "+next.getSurname());

        QueryBuilder<Adres, Integer> adresQB = adresDao.queryBuilder();
        Where<Adres, Integer> where2 = adresQB.where();

        where2.eq("student_id", next.getId());

        PreparedQuery<Adres> preparedAdres = adresQB.prepare();
        CloseableIterator<Adres> iterator2 = adresDao.iterator(preparedAdres);

        while(iterator2.hasNext()){
            Log.v("iterator po adresie", "!!");
            Adres nextAdres = iterator2.next();
            Log.v("Adres", ""+nextAdres.getId()+" "+nextAdres.getMiasto());
            //wypisz telefony 
            QueryBuilder<Phone, Integer> queryPhone = phoneDao.queryBuilder();
            Where<Phone, Integer> where3 = queryPhone.where();
            where3.eq("adres_id", nextAdres.getId());

            PreparedQuery<Phone> preparedPhone = queryPhone.prepare();
            CloseableIterator<Phone> iterator3 = phoneDao.iterator(preparedPhone);

            while(iterator3.hasNext()){
                Log.v("inside phone iterator", "");
                Phone next2 = iterator3.next();
                Log.v("phone", ""+next2.getId()+" "+next2.getNumber());
            }
            iterator3.close();
        }
        iterator2.close();
    }
    iterator.close();
}

推荐答案

有许多资源和手册可以帮助在 Android 下使用 ORMLite.

There are a number of resources and manuals that help with the usage of ORMLite under Android.

http://ormlite.com/docs/android
http://ormlite.com/docs/android-examples

要持久化集合,您必须首先为类获取一个 Dao,然后使用 Dao 创建每个对象:

To persist a collection, you must first get a Dao for the class and then create each of the objects using the Dao:

Dao<Person, Integer> personDao =
    DaoManager.createDao(androidConnectionSource, Person.class);
for (Person person : personCollection) {
    personDao.create(person);
}

您的 Phone 类也几乎相同.

Pretty much the same for your Phone class as well.

他实际上是在询问如何使用 ORMLite 的 ForeignCollectionField 功能.可在此处找到相关文档:

He was really asking about how to use the ForeignCollectionField feature of ORMLite. The documentation for this can be found here:

http://ormlite.com/docs/foreign-collection

还有一个使用它的示例项目.在上面的示例中,您缺少 Phone 中的 Person 字段.如果这个人有一组电话对象,那么每个电话对象都需要有一个对应的人.这在 ORMLite 中称为外部对象".

There is also an example project that uses it. In the above example, you are missing a Person field inside of Phone. If the person has a collection of phone objects then each phone object needs to have a corresponding person. This is called a "foreign object" in ORMLite.

http://ormlite.com/docs/foreign-object

这篇关于ORMLite 中的集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆