|
20 | 20 | import com.wisemapping.model.*; |
21 | 21 | import jakarta.persistence.EntityManager; |
22 | 22 | import jakarta.persistence.Query; |
| 23 | +import jakarta.persistence.PersistenceContext; |
23 | 24 | import jakarta.persistence.TypedQuery; |
24 | 25 | import jakarta.persistence.criteria.CriteriaBuilder; |
25 | 26 | import jakarta.persistence.criteria.CriteriaQuery; |
|
43 | 44 | public class MindmapManagerImpl |
44 | 45 | implements MindmapManager { |
45 | 46 | private static final Logger logger = LoggerFactory.getLogger(MindmapManagerImpl.class); |
46 | | - @Autowired |
| 47 | + @PersistenceContext |
47 | 48 | private EntityManager entityManager; |
48 | 49 | @Autowired |
49 | 50 | private jakarta.persistence.EntityManagerFactory entityManagerFactory; |
@@ -309,113 +310,19 @@ public void updateMindmap(@NotNull Mindmap mindMap, boolean saveHistory) { |
309 | 310 | @Transactional(propagation = Propagation.REQUIRES_NEW) |
310 | 311 | public void updateMindmapSpamInfo(@NotNull com.wisemapping.model.MindmapSpamInfo spamInfo) { |
311 | 312 | assert spamInfo != null : "Update MindmapSpamInfo: SpamInfo is required!"; |
312 | | - // Validate that we have a valid mindmap ID |
313 | 313 | if (spamInfo.getMindmapId() == null || spamInfo.getMindmapId() < 0) { |
314 | 314 | throw new IllegalArgumentException("Invalid mindmap ID for spam info: " + spamInfo.getMindmapId()); |
315 | 315 | } |
316 | | - // "Last Win" strategy: Use native SQL to force update regardless of conflicts |
317 | | - // This ensures the latest data always wins, even in high concurrency scenarios |
318 | | - try { |
319 | | - // Use Java Calendar for consistent timestamp handling across all databases |
320 | | - Calendar now = Calendar.getInstance(); |
321 | | - // Get database-specific SQL based on database product name |
322 | | - String sql = getUpsertSqlForDatabase(); |
323 | | - // Convert SpamStrategyType enum to Character for native SQL |
324 | | - Character spamTypeCode = spamInfo.getSpamTypeCode() != null ? spamInfo.getSpamTypeCode().getCode() : null; |
325 | | - entityManager.createNativeQuery(sql) |
326 | | - .setParameter(1, spamInfo.getMindmapId()) |
327 | | - .setParameter(2, spamInfo.isSpamDetected()) |
328 | | - .setParameter(3, spamInfo.getSpamDetectionVersion()) |
329 | | - .setParameter(4, spamTypeCode) // Convert enum to char |
330 | | - .setParameter(5, spamInfo.getSpamDescription()) |
331 | | - .setParameter(6, now) // created_at |
332 | | - .setParameter(7, now) // updated_at |
333 | | - .setParameter(8, now) // updated_at for UPDATE clause |
334 | | - .executeUpdate(); |
335 | | - } catch (Exception e) { |
336 | | - // If native SQL fails, log the error and throw a runtime exception |
337 | | - // This ensures we don't fall back to JPA merge which causes optimistic locking |
338 | | - // issues |
339 | | - throw new RuntimeException("Failed to update MindmapSpamInfo for mindmap ID: " + |
340 | | - spamInfo.getMindmapId() + " (native SQL failed)", e); |
341 | | - } |
342 | | - } |
343 | | - |
344 | | - /** |
345 | | - * Gets the appropriate UPSERT SQL statement based on the database product. |
346 | | - * Supports MySQL, PostgreSQL, and HSQLDB. |
347 | | - * |
348 | | - * @return Database-specific UPSERT SQL statement |
349 | | - */ |
350 | | - private String getUpsertSqlForDatabase() { |
351 | | - try { |
352 | | - // Get database dialect name |
353 | | - // Use entityManagerFactory which is already autowired, as unwrapping |
354 | | - // entityManager to EMF might fail |
355 | | - String dialectName = entityManagerFactory |
356 | | - .unwrap(org.hibernate.SessionFactory.class) |
357 | | - .getSessionFactoryOptions() |
358 | | - .getServiceRegistry() |
359 | | - .getService(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment.class) |
360 | | - .getDialect() |
361 | | - .getClass() |
362 | | - .getSimpleName(); |
363 | | - |
364 | | - // Determine SQL based on Hibernate dialect class name |
365 | | - if (dialectName.contains("PostgreSQL")) { |
366 | | - // PostgreSQL uses ON CONFLICT |
367 | | - return """ |
368 | | - INSERT INTO MINDMAP_SPAM_INFO (mindmap_id, spam_detected, spam_detection_version, spam_type_code, spam_description, created_at, updated_at) |
369 | | - VALUES (?, ?, ?, ?, ?, ?, ?) |
370 | | - ON CONFLICT (mindmap_id) DO UPDATE SET |
371 | | - spam_detected = EXCLUDED.spam_detected, |
372 | | - spam_detection_version = EXCLUDED.spam_detection_version, |
373 | | - spam_type_code = EXCLUDED.spam_type_code, |
374 | | - spam_description = EXCLUDED.spam_description, |
375 | | - updated_at = ? |
376 | | - """; |
377 | | - } else if (dialectName.contains("HSQL")) { |
378 | | - // HSQLDB uses MERGE |
379 | | - return """ |
380 | | - MERGE INTO MINDMAP_SPAM_INFO AS t |
381 | | - USING (VALUES(?, ?, ?, ?, ?, ?, ?)) AS s(mindmap_id, spam_detected, spam_detection_version, spam_type_code, spam_description, created_at, updated_at) |
382 | | - ON t.mindmap_id = s.mindmap_id |
383 | | - WHEN MATCHED THEN UPDATE SET |
384 | | - spam_detected = s.spam_detected, |
385 | | - spam_detection_version = s.spam_detection_version, |
386 | | - spam_type_code = s.spam_type_code, |
387 | | - spam_description = s.spam_description, |
388 | | - updated_at = ? |
389 | | - WHEN NOT MATCHED THEN INSERT (mindmap_id, spam_detected, spam_detection_version, spam_type_code, spam_description, created_at, updated_at) |
390 | | - VALUES (s.mindmap_id, s.spam_detected, s.spam_detection_version, s.spam_type_code, s.spam_description, s.created_at, s.updated_at) |
391 | | - """; |
392 | | - } else { |
393 | | - // MySQL uses ON DUPLICATE KEY UPDATE |
394 | | - return """ |
395 | | - INSERT INTO MINDMAP_SPAM_INFO (mindmap_id, spam_detected, spam_detection_version, spam_type_code, spam_description, created_at, updated_at) |
396 | | - VALUES (?, ?, ?, ?, ?, ?, ?) |
397 | | - ON DUPLICATE KEY UPDATE |
398 | | - spam_detected = VALUES(spam_detected), |
399 | | - spam_detection_version = VALUES(spam_detection_version), |
400 | | - spam_type_code = VALUES(spam_type_code), |
401 | | - spam_description = VALUES(spam_description), |
402 | | - updated_at = ? |
403 | | - """; |
404 | | - } |
405 | | - } catch (Exception e) { |
406 | | - // Fallback to MySQL syntax if detection fails |
407 | | - logger.warn("Failed to detect database dialect, using MySQL syntax: {}", e.getMessage()); |
408 | | - return """ |
409 | | - INSERT INTO MINDMAP_SPAM_INFO (mindmap_id, spam_detected, spam_detection_version, spam_type_code, spam_description, created_at, updated_at) |
410 | | - VALUES (?, ?, ?, ?, ?, ?, ?) |
411 | | - ON DUPLICATE KEY UPDATE |
412 | | - spam_detected = VALUES(spam_detected), |
413 | | - spam_detection_version = VALUES(spam_detection_version), |
414 | | - spam_type_code = VALUES(spam_type_code), |
415 | | - spam_description = VALUES(spam_description), |
416 | | - updated_at = ? |
417 | | - """; |
| 316 | + MindmapSpamInfo managed = entityManager.find(MindmapSpamInfo.class, spamInfo.getMindmapId()); |
| 317 | + if (managed == null) { |
| 318 | + managed = new MindmapSpamInfo(); |
| 319 | + managed.setMindmapId(spamInfo.getMindmapId()); |
| 320 | + entityManager.persist(managed); |
418 | 321 | } |
| 322 | + managed.setSpamDetected(spamInfo.isSpamDetected()); |
| 323 | + managed.setSpamDetectionVersion(spamInfo.getSpamDetectionVersion()); |
| 324 | + managed.setSpamTypeCode(spamInfo.getSpamTypeCode()); |
| 325 | + managed.setSpamDescription(spamInfo.getSpamDescription()); |
419 | 326 | } |
420 | 327 |
|
421 | 328 | /** |
@@ -1117,7 +1024,7 @@ private void saveHistory(@NotNull final Mindmap mindMap) { |
1117 | 1024 | history.setCreationTime(Calendar.getInstance()); |
1118 | 1025 | history.setEditor(mindMap.getLastEditor()); |
1119 | 1026 | history.setMindmapId(mindMap.getId()); |
1120 | | - entityManager.merge(history); |
| 1027 | + entityManager.persist(history); |
1121 | 1028 | } |
1122 | 1029 |
|
1123 | 1030 | /** |
|
0 commit comments