1

一个星期以来我一直在努力解决这个问题,但似乎仍然找不到解决方案。我正在使用 spring-data-neo4j maven artifact 和以下导致此问题的代码行:

/**
 * 
 */
@Autowired
private UserRepository userRepository;

@Transactional
public void addClassDescriptor(User user, ClassDescriptor classDescriptor) {
    Project project = user.getDefaultProject();
    ManagedFieldAccessorSet<ClassDescriptor> accessorSet = (ManagedFieldAccessorSet<ClassDescriptor>) project.getClassDescriptors();
    accessorSet.add(classDescriptor);
    /*
     * Save the user object after updating the set
     */
    userRepository.save(user);
}

执行该方法时,会出现以下错误

accessorSet.add(classDescriptor);

堆栈跟踪:

org.neo4j.graphdb.NotInTransactionException
    at org.neo4j.kernel.impl.persistence.PersistenceManager.getResource(PersistenceManager.java:252)
    at org.neo4j.kernel.impl.persistence.PersistenceManager.nodeCreate(PersistenceManager.java:155)
    at org.neo4j.kernel.impl.core.NodeManager.createNode(NodeManager.java:270)
    at org.neo4j.kernel.EmbeddedGraphDbImpl.createNode(EmbeddedGraphDbImpl.java:317)
    at org.neo4j.kernel.EmbeddedGraphDatabase.createNode(EmbeddedGraphDatabase.java:103)
    at org.springframework.data.neo4j.support.DelegatingGraphDatabase.createNode(DelegatingGraphDatabase.java:82)
    at org.springframework.data.neo4j.support.mapping.EntityStateHandler.useOrCreateState(EntityStateHandler.java:115)
    at org.springframework.data.neo4j.support.mapping.Neo4jEntityConverterImpl.write(Neo4jEntityConverterImpl.java:145)
    at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister$CachedConverter.write(Neo4jEntityPersister.java:176)
    at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:238)
    at org.springframework.data.neo4j.support.mapping.Neo4jEntityPersister.persist(Neo4jEntityPersister.java:227)
    at org.springframework.data.neo4j.support.Neo4jTemplate.save(Neo4jTemplate.java:295)
    at org.springframework.data.neo4j.fieldaccess.AbstractNodeRelationshipFieldAccessor.getOrCreateState(AbstractNodeRelationshipFieldAccessor.java:97)
    at org.springframework.data.neo4j.fieldaccess.AbstractNodeRelationshipFieldAccessor.createSetOfTargetNodes(AbstractNodeRelationshipFieldAccessor.java:89)
    at org.springframework.data.neo4j.fieldaccess.OneToNRelationshipFieldAccessorFactory$OneToNRelationshipFieldAccessor.setValue(OneToNRelationshipFieldAccessorFactory.java:66)
    at org.springframework.data.neo4j.fieldaccess.ManagedFieldAccessorSet.updateValue(ManagedFieldAccessorSet.java:90)
    at org.springframework.data.neo4j.fieldaccess.ManagedFieldAccessorSet.update(ManagedFieldAccessorSet.java:78)
    at org.springframework.data.neo4j.fieldaccess.ManagedFieldAccessorSet.add(ManagedFieldAccessorSet.java:104)

我的实体如下:(User.java)

@GraphId
private Long id;

@RelatedTo(elementClass = Project.class)
@Fetch
private Set<Project> projects;

(项目.java)

    @GraphId
private Long id;
/**
 * 
 */
@RelatedTo(elementClass = ClassDescriptor.class)
@Fetch
private Set<ClassDescriptor> classDescriptors;

/**
 * 
 */
private boolean defaultProject;

请帮忙 !附件是依赖树。

依赖树

4

3 回答 3

2

我发现在使用集合操作添加关系以及 @Transactional 注释时,您需要获取对 GraphDatabaseService 的引用并显式开始和结束事务:

@Autowired
private GraphDatabaseService graphDb;

@Transactional
public void addRelation() {
  Transaction tx = graphDb.beginTx();
  ...
  tx.success(); //or tx.failure();
  tx.finish();
}
于 2013-04-30T10:12:20.460 回答
1

阿比,

如果它一直在发生,那么我认为您的服务不是一个合适的 bean。你怎么把你的东西连接起来?

您使用的是简单模式还是高级模式 (AspectJ)?

你发布的东西看起来很好很简单,所以我不明白为什么它不起作用......

问候,

拉斯

于 2012-07-16T07:56:44.650 回答
0

您必须在尝试中保护事务...

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;


public class App {
    static Node firstNode = null;
    static GraphDatabaseService graphDb = null;
    public static void main(String[] args) {
        System.out.println("Start ...");
        graphDb = ConnectNeo4j.initDB();
        Transaction tx = graphDb.beginTx();
        try {
            firstNode = graphDb.createNode();
            firstNode.setProperty("message", "Es Geht!");
            System.out.println(firstNode.getProperty("message"));
            tx.success();
    } catch (Exception e) {
        System.err.println(e.getMessage());
    } finally {
        tx.close();
        }
    }
}
于 2014-05-23T23:18:05.310 回答