use of org.neo4j.ogm.metadata.MetaData in project neo4j-ogm by neo4j.
the class InvalidMetadataAutoIndexManagerTest method shouldCheckPropertiesExistsForCompositeIndex.
@Test(expected = MetadataException.class)
public void shouldCheckPropertiesExistsForCompositeIndex() {
MetaData metadata = new MetaData(NoPropertyCompositeIndexEntity.class.getName());
ClassInfo classInfo = metadata.classInfo(NoPropertyCompositeIndexEntity.class.getName());
classInfo.getCompositeIndexes();
}
use of org.neo4j.ogm.metadata.MetaData in project neo4j-ogm by neo4j.
the class EntityUtils method setIdentity.
public static void setIdentity(Object entity, Long identity, MetaData metaData) {
ClassInfo classInfo = metaData.classInfo(entity);
if (classInfo.hasIdentityField()) {
FieldInfo identityField = classInfo.identityField();
identityField.write(entity, identity);
} else if (identity == null) {
// Reset any generated field if the new value is null in case the generated values is not an internal id.
classInfo.fieldsInfo().fields().stream().filter(f -> f.getAnnotations().has(Id.class) && f.getAnnotations().has(GeneratedValue.class)).findFirst().ifPresent(generatedField -> generatedField.write(entity, null));
}
}
use of org.neo4j.ogm.metadata.MetaData in project neo4j-ogm by neo4j.
the class DeleteDelegate method deleteOneOrMoreObjects.
// TODO : this is being done in multiple requests at the moment, one per object. Why not put them in a single request?
private void deleteOneOrMoreObjects(List<?> objects, Set<Object> neighbours) {
Set<Object> notified = new HashSet<>();
if (session.eventsEnabled()) {
for (Object affectedObject : neighbours) {
if (!notified.contains(affectedObject)) {
session.notifyListeners(new PersistenceEvent(affectedObject, Event.TYPE.PRE_SAVE));
notified.add(affectedObject);
}
}
}
for (Object object : objects) {
MetaData metaData = session.metaData();
ClassInfo classInfo = metaData.classInfo(object);
if (classInfo == null) {
session.warn(object.getClass().getName() + " is not an instance of a persistable class");
} else {
MappingContext mappingContext = session.context();
Long id = mappingContext.optionalNativeId(object).filter(possibleId -> possibleId >= 0).orElseGet(() -> {
session.warn(String.format("Instance of class %s has to be reloaded to be deleted. This can happen if the session has " + "been cleared between loading and deleting or using an object from a different transaction.", object.getClass()));
return classInfo.getPrimaryIndexOrIdReader().apply(object).map(primaryIndexOrId -> session.load(object.getClass(), (Serializable) primaryIndexOrId)).flatMap(reloadedObject -> mappingContext.optionalNativeId(reloadedObject)).orElse(-1L);
});
if (id >= 0) {
Statement request = getDeleteStatement(object, id, classInfo);
if (session.eventsEnabled() && !notified.contains(object)) {
session.notifyListeners(new PersistenceEvent(object, Event.TYPE.PRE_DELETE));
notified.add(object);
}
RowModelRequest query = new DefaultRowModelRequest(request.getStatement(), request.getParameters());
session.doInTransaction(() -> {
try (Response<RowModel> response = session.requestHandler().execute(query)) {
if (request.optimisticLockingConfig().isPresent()) {
List<RowModel> rowModels = response.toList();
session.optimisticLockingChecker().checkResultsCount(rowModels, request);
}
if (metaData.isRelationshipEntity(classInfo.name())) {
session.detachRelationshipEntity(id);
} else {
session.detachNodeEntity(id);
}
// enabled in the first place.
if (notified.contains(object)) {
session.notifyListeners(new PersistenceEvent(object, Event.TYPE.POST_DELETE));
}
}
}, Transaction.Type.READ_WRITE);
}
}
}
if (session.eventsEnabled()) {
for (Object affectedObject : neighbours) {
if (notified.contains(affectedObject)) {
session.notifyListeners(new PersistenceEvent(affectedObject, Event.TYPE.POST_SAVE));
}
}
}
}
use of org.neo4j.ogm.metadata.MetaData in project neo4j-ogm by neo4j.
the class LoadByInstancesDelegate method findCommonClassInfo.
/**
* @param objects
* @param <T>
* @return A class info that is part of the hierarchy of the distinct object types contained in {@code objects}.
*/
private <T> ClassInfo findCommonClassInfo(Collection<T> objects) {
MetaData metaData = session.metaData();
Set<ClassInfo> infos = objects.stream().map(//
Object::getClass).distinct().map(//
metaData::classInfo).map(//
LoadByInstancesDelegate::getRootClassInfo).collect(toSet());
if (infos.size() != 1) {
throw new MappingException("Can't find single supertype for " + infos);
}
return infos.iterator().next();
}
use of org.neo4j.ogm.metadata.MetaData 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);
}
Aggregations