ORMLite多对多关系 [英] ORMLite many to many relation

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

问题描述

为了做出许多与ORMLite这两个类之间有许多关系:

  @DatabaseTable(tableName值=测试1)
公共类的Test1 {
    @ForeignCollectionField
    私人ForeignCollection<&的Test2 GT; test2Collection;
}@DatabaseTable(tableName值=测试2)
公共类的Test2 {
    @ForeignCollectionField
    私人ForeignCollection<&Test1的GT; test1Collection;
}

我面临到ORMLite在创建表的问题,不知道这个类..

之间的外键

为了使这种关系我必须对每个类一样,添加一个ForeignDtabaseField:

  @DatabaseTable(tableName值=测试1)
    公共类的Test1 {
        @DatabaseField(外资= TRUE,foreignAutoRefresh = TRUE)
    私人的Test2 TEST2;
        @ForeignCollectionField
        私人ForeignCollection<&的Test2 GT; test2Collection;
    }
 @DatabaseTable(tableName值=测试2)
    公共类的Test2 {
        @DatabaseField(外资= TRUE,foreignAutoRefresh = TRUE)
    私人测试1测试1;
        @ForeignCollectionField
        私人ForeignCollection<&Test1的GT; test2Collection;
    }

这似乎是一个奇怪的方式..?


解决方案

  

为了使这种关系我必须对每个类一样,添加一个ForeignDatabaseField:


是的。你的第二个例子是正确的。没有为 ORMLite 没有其他办法能够弄清楚如何使用测试1 正在以的Test2 实例相关联,反之亦然。

请参阅文档:

In order to make a many to many relation between these two class with ORMLite :

@DatabaseTable(tableName = "test1")
public class Test1 {
    @ForeignCollectionField
    private ForeignCollection<Test2> test2Collection;
}

@DatabaseTable(tableName = "test2")
public class Test2{
    @ForeignCollectionField
    private ForeignCollection<Test1> test1Collection;
}

I'm facing to the problem that ORMLite at the tables creation, don't know about the Foreign Keys between this class..

In order to make this relation do I have to add a single ForeignDtabaseField on each class like that :

 @DatabaseTable(tableName = "test1")
    public class Test1 {
        @DatabaseField(foreign = true, foreignAutoRefresh = true)
    private Test2 test2;
        @ForeignCollectionField
        private ForeignCollection<Test2> test2Collection;
    }
 @DatabaseTable(tableName = "test2")
    public class Test2 {
        @DatabaseField(foreign = true, foreignAutoRefresh = true)
    private Test1 test1;
        @ForeignCollectionField
        private ForeignCollection<Test1> test2Collection;
    }

It seems to be a strange way.. ?

解决方案

In order to make this relation do I have to add a single ForeignDatabaseField on each class like that:

Yes. Your second example is correct. There is no other way for ORMLite to be able to figure out how the Test1 that are associated with a Test2 instance and vice versa.

See the docs:

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

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