I was going through the spring docs and came to know about this rollbackFor thing which's used to define the class for which the transaction must be rolled back in case exception occurs of that type. So tried the following-
But it does not work. It basically inserts the record and throws the runtime exception and does not rollback.
Am I right in assuming that that's the way it is supposed to work?
By default transactions @Transactional will automatically rollback when any RuntimeException is thrown. So rollbackFor and noRollbackFor are used to customize specifically what exceptions you want to rollback for. So say you have 15 different RuntimeExceptions that might be thrown and you want only two of them to actually rollback your transaction, then you use the rollbackFor property to list just those two. If you want 12 to rollback and 3 not to rollback, then you use the noRollbackFor property. Basically they are the opposite of each other and you choose the one that will cause you the least amount of typing.