Skip to content

Commit

Permalink
Merge pull request #78 from OsgiliathEnterprise/embeddedid
Browse files Browse the repository at this point in the history
Avoid NPE
  • Loading branch information
Tcharl authored Aug 5, 2024
2 parents f9431e0 + 66041f3 commit fed7958
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;
import java.util.stream.Stream;

import static net.osgiliath.migrator.core.configuration.DataSourceConfiguration.SINK_PU;
Expand All @@ -53,14 +54,13 @@ public VertexPersister(DataMigratorConfiguration dmc, RawElementProcessor rawEle

@Transactional(transactionManager = SINK_TRANSACTION_MANAGER)
public void persistVertices(Stream<ModelElement> entities) {
entities.map(me -> {
entities.flatMap(me -> {
if (reconcile) {
Object found = entityManager.find(((JpaMetamodelVertex) me.vertex()).entityClass(), rawElementProcessor.getId(me).orElseThrow());
if (null != found) {
return found;
}
return rawElementProcessor.getId(me).map(
id -> entityManager.find(((JpaMetamodelVertex) me.vertex()).entityClass(), id)
).stream();
}
return me.rawElement();
return Optional.of(me.rawElement()).stream();
})
.forEach(entityManager::persist);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private List<Predicate> excludeAlreadyLoaded(MetamodelVertex entityVertex, List<
.map(object ->
builder.not(
builder.equal(
root.get(pk), modelElementProcessor.getId(object)))
root.get(pk), modelElementProcessor.getId(object).orElseThrow()))
).toList()).orElseGet(ArrayList::new);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,16 @@ public Optional<Object> getId(MetamodelVertex metamodelVertex, Object entity) {

private Optional<Object> getRawId(Class entityClass, Object entity) {
// Cannot use getRawFieldValue due to cycle and the @Transactional aspect
log.debug("getting id for entity of class {} and entity {}", entityClass.getSimpleName(), entity);
return getPrimaryKeyGetterMethod(entityClass).map(
primaryKeyGetterMethod -> {
try {
return primaryKeyGetterMethod.invoke(entity);
if (null != entity) {
return primaryKeyGetterMethod.invoke(entity);
} else {
log.error("Cannot get the id because entity is null for entityclass {}", entityClass);
return Optional.empty();
}
} catch (IllegalAccessException | InvocationTargetException e) {
throw new ErrorCallingRawElementMethodException(e);
}
Expand Down
2 changes: 1 addition & 1 deletion report-aggregate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>net.osgiliath.datamigrator</groupId>
<artifactId>data-migrator</artifactId>
<version>1.69-SNAPSHOT</version>
<version>1.70-SNAPSHOT</version>
</parent>

<artifactId>report-aggregate</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion sample-mono/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<parent>
<groupId>net.osgiliath.datamigrator</groupId>
<artifactId>data-migrator</artifactId>
<version>1.69-SNAPSHOT</version>
<version>1.70-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<groupId>net.osgiliath.datamigrator.sample</groupId>
Expand Down

0 comments on commit fed7958

Please sign in to comment.