What you describe is the expected behavior. The transaction is committed after the method ends, the commit leads to the unique constraint violation.
Adding @Transactional
to the service methods makes the transaction end after the call to the service method the transaction is committed. Removing it makes the transaction commit after the call to the repository method.
Also why do you have a BO a Facade and a Repository?! Basically the BO and Facade are the same IMHO.
TO fix your your problem have your GarageRepository
extend JpaRepository
instead of PagingAndSortingRepository
and call the saveAndFlush
method instead of save
. This will execute the sql (not committing the transaction) and trigger a constraint violation exception.
Another solution is to instead of doing a try/catch in your BO create and Aspect which does the conversion. Saves you coding the try/catch each time you need it.