Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,10 @@ public Notifier getByUuidAndTime(String uuid, Instant time) {

final Timestamp timestamp = Timestamp.from(time);

final String sql = "SELECT " + "a.id AS id, a.uuid AS uuid," + "COALESCE(b.changedate, a.changedate) AS changedate,"
+ "a.creationdate AS creationdate, a.change_user_id AS change_user_id,"
+ "COALESCE(b.registrationnumber, a.registrationnumber) AS registrationnumber," + "COALESCE(b.firstname, a.firstname) AS firstname,"
+ "COALESCE(b.lastname, a.lastname) AS lastname," + "COALESCE(b.address, a.address) AS address," + "COALESCE(b.email, a.email) AS email,"
+ "COALESCE(b.phone, a.phone) AS phone, "+ "COALESCE(b.agentfirstname, a.agentfirstname) AS agentfirstname, "
+ "COALESCE(b.agentlastname, a.agentlastname) AS agentlastname "+ "FROM notifier a " + "LEFT JOIN " + "(SELECT * FROM notifier_history "
+ "WHERE uuid = :uuid AND changedate <= CAST(:changeDate AS TIMESTAMP) " + "ORDER BY changedate DESC LIMIT 1" + ") b ON a.uuid = b.uuid "
+ "WHERE a.uuid = :uuid";
final String sql = "SELECT * FROM ("
+ "SELECT * FROM (SELECT * FROM notifier_history b WHERE b.uuid = :uuid AND changedate <= CAST(:changeDate AS TIMESTAMP) ORDER BY changedate DESC LIMIT 1) "
+ "UNION SELECT * FROM notifier a WHERE a.uuid = :uuid) "
+ "WHERE uuid = :uuid AND changedate <= CAST(:changeDate AS TIMESTAMP) ORDER BY changedate DESC LIMIT 1;";
Comment on lines +94 to +97

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Avoid SELECT * across UNION; use UNION ALL; drop trailing semicolon; simplify filters

  • SELECT * + UNION between base and history risks column-order/type mismatches when mapping to Notifier; verify both tables have identical column sets in identical order. Prefer enumerating columns explicitly.
  • UNION incurs unnecessary dedup; UNION ALL is faster here.
  • The outer WHERE repeats uuid/changedate; push changedate into both branches and keep a single ORDER BY/LIMIT.
  • Trailing semicolon can break some JPA providers.

Apply:

-final String sql = "SELECT * FROM ("
-    + "SELECT * FROM (SELECT * FROM notifier_history b WHERE b.uuid = :uuid AND changedate <= CAST(:changeDate AS TIMESTAMP) ORDER BY changedate DESC LIMIT 1) "
-    + "UNION SELECT * FROM notifier a WHERE a.uuid = :uuid) "
-    + "WHERE uuid = :uuid AND changedate <= CAST(:changeDate AS TIMESTAMP) ORDER BY changedate DESC LIMIT 1;";
+final String sql =
+    "SELECT t.* FROM (" +
+    "  SELECT b.* FROM notifier_history b " +
+    "  WHERE b.uuid = :uuid AND b.changedate <= CAST(:changeDate AS TIMESTAMP) " +
+    "  UNION ALL " +
+    "  SELECT a.* FROM notifier a " +
+    "  WHERE a.uuid = :uuid AND a.changedate <= CAST(:changeDate AS TIMESTAMP) " +
+    ") t " +
+    "ORDER BY t.changedate DESC LIMIT 1";

If history/base schemas diverge, explicitly list columns in the exact Notifier mapping order instead of using *.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
final String sql = "SELECT * FROM ("
+ "SELECT * FROM (SELECT * FROM notifier_history b WHERE b.uuid = :uuid AND changedate <= CAST(:changeDate AS TIMESTAMP) ORDER BY changedate DESC LIMIT 1) "
+ "UNION SELECT * FROM notifier a WHERE a.uuid = :uuid) "
+ "WHERE uuid = :uuid AND changedate <= CAST(:changeDate AS TIMESTAMP) ORDER BY changedate DESC LIMIT 1;";
final String sql =
"SELECT t.* FROM (" +
" SELECT b.* FROM notifier_history b " +
" WHERE b.uuid = :uuid AND b.changedate <= CAST(:changeDate AS TIMESTAMP) " +
" UNION ALL " +
" SELECT a.* FROM notifier a " +
" WHERE a.uuid = :uuid AND a.changedate <= CAST(:changeDate AS TIMESTAMP) " +
") t " +
"ORDER BY t.changedate DESC LIMIT 1";
🤖 Prompt for AI Agents
In
sormas-backend/src/main/java/de/symeda/sormas/backend/person/notifier/NotifierService.java
around lines 94-97, the query uses SELECT * with UNION and a trailing semicolon
and repeats filters; replace SELECT * with an explicit, ordered list of columns
matching the Notifier entity mapping (ensuring identical order and types for
both branches), change UNION to UNION ALL, move the changedate filter into both
the history and base SELECT branches (keep a single ORDER BY ... LIMIT in the
outer query), and remove the trailing semicolon so the JPA provider won’t choke;
if schemas diverge, enumerate columns exactly in the same order for both
SELECTs.


Query query = em.createNativeQuery(sql, Notifier.class);
query.setParameter("uuid", uuid);
Expand Down
Loading