在android中使用ORMLite保存Collection类

发布于 2024-11-14 08:56:02 字数 1849 浏览 2 评论 0原文

我有两个类设置如下。我很困惑什么时候需要将某些东西注释为外国收藏,什么时候不需要。这可能听起来很愚蠢,但 ORMLite 文档中没有任何地方说明是否允许非外部集合。如果我有一个 int 列表,它被自动装箱为 Integer ,该怎么办?我可以使用 Collection 上方的标准 @DatabaseField 来保留它吗?根据 ORMLite,外部集合还必须具有反向引用才能工作(对父级的引用,给定一对多关系)。对于下面的示例,我假设您应该将 myBList 注释为外部集合,并将 myA 设为外部对象,但是您如何处理 myStringList?

我在这里看到了示例代码,但它没有回答我的问题: http://ormlite.com/docs/examples< /a>

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;
    }
}

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;
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

终弃我 2024-11-21 08:56:02

@罗伯特是正确的。当 hibernate 持久化一个集合(甚至是一个数组)时,它会使用带有外部 id 的隐藏额外表来实现这一点——换句话说,就是隐藏的外部集合。 ORMLite 试图遵守 KISS 原则,因此您可以“手动”定义外部集合。

我添加了有关存储集合的更多详细信息。

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


这意味着您无法保留 Integer type 因为没有foreign-id。此外,您的代码还可以定义外部集合 CollectionForeignCollection。其中任一者都将使用 ForeignCollection 设置。 ORMLite 不支持列表或其他集合类型。

@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.

双马尾 2024-11-21 08:56:02

如果您想将对象的集合(例如 ArrayList)保存到 ORMLite,最简单的方法是:

@DatabaseField(dataType = DataType.SERIALIZABLE)
private SerializedList<MyObject> myObjects;

并获取我的对象列表:

public List<MyObject> getMyObjects() {
  return myObjects;
}

If you want to save a Collection (such as an ArrayList) of objects to ORMLite the easiest way is this:

@DatabaseField(dataType = DataType.SERIALIZABLE)
private SerializedList<MyObject> myObjects;

and to get my list of objects:

public List<MyObject> getMyObjects() {
  return myObjects;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文