Код: Выделить всё
@MappedSuperclass
abstract class MyBaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Int = 0
var eid: Int = 0
}
interface MyRepository {
@Transactional
fun saveInsert(entity: T): Optional
}
open class MyRepositoryImpl : MyRepository {
@Autowired
private lateinit var entityManager: EntityManager
@Transactional
override fun saveInsert(entity: T): Optional {
// lock table
entityManager.createNativeQuery("LOCK TABLE myTable WRITE").executeUpdate()
// get current max EID
val result = entityManager.createNativeQuery("SELECT MAX(eid) FROM myTable LIMIT 1").singleResult as? Int ?: 0
// set entities EID with incremented result
entity.eid = result + 1
// test if the table is locked. sending manually 2-3 POST requests to REST
Thread.sleep(5000)
// save
entityManager.persist(entity)
// unlock
entityManager.createNativeQuery("UNLOCK TABLES").executeUpdate()
return Optional.of(entity)
}
}
Подробнее здесь: https://stackoverflow.com/questions/565 ... pring-boot