salih
salih

Reputation: 324

How to add a listener in Hibernate for a specific database row changes?

enter image description here

I am developing a food ordering application, As shown in the image user1 will make payment and order then call API /waitForStatus/orderid To get the confirmation of it, which should return only after Payment Provider confirms back to us.

Payment Provider will update the table as Rejected or Confirmed using API /confirmOrder/orderid

My question is how to add a listener to a specific row and column.To know when provider updates back status

Here is my Entity Class

@Data
@Entity
@Table(name = "Order")
public class Order{

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "ID")
    private Long id;

    
    @Column(name = "STATUS")
    private String status;
}

Here is my JPA Repository class

@Repository
public interface OrderRepository extends JpaRepository<Order, Long> {

    Order getOrderById(Long id);

}

Here is my Service class

@Service
public class OrderDomainService implements IOrderDomainService {
    private final OrderRepository orderRepository;

    public OrderDomainService(OrderRepository orderRepository) {
        this.orderRepository = orderRepository;
    }

   @Override
   public UserOrder getOrderById(Long orderId) {
         return orderRepository.getOrderById(Long id)
   }
   
   @Override
   public UserOrder waitForStatusCongfirmation(Long orderId) {
        
        //<-------------How to handle here
   }

}

Let me know how to listen for a change in specific row, Thank You.

Upvotes: 0

Views: 985

Answers (1)

Christian Beikov
Christian Beikov

Reputation: 16410

There is a great tool called Debezium that you can use to listen for any database changes: https://debezium.io/

Upvotes: 1

Related Questions