Skip to content

ServiceGroup uses inconsistent ID generation strategy causing duplicate key errors #325

@stanleykc

Description

@stanleykc

Problem

When creating a new service group via the admin UI, a 500 error occurs with:

Duplicate entry '2' for key 'service_groups.PRIMARY'

Root Cause

The ServiceGroup entity uses GenerationType.AUTO for ID generation:

// app/src/main/java/app/model/service/group/ServiceGroup.java:26-28
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

This is inconsistent with all other entities which use GenerationType.IDENTITY:

Entity Strategy
User IDENTITY
AttributeValue IDENTITY
ServiceDefinitionAttribute IDENTITY
Service IDENTITY
JurisdictionUser IDENTITY
RemoteHost IDENTITY
ServiceRequest IDENTITY
JurisdictionBoundaryEntity IDENTITY
ServiceGroup AUTO ← inconsistent

With MySQL, GenerationType.AUTO uses a hibernate_sequence table to track the next ID value. This sequence can get out of sync when data is seeded or imported with explicit IDs.

Recommended Fix

Change ServiceGroup to use GenerationType.IDENTITY to match other entities:

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

This uses MySQL's native AUTO_INCREMENT which is more reliable and consistent with the rest of the codebase.

Workaround

Until fixed, update the hibernate_sequence table (which GenerationType.AUTO actually uses):

-- Check current sequence value
SELECT * FROM hibernate_sequence;

-- Update to a safe value higher than any existing ID
UPDATE hibernate_sequence SET next_val = 10;

-- Optionally also reset MySQL auto-increment (belt and suspenders)
SELECT MAX(id) FROM service_groups;
ALTER TABLE service_groups AUTO_INCREMENT = <max_id + 1>;

Impact

  • Severity: Medium - Admin functionality blocked
  • Affected: Creating new service groups

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions