From 55b01e7907720c80d6f054a7ff4027988a950737 Mon Sep 17 00:00:00 2001
From: Jan-Niclas Struewer <j.n.struewer@gmail.com>
Date: Tue, 16 May 2023 14:14:51 +0200
Subject: [PATCH] fixed database lazy loading

---
 .../iem/dataprovider/sarif/SarifExtensions.kt    |  2 +-
 .../taskManager/tasks/ProcessTask.kt             | 16 ++++++----------
 src/main/resources/application.properties        |  7 +++++--
 3 files changed, 12 insertions(+), 13 deletions(-)

diff --git a/src/main/kotlin/de/fraunhofer/iem/dataprovider/sarif/SarifExtensions.kt b/src/main/kotlin/de/fraunhofer/iem/dataprovider/sarif/SarifExtensions.kt
index 74ec79f6..bf906062 100644
--- a/src/main/kotlin/de/fraunhofer/iem/dataprovider/sarif/SarifExtensions.kt
+++ b/src/main/kotlin/de/fraunhofer/iem/dataprovider/sarif/SarifExtensions.kt
@@ -11,7 +11,7 @@ import java.sql.Timestamp
 import java.time.Instant
 import java.util.*
 
-fun de.fraunhofer.iem.dataprovider.sarif.Rule.asDbObject(): de.fraunhofer.iem.dataprovider.toolResult.Rule {
+fun de.fraunhofer.iem.dataprovider.sarif.Rule.asDbObject(): Rule {
     val rule = Rule()
     rule.sarifRuleId = this.id
     rule.shortDescription = this.shortDescription.text
diff --git a/src/main/kotlin/de/fraunhofer/iem/dataprovider/taskManager/tasks/ProcessTask.kt b/src/main/kotlin/de/fraunhofer/iem/dataprovider/taskManager/tasks/ProcessTask.kt
index cc7ec22e..b9fe3691 100644
--- a/src/main/kotlin/de/fraunhofer/iem/dataprovider/taskManager/tasks/ProcessTask.kt
+++ b/src/main/kotlin/de/fraunhofer/iem/dataprovider/taskManager/tasks/ProcessTask.kt
@@ -58,19 +58,15 @@ sealed class SarifTask : ProcessTask() {
 
         logger.info("Handle Process return in $javaClass")
         val sarifResult = getSarifFromFilePath(resultPath)
-//        val repo = repository.findById(repoId)
+        val repo = repository.findById(repoId)
         val sarifDb = sarifResult.asDbObject()
         val results = toolRunRepository.saveAll(sarifDb)
-        results.forEach {
-            val dbres = toolRunRepository.findById(it.id!!).get()
-//            Hibernate.initialize(dbres.toolResults)
-            logger.info(dbres.toString())
-        }
-//        repo.ifPresent {
+
+        repo.ifPresent {
 //            Hibernate.initialize(it.toolRuns) // TODO: this fails because the session seems to be closed and add won't work because toolRuns are loaded lazily
-//            it.toolRuns.addAll(results)
-//            repository.save(it)
-//        }
+            it.addToolResults(results)
+            repository.save(it)
+        }
         sendResult(sarifResult)
     }
 
diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties
index 8dd67df9..2601e356 100644
--- a/src/main/resources/application.properties
+++ b/src/main/resources/application.properties
@@ -3,6 +3,8 @@ OPENCODE_GITLAB_URL=https://gitlab.opencode.de/
 OPENCODE_GITLAB_TOKEN=${OPENCODE_TOKEN}
 GITHUB_URL=https://gitlab.opencode.de/
 GITHUB_TOKEN=${GITHUB_TOKEN}
+GIT_PROJECT_PATH=${GIT_PROJECT_PATH}
+ODC_OUTPUT_PATH=${ODC_OUTPUT_PATH}
 spring.datasource.url=jdbc:postgresql://localhost:5432/dataprovider
 spring.datasource.username=sa
 spring.datasource.password=password
@@ -10,6 +12,7 @@ spring.jpa.generate-ddl=true
 spring.jpa.show-sql=true
 spring.jpa.hibernate.ddl-auto=update
 spring.jpa.properties.hibernate.format_sql=true
-GIT_PROJECT_PATH=${GIT_PROJECT_PATH}
-ODC_OUTPUT_PATH=${ODC_OUTPUT_PATH}
+spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
+spring.jpa.properties.hibernate.bytecode.use_reflection_optimizer=false
+spring.jpa.open-in-view=false
 #logging.level.root=DEBUG
\ No newline at end of file
-- 
GitLab