【发布时间】:2015-05-08 17:13:21
【问题描述】:
我对与数据相关的技术还很陌生,并且遇到了很多问题。我的代码与 Spring Data Neo4j 相关,但我认为这个问题也可能与其他框架相关(如 JPA 实现)。
为了简单起见,假设我有两个相关的实体
@NodeEntity public class A { @RelatedTo Set<B> bs /* Other fields, getters and setters*/ }
@NodeEntity public class B { @RelatedTo A a; /* Other fields, getters and setters*/}
两个@Repository 来访问它们
@Repositoty
public interface ARepository extends CRUDRepository<A> { /* The usual methods based on name */ }
@Repositoty
public interface BRepository extends CRUDRepository<B> { /* The usual methods based on name */ }
以及服务层上的两个接口
@Service
public interface AService { /* Some useful methods such as CRUD operations*/ }
@Service
public interface BService { /* Some useful methods such as CRUD operations*/ }
显然,这两个接口都有一个类,用于实现所使用的特定数据库的方法(在本例中为AServiceNeo4j 和BServiceNeo4j)
好的,在所有这些样板代码之后,这里是真正的问题:
- 首先,我必须在哪里初始化
bs?我见过很多人在声明字段时直接在 POJO 类中执行此操作(private Set<B> bs = new HashSet<B>();)。这是一个很好的解决方案吗?它对性能没有负面影响吗?更一般地说,这个解决方案的优缺点是什么,而不是例如在@Service类上做它(就像我下面的代码)? - 为了向集合中添加一个元素,我是否应该始终获取整个集合,然后在其上调用添加并最终存储/更新实体?还是有更简洁的方法可以避免将整个集合加载到内存中?
- 在我的服务层上使用
addB()方法是否有意义(再次,如下面的示例)?我还没有看到这个解决方案;相反,在某些示例中,我看到直接在 POJO 类中声明了此方法(因此是addB(B b)方法)。在其他示例中,我根本没有看到addB()方法;相反,业务逻辑只是调用a.getBs().add(new B())。最佳解决方案是什么?为什么?
我提出的解决方案是这样的
@Service
public class AServiceNeo4j
{
/* The other usual methods based on name */
public void addB(A a, B b)
{
// Start transaction, start try block
if (a.getBs() == null)
a.setBs(new HashSet<B>() );
a.getBs().add(b);
// Store a, add catch and finally blocks
}
}
所以,从我的业务逻辑来看,我可以这样做:
A a = new A();
AService aService = // get the service bean from the applicationContext
aService.create(a);
B b = new B();
aService.addB(a, b);
相对于
A a = new A();
AService aService = // get the service bean from the applicationContext
aService.create(a);
B b = new B();
a.getB(s).add(b); // To work, the set should be instantiated in the POJO class (see point 1)
aService.update(a);
这两种解决方案的优势是什么?建议的方法是什么?
此外,对于这两种解决方案,框架不会自动更新对象。我的意思是,对于第一个解决方案,调用 a.getBs().size() 返回 0(或 null,取决于 POJO 类的实现)。调用b.getA() 的第二个解决方案也会出现同样的问题,
为了使这些方法正常工作,我必须从数据存储中检索本地对象以获得更新的值。类似的东西
a = aService.find(a); // or aService.fetch(a);
a.getBs().size();
或
b = bService.find(b); // or bService.fetch(b);
b.getA();
这是为什么?不应该由底层框架自动完成吗?如果不是,这是标准的处理方式吗?
【问题讨论】:
标签: spring orm neo4j spring-data spring-data-neo4j