Campaign repository

CampaignRepository

The CampaignRepository interface provides access to Campaign entities in the database. It extends several standard Spring Data interfaces and includes support for safe entity retrieval within a transaction using SynchronousAccessRepository.

Repository Example:

public interface CampaignRepository extends 
    JpaRepository<Campaign, Long>,
    JpaSpecificationExecutor<Campaign>,
    SynchronousAccessRepository<Campaign, Long> {

    @Query("select c.id from Campaign c where c.executionType = 'AUTOMATIC' and c.automaticCampaignStatus = 'SCHEDULED' and c.active = true")
    List<Long> findAutomaticScheduledCampaigns();
}

Inherited Interfaces:

  • JpaRepository — provides basic CRUD operations

  • JpaSpecificationExecutor — enables filtering support via CampaignFilter

  • SynchronousAccessRepository — a custom interface from timveroOS that provides the getSync(id) method: This method ensures the entity is retrieved with a lock until the current transaction completes — which is crucial for concurrent access scenarios (e.g., during automatic campaign executions)

Additional Methods:

    @Query("select c.id from Campaign c where c.executionType = 'AUTOMATIC' and c.automaticCampaignStatus = 'SCHEDULED' and c.active = true")
    List<Long> findAutomaticScheduledCampaigns();

This method is used by the CampaignExecutionProducer component to retrieve all active automatic campaigns scheduled for execution.

Usage Example:

Campaign campaign = campaignRepository.getSync(campaignId);

The getSync method ensures that the retrieved Campaign entity is synchronized with the current transaction — which is critical for components operating in a distributed environment or when using Shedlock.

Last updated

Was this helpful?