简体   繁体   English

Android OrmLite外键/外集合

[英]Android OrmLite Foreign Key / Foreign Collection

On my recent project on Android Im using OrmLite to model one-to-many relations as well as simple one-to-one relations. 在我最近的Android Im项目上,使用OrmLite建模一对多关系以及简单的一对一关系。 Ive noticed that with one-to-many relations, the child holds the foreign key reference to the parent in the database. Ive注意到,通过一对多关系,子级在数据库中拥有对父级的外键引用。 Still when loading the parent object OrmLite knows what to do and loads the collection of child elements which is, of course, the desired behavior. 在加载父对象时,OrmLite仍然知道该怎么做并加载子元素的集合,这当然是所需的行为。

However, with simple one-to-one relationships, the parent object seems to be required holding the foreign key column in order to achieve the same behavior. 但是,对于简单的一对一关系,似乎需要父对象持有外键列才能实现相同的行为。

So the actual question: Is it possible to have OrmLite loading the child objects in one-to-one relations when the foreign key is only set in the child, as it is the standard behavior with one-to-many relations? 因此,实际的问题是:当仅在子项中设置外键时,OrmLite是否可以一对一关系加载子对象,因为这是一对多关系的标准行为?

Here is some example code: 这是一些示例代码:

@DatabaseTable
public class Parent
{  

@DatabaseField(foreign = true)
private Child1 child1;

@ForeignCollectionField
private Collection<Child2> children2;

}

So in child 1 and 2 I need to have the reference to the Parent like this: 因此,在孩子1和2中,我需要像这样引用父对象:

 public class Child1 / Child2
{       

    @DatabaseField(foreign = true)
    private Parent parent;
}

So when saving a child2 I simply set 因此,在保存孩子2时,我只需设置

child2.setParent(parent);
child2Dao.create(child2)

But in order to arrive at the same behavior when querying the parent including the child1 and child2 I would have to save the relationship the other way around: 但是,为了在查询包括child1和child2的父对象时达到相同的行为,我必须以另一种方式保存关系:

parent.setChild1(child1)
parentDao.create(parent)

This is very inconvenient as I want either child1/child2 both holding the foreign key of the parent or the other way around. 这非常不方便,因为我要让child1 / child2都持有父级的外键或相反。 But a mixture seems somewhat ugly! 但是混合似乎有点丑陋!

Any ideas how to achieve that? 有什么想法要实现吗?

Ive searched for answers to that question here but couldnt find one. 我在这里搜索了该问题的答案,但找不到答案。 In case it is a duplicate, Im sorry! 万一重复,对不起!

Thank you very much! 非常感谢你!

Edit: 编辑:

To be more precisely: Is it possible to set the Foreign Key in my child1 table and still be able to obtain child1 when querying for the Parent. 更准确地说:是否可以在我的child1表中设置外键,并且在查询Parent时仍然能够获得child1。 ORMLite does it automagically for the foreign collection of child2. ORMLite会自动对child2的外部集合进行处理。 I want the same behavior for child1. 我想要child1的行为相同。 But when I set 但是当我设定

child1.setParent(parent);
child1Dao.create(child1);

and then make a query 然后进行查询

Parent parent = parentDao.queryForId(1)

only child2 is obtained since the Parent Table does not have a reference (foreign key) to the child1. 由于父表没有对child1的引用(外键),因此仅获得child2。 Only child1 has a reference to the parent. 仅child1引用父项。

So: I want either have OrmLite automatically update the parent foreign key column or tell Ormlite to still obtain child1 even though no foreign key is set in the parent (as this is exactly the case with the collection of child2). 因此:我希望让OrmLite自动更新父外键列,或者告诉Ormlite即使在父中未设置任何外键的情况下仍获取child1(因为child2的集合就是这种情况)。 Is this somehow possible? 这可能吗? Or not? 或不?

