Neo4j 使用 Spring 数据进行急切/延迟加载
我正在研究 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
注释调用
findOne(xxx)
的方法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)
withfrom
(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!