在Android中坚持一个集合类ORMLite [英] Persisting a Collection class with ORMLite in android

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

问题描述

我有两个班的设置如下所示。我很困惑,当我需要注释的东西作为一个外来的收集,当我不知道。这也可能听起来很傻,但我在 ORMLite 文件没有说非外国集合是否被允许。如果我有 INT S的一览表中得到autoboxed到整数 S'我可以用一个标准的 @DatabaseField 收集上述只是坚持这一点?外国集合,根据 ORMLite ,还必须有反向引用为它工作(参考父,给予一对多realtionship)。对于下面的例子中,我假设你应该标注 myBList 作为一门集以及使 MYA 异物,但你怎么能处理 myStringList

我所看到的样品code在这里,但它并没有回答我的问题:的http:// ormlite。 COM /文档/例子

 公共类A {

    私人设置< B> myBList =新的HashSet< B>();
    私人列表<字符串> myStringList =新的ArrayList<字符串>();
    专用长ID;
    公开发行A(){​​}

    公共设置< B> getMyBList(){
        返回myBList;
    }
    公共无效setMyBList(集< B> myBList){
        this.myBList = myBList;
    }
    公开名单<字符串> getMyStringList(){
        返回myStringList;
    }
    公共无效setMyStringList(名单<字符串> myStringList){
        this.myStringList = myStringList;
    }
    公共无效SETID(长ID){
        this.id = ID;
    }

    众长的getId(){
        返回ID;
    }
}

公共类B {
    私人诠释敏;
    私人字符串myString的;
    非公开A妙;
    专用长ID;

    市民B(){}

    公开发行A getMyA(){
         返回妙;
    }
    公开发行A setMyA(A一){
        MYA =一个;
    }

    公众诠释getMyInt(){
        返回敏;
    }
    公共无效setMyInt(INT敏){
        this.myInt =敏;
    }
    公共字符串getMyString(){
        返回MyString的;
    }
    公共无效setMyString(字符串MyString的){
        this.myString = myString的;
    }

    公共无效SETID(长ID){
        this.id = ID;
    }

    众长的getId(){
        返回ID;
    }
}
 

解决方案

@Robert是正确的。当Hibernate持久化的集合(甚至数组),它也具有与国外IDS隐藏额外的表 - 换句话说隐藏国外收藏。 ORMLite试图坚持KISS原则,因此有你的手定义外的集合来代替。

我添加了有关存储集的更多细节。

  

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


这意味着你不能坚持一个整数键入因为没有国外-ID。此外,您的code可以定义外集收藏<排序> ForeignCollection<排序> 。要么一会设置一个 ForeignCollection 。 ORMLite不支持列表或其他集合类型。

I have two classes setup like the following. I am confused as to when I need to annotate something as an foreign collection and when I do not. This may also sound silly, but nowhere in the ORMLite documentation does it say whether or not a non-foreign collection is allowed. What if I have a List of ints which get autoboxed into Integers? can I just persist this using a standard @DatabaseField above the Collection? A foreign collection, according to ORMLite, must also have back reference for it to work (a reference to the parent, given a one to many realtionship). For the example below, I am assuming you should annotate myBList as a foreign collection as well as making myA a foreign object, but how could you handle myStringList?

I Have seen sample code here but it doesn't answer my questions: http://ormlite.com/docs/examples

public class A {

    private Set<B> myBList = new HashSet<B>();
    private List<String> myStringList = new ArrayList<String>();
    private long id;    
    public A(){}

    public Set<B> getMyBList() {
        return myBList;
    }
    public void setMyBList(Set<B> myBList) {
        this.myBList = myBList;
    }
    public List<String> getMyStringList() {
        return myStringList;
    }
    public void setMyStringList(List<String> myStringList) {
        this.myStringList = myStringList;
    }
    public void setId(long id){
        this.id = id;
    }

    public long getId(){
        return id;
    }
}

public class B {
    private int myInt;
    private String myString;
    private A myA;
    private long id;

    public B(){}

    public A getMyA(){
         return myA;
    }
    public A setMyA(A a){
        myA = a;
    }

    public int getMyInt() {
        return myInt;
    }
    public void setMyInt(int myInt) {
        this.myInt = myInt;
    }
    public String getMyString() {
        return myString;
    }
    public void setMyString(String myString) {
        this.myString = myString;
    }

    public void setId(long id){
        this.id = id;
    }

    public long getId(){
        return id;
    }
}

解决方案

@Robert is correct. When hibernate persists a collection (or even an array), it does so with hidden extra tables with foreign ids -- in other words hidden foreign collections. ORMLite tries to adhere to the KISS principle and so has you define the foreign collections "by hand" instead.

I've added more details about storing collections.

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


This means that you cannot persist an Integer type because there is no foreign-id. Also, your code can define a foreign collection Collection<Order> or ForeignCollection<Order>. Either one will be set with a ForeignCollection. ORMLite does not support lists or other collection types.

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

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