I hope this is not too confusing :) Im totally aware that it would be simpler to just set the foreign key in the parent. 我希望这不要太令人困惑:)我完全意识到,仅在父级中设置外键会更简单。 But I really dislike that approach as I will end up with some children having a foreign key of the parent and some that do not. 但是我真的不喜欢这种方法,因为我最终会遇到一些孩子拥有父母的外键,而另一些孩子却没有。

Thank you very much 非常感谢你

ORMLite supports the concept of "foreign" objects where one or more of the fields correspond to an object are persisted in another table in the same database. ORMLite支持“外部”对象的概念,其中与一个对象相对应的一个或多个字段保留在同一数据库的另一个表中。 For example, if you had an Order objects in your database and each Order had a corresponding Account object, then the Order object would have foreign Account field. 例如,如果您的数据库中有一个Order对象,而每个Order都有一个对应的Account对象,那么Order对象将具有外部Account字段。 With foreign objects, just the id field from the Account is persisted to the Order table as the column "account_id". 对于异物,只有帐户的id字段作为“ account_id”列保留在订单表中。 For example, the Order class might look something like: 例如,Order类可能类似于:

@DatabaseTable(tableName = "orders")
public class Order {

    @DatabaseField(generatedId = true)
    private int id;

    @DatabaseField(canBeNull = false, foreign = true)
    private Account account;
    …
}

When the Order table was created, something like the following SQL would be generated: 创建Order表时,将生成类似以下SQL的内容:

CREATE TABLE `orders`
   (`id` INTEGER AUTO_INCREMENT , `account_id` INTEGER,
    PRIMARY KEY (`id`)); 

When you are creating a field with a foreign object, please note that the foreign object will not automatically be created for you. 当您使用异物创建字段时,请注意,不会自动为您创建异物。 If your foreign object has a generated-id which is provided by the database then you need to create it before you create any objects that reference it. 如果您的异物具有数据库提供的generated-id,那么您需要先创建它,然后再创建引用它的任何对象。 For example: 例如:

Account account = new Account("Jim Coakley");
accountDao.create(account);
// this will create the account object and set any generated ids

// now we can set the account on the order and create it
Order order = new Order("Jim Sanders", 12.34);
order.setAccount(account);
…
orderDao.create(order);

A foreign collection allows you to add a collection of orders on the account table. 外部收款允许您在帐户表上添加订单的收款。 Whenever an Account object is returned by a query or refreshed by the DAO, a separate query is made over the order table and a collection of orders is set on the account. 每当通过查询返回或通过DAO刷新Account对象时,都会对订单表进行单独的查询,并在该帐户上设置订单集合。 All of the orders in the collection have a corresponding foreign object that matches the account. 集合中的所有订单都有一个与该帐户匹配的对应异物。 For example: 例如:

public class Account {
    …
    @ForeignCollectionField(eager = false)
    ForeignCollection<Order> orders;
    …
}

In the above example, the @ForeignCollectionField annotation marks that the orders field is a collection of the orders that match the account. 在上面的示例中, @ForeignCollectionField批注标记订单字段是与该帐户匹配的订单的集合。 The field type of orders must be either ForeignCollection or Collection – no other collections are supported because they are much heavier with many methods to support. 订单的字段类型必须为ForeignCollection或Collection-不支持其他任何collection,因为它们要用很多方法来支持很多。

Source: http://ormlite.com/ 资料来源: http//ormlite.com/

According to ORMLite documentation , the DatabaseField is, by default, with canBeNull = true. 根据ORMLite文档 ,默认情况下,DatabaseField的canBeNull = true。 So, it is not necessary to set the Parent when creating a Child1. 因此,创建Child1时不必设置父级。 To obtain the Childs in the Parent and vice-versa, you can set foreignAutoRefresh = true. 要获取父级中的子级,反之亦然,可以设置foreignAutoRefresh = true。

If you want to leave the foreign key column in only one table, maybe you can achieve that by setting maxForeignAutoRefreshLevel = 1. 如果只想将外键列留在一个表中,则可以通过将maxForeignAutoRefreshLevel设置为1来实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM