fixed entity-manager-issue with cross-store
This commit is contained in:
@@ -35,11 +35,14 @@ import org.springframework.data.graph.neo4j.support.node.Neo4jNodeBacking;
|
||||
import org.springframework.data.graph.neo4j.support.node.PartialNeo4jEntityInstantiator;
|
||||
import org.springframework.data.graph.neo4j.support.relationship.ConstructorBypassingGraphRelationshipInstantiator;
|
||||
import org.springframework.data.graph.neo4j.support.relationship.Neo4jRelationshipBacking;
|
||||
import org.springframework.data.graph.neo4j.transaction.ChainedTransactionManager;
|
||||
import org.springframework.orm.jpa.EntityManagerFactoryUtils;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.persistence.transaction.NaiveDoubleTransactionManager;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.jta.JtaTransactionManager;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.validation.Validator;
|
||||
|
||||
@@ -88,7 +91,7 @@ public class Neo4jConfiguration {
|
||||
gdc.setGraphDatabaseService(getGraphDatabaseService());
|
||||
gdc.setRelationshipEntityInstantiator(new ConstructorBypassingGraphRelationshipInstantiator());
|
||||
if (isUsingCrossStorePersistence()) {
|
||||
gdc.setGraphEntityInstantiator(new PartialNeo4jEntityInstantiator(new Neo4jConstructorGraphEntityInstantiator(), getEntityManagerFactory().createEntityManager()));
|
||||
gdc.setGraphEntityInstantiator(new PartialNeo4jEntityInstantiator(new Neo4jConstructorGraphEntityInstantiator(), entityManagerFactory));
|
||||
}
|
||||
else {
|
||||
gdc.setGraphEntityInstantiator(new Neo4jConstructorGraphEntityInstantiator());
|
||||
@@ -137,7 +140,7 @@ public class Neo4jConfiguration {
|
||||
JtaTransactionManager jtaTm = new JtaTransactionManager();
|
||||
jtaTm.setTransactionManager(new SpringTransactionManager(getGraphDatabaseService()));
|
||||
jtaTm.setUserTransaction(new UserTransactionImpl(getGraphDatabaseService()));
|
||||
return new NaiveDoubleTransactionManager(jpaTm, jtaTm);
|
||||
return new ChainedTransactionManager(jpaTm, jtaTm);
|
||||
}
|
||||
else {
|
||||
PlatformTransactionManager transactionManager = new JtaTransactionManager();
|
||||
|
||||
@@ -117,7 +117,6 @@ public class PartialNodeEntityState<ENTITY extends NodeBacked> extends DefaultEn
|
||||
node = graphDatabaseContext.createNode();
|
||||
persistForeignId(node, id);
|
||||
setPersistentState(node);
|
||||
entity.setPersistentState(node);
|
||||
log.info("User-defined constructor called on class " + entity.getClass() + "; created Node [" + entity.getPersistentState() + "]; Updating metamodel");
|
||||
graphDatabaseContext.postEntityCreation(entity);
|
||||
} else {
|
||||
|
||||
@@ -19,9 +19,11 @@ package org.springframework.data.graph.neo4j.support.node;
|
||||
import org.neo4j.graphdb.Node;
|
||||
import org.springframework.data.graph.core.NodeBacked;
|
||||
import org.springframework.data.graph.neo4j.fieldaccess.PartialNodeEntityState;
|
||||
import org.springframework.orm.jpa.EntityManagerFactoryUtils;
|
||||
import org.springframework.persistence.support.EntityInstantiator;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
|
||||
/**
|
||||
* Entity instantiator for Node entities that takes into account that the entity is persisted in a JPA store as well.
|
||||
@@ -32,12 +34,12 @@ import javax.persistence.EntityManager;
|
||||
public class PartialNeo4jEntityInstantiator implements EntityInstantiator<NodeBacked, Node> {
|
||||
|
||||
private final Neo4jConstructorGraphEntityInstantiator delegate;
|
||||
private EntityManagerFactory entityManagerFactory;
|
||||
|
||||
private final EntityManager entityManager;
|
||||
|
||||
public PartialNeo4jEntityInstantiator(Neo4jConstructorGraphEntityInstantiator delegate, EntityManager entityManager) {
|
||||
public PartialNeo4jEntityInstantiator(Neo4jConstructorGraphEntityInstantiator delegate, EntityManagerFactory entityManagerFactory) {
|
||||
this.delegate = delegate;
|
||||
this.entityManager = entityManager;
|
||||
this.entityManagerFactory = entityManagerFactory;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,10 +54,14 @@ public class PartialNeo4jEntityInstantiator implements EntityInstantiator<NodeBa
|
||||
public <T extends NodeBacked> T createEntityFromState(Node n, Class<T> entityClass) {
|
||||
if (n.hasProperty(PartialNodeEntityState.FOREIGN_ID)) {
|
||||
final Object foreignId = n.getProperty(PartialNodeEntityState.FOREIGN_ID);
|
||||
final T result = entityManager.find(entityClass, foreignId);
|
||||
final T result = entityManager().find(entityClass, foreignId);
|
||||
result.setPersistentState(n);
|
||||
return result;
|
||||
}
|
||||
return delegate.createEntityFromState(n, entityClass);
|
||||
}
|
||||
|
||||
private EntityManager entityManager() {
|
||||
return EntityManagerFactoryUtils.getTransactionalEntityManager(entityManagerFactory);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +81,8 @@ public class RecommendationTest {
|
||||
Assert.assertNotNull("jpa-id",user.getId());
|
||||
Assert.assertNotNull("node",user.getPersistentState());
|
||||
User user2 = user("Jane");
|
||||
user.knows(user2);
|
||||
user.getFriends().add(user2);
|
||||
//user.knows(user2);
|
||||
Assert.assertEquals(user2, user.getFriends().iterator().next());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -80,9 +80,6 @@
|
||||
<property name="relationshipEntityStateFactory" ref="relationshipEntityStateFactory"/>
|
||||
</bean>
|
||||
|
||||
<!--
|
||||
<bean class="org.springframework.persistence.graph.Neo4jSimpleNodePropertyStorageForeignStoreKeyManager"/>
|
||||
-->
|
||||
|
||||
<bean id="graphDatabaseService" class="org.neo4j.kernel.ImpermanentGraphDatabase"
|
||||
destroy-method="shutdown" scope="singleton">
|
||||
@@ -99,7 +96,7 @@
|
||||
<constructor-arg>
|
||||
<bean class="org.springframework.data.graph.neo4j.support.node.Neo4jConstructorGraphEntityInstantiator"/>
|
||||
</constructor-arg>
|
||||
<constructor-arg ref="entityManager"/>
|
||||
<constructor-arg ref="entityManagerFactory"/>
|
||||
</bean>
|
||||
</property>
|
||||
<property name="conversionService">
|
||||
|
||||
Reference in New Issue
Block a user