use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.
the class BasicDriverTest method shouldSaveMultipleObjectsWithWriteProtectionFromRoot.
@Test
public void shouldSaveMultipleObjectsWithWriteProtectionFromRoot() throws Exception {
User avon = new User("Avon Barksdale");
session.save(avon);
User stringer = new User("Stringer Bell");
session.save(stringer);
session.clear();
try {
// save only Avon's properties, protect neighboring nodes from writes
((Neo4jSession) session).addWriteProtection(WriteProtectionTarget.PROPERTIES, object -> (object instanceof User) && !avon.getId().equals(((User) object).getId()));
stringer.setName("Marlo");
avon.befriend(stringer);
session.save(avon);
session.clear();
Collection<User> users = session.loadAll(User.class);
assertThat(users).hasSize(2).extracting(User::getName).containsExactlyInAnyOrder("Avon Barksdale", "Stringer Bell");
} finally {
((Neo4jSession) session).removeWriteProtection(WriteProtectionTarget.PROPERTIES);
}
}
use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.
the class CyclicStructureTest method testCyclicStructure.
/**
* This issue was introduced by #407
*
* @see org.neo4j.ogm.persistence.examples.social.SocialIntegrationTest#shouldSaveObjectsToCorrectDepth
* @see org.neo4j.ogm.persistence.examples.social.SocialIntegrationTest#shouldSaveAllDirectedRelationships
*/
// GH-609
@Test
public void testCyclicStructure() throws Exception {
long numberOfNodes = 10;
long numberOfRefFields = 100;
List<CyclicNodeType> nodes = new ArrayList<>();
for (long i = 0; i < numberOfNodes; ++i) {
nodes.add(new CyclicNodeType());
}
List<RefField> refFields = new ArrayList<>();
for (long i = 0; i < numberOfRefFields; i++) {
RefField field = new RefField().setNodeTypes(nodes);
refFields.add(field);
}
for (CyclicNodeType nodeType : nodes) {
nodeType.setSubordinateNodeTypes(nodes);
nodeType.setRefFields(refFields);
}
Session session = sessionFactory.openSession();
// Make sure the number of builders fits the number of expected relationships
MultiStatementCypherCompiler compiler = (MultiStatementCypherCompiler) mapAndCompile((Neo4jSession) session, nodes.get(0), -1);
List<RelationshipBuilder> newRelationshipBuilders = extractRelationshipBuilder(compiler);
assertThat(newRelationshipBuilders).size().isEqualTo(numberOfNodes * numberOfNodes + (numberOfNodes * numberOfRefFields * 2));
// Make sure the session saves all relationships accordingly
Transaction transaction = session.beginTransaction();
session.save(nodes.get(0), -1);
session.clear();
transaction.commit();
session = sessionFactory.openSession();
Result result = session.query("" + " MATCH (c1:CyclicNodeType) - [s:SUBORDINATE] - (c2:CyclicNodeType)," + " (c1) - [f:HAS_FIELD] -> (r:RefField)" + " RETURN count(distinct c1) as numberOfNodes, count(distinct s) as countRelCyc, " + " count(distinct r) as numberOfRefFields, count(distinct f) as countRelHasField", Collections.emptyMap());
assertThat(result).hasSize(1);
result.queryResults().forEach(record -> {
assertThat(record.get("numberOfNodes")).isEqualTo(numberOfNodes);
assertThat(record.get("numberOfRefFields")).isEqualTo(numberOfRefFields);
assertThat(record.get("countRelCyc")).isEqualTo(numberOfNodes * numberOfNodes);
assertThat(record.get("countRelHasField")).isEqualTo(numberOfNodes * numberOfRefFields);
});
}
use of org.neo4j.ogm.session.Neo4jSession in project LinkAgent by shulieTech.
the class DataSourceWrapUtil method wrap.
public static void wrap(DataSourceMeta<Neo4jSession> neo4jSessionDataSourceMeta) {
if (DataSourceWrapUtil.pressureDataSources.containsKey(neo4jSessionDataSourceMeta)) {
return;
}
// 生成影子库配置
// 初始化影子库配置
DriverConfiguration sourceConfig = Components.driver().getConfiguration();
Configuration shadowConfig = new Configuration();
DriverConfiguration configuration = new DriverConfiguration(shadowConfig);
configuration.setDriverClassName(sourceConfig.getDriverClassName());
configuration.setConnectionPoolSize(sourceConfig.getConnectionPoolSize());
configuration.setEncryptionLevel(sourceConfig.getEncryptionLevel());
configuration.setTrustCertFile(sourceConfig.getTrustCertFile());
configuration.setTrustStrategy(sourceConfig.getTrustStrategy());
String key = DbUrlUtils.getKey(neo4jSessionDataSourceMeta.getUrl(), neo4jSessionDataSourceMeta.getUsername());
ShadowDatabaseConfig shadowDatabaseConfig = GlobalConfig.getInstance().getShadowDatabaseConfig(key);
if (null == shadowDatabaseConfig) {
ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子表或影子库!").setDetail("业务库配置:::url: " + neo4jSessionDataSourceMeta.getUrl()).closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
return;
}
configuration.setURI(shadowDatabaseConfig.getShadowUrl());
configuration.setCredentials(shadowDatabaseConfig.getShadowUsername(), shadowDatabaseConfig.getShadowPassword());
Driver shadowDriver = DriverService.load(configuration);
// 生成影子库session
Neo4jSession sourceSession = neo4jSessionDataSourceMeta.getDataSource();
Neo4JSessionExt shadowSession = new Neo4JSessionExt(metaDataMap.get(sourceSession), shadowDriver);
// 放入本地
DbMediatorDataSource<Neo4jSession> driverConfigurationDbMediatorDataSource = new DriverConfig();
driverConfigurationDbMediatorDataSource.setDataSourceBusiness(sourceSession);
driverConfigurationDbMediatorDataSource.setDataSourcePerformanceTest(shadowSession);
DataSourceWrapUtil.pressureDataSources.put(neo4jSessionDataSourceMeta, driverConfigurationDbMediatorDataSource);
}
use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.
the class UserTest method testDeserialiseUserWithArrayOfEnums.
@Test
public void testDeserialiseUserWithArrayOfEnums() {
MetaData metadata = new MetaData("org.neo4j.ogm.domain.cineasts.annotated");
Neo4jSession session = new Neo4jSession(metadata, true, new UsersRequest());
User user = session.load(User.class, "luanne", 1);
assertThat(user.getLogin()).isEqualTo("luanne");
assertThat(user.getSecurityRoles()).isNotNull();
assertThat(user.getSecurityRoles().length).isEqualTo(2);
}
use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.
the class ElectionTest method shouldAllowVoterToChangeHerMind.
@Test
public void shouldAllowVoterToChangeHerMind() {
Candidate a = new Candidate("A");
Candidate b = new Candidate("B");
Voter v = new Voter("V");
v.candidateVotedFor = b;
session.save(a);
session.save(v);
MappingContext context = ((Neo4jSession) session).context();
assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), null, Voter.class, Candidate.class))).isTrue();
session.clear();
a = session.load(Candidate.class, a.getId());
v = session.load(Voter.class, v.getId());
assertThat(v.candidateVotedFor.getId()).isEqualTo(b.getId());
assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), null, Voter.class, Candidate.class))).isTrue();
v.candidateVotedFor = a;
session.save(v);
session.clear();
session.load(Candidate.class, b.getId());
session.load(Voter.class, v.getId());
assertThat(v.candidateVotedFor.getId()).isEqualTo(a.getId());
assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", a.getId(), null, Voter.class, Candidate.class))).isTrue();
assertThat(context.containsRelationship(new MappedRelationship(v.getId(), "CANDIDATE_VOTED_FOR", b.getId(), null, Voter.class, Candidate.class))).isFalse();
session.clear();
}
Aggregations