Neo4j 使用 Spring 数据进行急切/延迟加载

发布于 2024-12-08 01:15:21 字数 1693 浏览 0 评论 0原文

我正在研究 Neo4j,并且有一个关于对象急切/延迟加载的问题。可以说我有类 Trolley ,其中有 Set (带有 getters/setters)。如果我执行以下操作:

Trolley t = new Trolley(...); // create empty trolley
t.addItem(f);  // add one item to the trolley
t.persist(); // persist the object 

然后我会根据 nodeId 找到手推车:

repo.findOne(xxx); // returns the trolley successfully

当我执行以下操作时:

trolley.getItems().size() 

这是空的。我想这是预期的行为。是否有类似于 JPA 的机制,其中会话/tx 打开以动态加载集合。

代码:

@NodeEntity
public class Trolley
{
    @Indexed
    private String name;

    @RelatedTo
    private Set<Item> items;

    public Trolley(){}

    public Trolley(String name)
    {
        this.name = name;
    }

    public void addItem(Item item)
    {
        this.items.add(item);
    }

    public Set<Item> getItems()
    {
        return items;
    }

}

@NodeEntity
public class Item
{
    private String name;

    public Item(){}

    public Item(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }
}

@Test
public void trolleyWithItemPersist()
{
    Trolley trolley = new Trolley("trolley1").persist();

    // Persisting - however I would've expected a cascade to
    // occur when adding to the collection.
    Item item =  new Item("item1").persist();

    // now add to the trolley
    trolley.addItem(item);

    // persist
    trolley.persist();

    // Now use repo to get trolley
    Trolley loadedTrolley = trolleyRepository.findOne(trolley.getNodeId());

    // should have one item
    assertEquals(1, loadedTrolley.getItems().size());

}

I'm investigating Neo4j and have a question with regards to object eager/lazy loading. Lets say I have class Trolley with has Set<Item> (with getters/setters). If I do the following:

Trolley t = new Trolley(...); // create empty trolley
t.addItem(f);  // add one item to the trolley
t.persist(); // persist the object 

I then later find the trolley based on the nodeId:

repo.findOne(xxx); // returns the trolley successfully

When I do something like:

trolley.getItems().size() 

this is empty. I guess this is the intended behaviour. Is there any mechanism similar to JPA where is the session/tx is open to load the collection dynamically.

Code:

@NodeEntity
public class Trolley
{
    @Indexed
    private String name;

    @RelatedTo
    private Set<Item> items;

    public Trolley(){}

    public Trolley(String name)
    {
        this.name = name;
    }

    public void addItem(Item item)
    {
        this.items.add(item);
    }

    public Set<Item> getItems()
    {
        return items;
    }

}

@NodeEntity
public class Item
{
    private String name;

    public Item(){}

    public Item(String name)
    {
        this.name = name;
    }

    public String getName()
    {
        return name;
    }
}

@Test
public void trolleyWithItemPersist()
{
    Trolley trolley = new Trolley("trolley1").persist();

    // Persisting - however I would've expected a cascade to
    // occur when adding to the collection.
    Item item =  new Item("item1").persist();

    // now add to the trolley
    trolley.addItem(item);

    // persist
    trolley.persist();

    // Now use repo to get trolley
    Trolley loadedTrolley = trolleyRepository.findOne(trolley.getNodeId());

    // should have one item
    assertEquals(1, loadedTrolley.getItems().size());

}

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

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

发布评论

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

评论(1

此刻的回忆 2024-12-15 01:15:21

注释调用 findOne(xxx) 的方法

@Transactional 

Afaik,在 Spring Data Jpa 中,要填充延迟加载字段,您需要使用 (org.springframework.transaction.annotation.Transactional)< /code>

也许它也适用于 neo4j...

我并不是真正熟练的 Spring Data 开发人员,但这是我知道检索延迟加载字段的唯一方法。如果有人有更好的解决方案,请随时写出来!

Afaik, in Spring Data Jpa, to populate an lazy loaded field you need to annotate the method which call the findOne(xxx) with

@Transactional 

from (org.springframework.transaction.annotation.Transactional)

Maybe it works also with neo4j...

I'm not really an skilled developper on Spring Data but this is the only way I know to retrieve lazy loaded fields. If someone has a better solution, feel free to write it!

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