简体   繁体   English

GAE / JPA / Datastore如何查询未拥有的列表

[英]GAE/JPA/Datastore How to query on an unowned List

I am trying to do something simple I thought but it is not working as expected using the Big Table and JPA, I am using datanucleus jpa v2. 我正在尝试做一些我认为简单的事情,但是使用Big Table和JPA却无法按预期工作,我使用的是datanucleus jpa v2。

@Entity
public class Inventory extends DatastoreObject {
...

    /**
     * List of all inventory items in this object.
     */
    @OneToMany(mappedBy = "inventory", fetch = FetchType.LAZY, cascade={CascadeType.ALL})
    private List<InventoryItem> inventoryItems = new ArrayList<InventoryItem>();

...

}



@Entity
public class InventoryItem extends DatastoreObject {

    ...

    @ManyToOne
    private Inventory inventory;

    ...    
}

When I look at the Datastore Viewer, I see entities with their correlating relationship columns. 当我查看数据存储查看器时,会看到带有关联关系列的实体。 This is the inventory table which has a list of ids for the inventory items. 这是库存表,其中包含库存物品的ID列表。

Inventory Table
    Key    Write Ops    ID/Name         inventoryItems 
    agxzbWFydGJhcnNpdGVyDwsSCUludmVudG9yeRgwDA  8   48      [InventoryItem(69)] 


Inventory Item Table
    Key    Write Ops    ID/Name     inventory_id
    agxzbWFydGJhcnNpdGVyEwsSDUludmVudG9yeUl0ZW0YRQw     14  69  Inventory(48)

My question is how do I fetch all the inventory item's where their inventory_id is for example 48. I know I can fetch the inventory object and do an array sublist but this seems really inefficient. 我的问题是如何获取所有清单项目(例如,它们的inventory_id为48的清单)。我知道我可以获取清单对象并创建一个数组子列表,但这似乎效率很低。

When I try to do a query it doesn't work as expected and I understand some of the reasons as joins are not supported but it seems that it would work if I could access the inventory_id column of then inventory item table but it seems that I cannot. 当我尝试执行查询时,它无法按预期运行,并且我理解了一些不支持联接的原因,但是,如果我可以访问库存项目表中的ventory_id列,则似乎可以使用,但是我似乎不能。

When I try simple queries this won't work. 当我尝试简单的查询时,它将不起作用。

"Select from InventoryItem.class.getName() item where item.inventory.id = :inventoryId"

I was hoping this would work in datanucleus, I understand that it is typically a join but if datanucleus knows that it is an unowned relationship and that the inventory object is mapped by it's id couldn't this work somehow? 我希望它可以在数据核中工作,我知道它通常是一个联接,但是如果数据核知道这是一个无主关系,并且库存对象是通过其ID映射的,则无法以某种方式工作吗? I understand that the .id attribute wouldn't make any sense as it is an arbitrary token but it seems there should be a way to do this type of query with an unowned @OneToMany(mappedBy="inventory") configuration. 我知道.id属性没有任何意义,因为它是一个任意令牌,但似乎应该有一种方法可以使用无名的@OneToMany(mappedBy =“ inventory”)配置进行此类型的查询。

I tried the other way also by assigning the object as a parameter but to my surprise this did not work either. 我也尝试通过将对象分配为参数来尝试另一种方法,但令我惊讶的是,这也没有用。

"Select from InventoryItem.class.getName() item where item.inventory = :inventory"

Here I try the query by first fetching the object and then trying to query using that object as the parameter but again this did not work unless the objects was "embedded" 在这里,我首先通过获取对象,然后尝试使用该对象作为参数进行查询,但是再次尝试,除非对象被“嵌入”,否则此方法不起作用

My preferred solution would be the first query operation but I am pretty sure this is not possible using big table. 我的首选解决方案是第一个查询操作,但是我很确定使用大表是不可能的。 I am still confused though why the second operation does not work. 我仍然感到困惑,尽管为什么第二项操作不起作用。

Any help would be appreciated. 任何帮助,将不胜感激。

Well in such cases what I prefer instead of putting any relationships between entities, I simply create Key for one of my Entity and I save the same key in the other entity as a Foreign Key, so if I need to query the child entity I use Foreign Key as get back my results. 好了,在这种情况下,我宁愿不为实体之间放置任何关系,而只是为我的一个实体创建键,然后将相同的键保存为外键,所以如果我需要查询我使用的子实体外键作为取回我的结果。

I have recently started using Objectify and I suggest you that if you need such kind of problems you can use objectify to persist data to Appengine Datastore. 我最近开始使用Objectify,建议您如果需要此类问题,可以使用Objectify将数据持久保存到Appengine Datastore。 It is very efficient and simple to learn and use. 它非常有效且易于学习和使用。

Is that JPA query ok? JPA查询可以吗? It should look like this: 它看起来应该像这样:

select item from InventoryItem item where item.inventory.id = :inventoryId

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

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