Hibernate Can';t实时刷新Spring数据中的数据库+;冬眠+;科特林
我想使用Kotlin、Spring Data和Hibernate在while循环中读取数据库值(lastLocationDate)。问题是,当我更改数据库中的值时,this.productDao.findBySerialNumber(“11223344”)将不会刷新循环中的值。我需要重新启动服务以获取新的数据库值。以下是实施方案: TestServiceImplHibernate Can';t实时刷新Spring数据中的数据库+;冬眠+;科特林,hibernate,kotlin,spring-data-jpa,Hibernate,Kotlin,Spring Data Jpa,我想使用Kotlin、Spring Data和Hibernate在while循环中读取数据库值(lastLocationDate)。问题是,当我更改数据库中的值时,this.productDao.findBySerialNumber(“11223344”)将不会刷新循环中的值。我需要重新启动服务以获取新的数据库值。以下是实施方案: TestServiceImpl @Service internal open class TestServiceImpl(private val productDao
@Service
internal open class TestServiceImpl(private val productDao: ProductDao) : TestService {
@Throws(TestException::class)
override fun testProduct() {
var count = 0
while ( count < 36 ) {
val product = this.productDao.findBySerialNumberAndIdProductType("W33DDA", 1L)
val lastLocationDate = product?.lastLocationDate
println("lastLocationDate = $lastLocationDate")
Thread.sleep(5000L)
count += 1
}
}
}
TestController
@RestController
@RequestMapping("/rest/v1")
class TestController(private val testService: TestService) {
@GetMapping("/test", consumes = [MediaType.APPLICATION_JSON_VALUE])
fun testProduct() {
this.testService.testProduct()
}
}
ProductDao
interface ProductDao : JpaRepository<Product, Long> {
fun findBySerialNumber(serialNumber: String): Product?
}
应用程序
@SpringBootApplication(scanBasePackages = arrayOf("XX"))
@EntityScan("XXXX.infrastructure")
@EnableJpaRepositories("XXXX.infrastructure.dao")
class Application
fun main(args: Array<String>) {
runApplication<WebApplication>(*args)
}
@SpringBootApplication(scanBasePackages=arrayOf(“XX”))
@EntityScan(“XXXX.infrastructure”)
@EnableJParepositions(“XXXX.infrastructure.dao”)
班级申请
趣味主线(args:Array){
运行应用程序(*args)
}
有人知道如何实时获取数据库值吗?试试
@Transactional(propagation=propagation.REQUIRES_NEW)
谢谢,我试过了,但没有成功我们找到了一个解决方案,方法是分离查询返回的实体(请参阅:)试试@Transactional(propagation=propagation.REQUIRES_NEW)
谢谢,我尝试过,但没有成功。我们通过分离查询返回的实体找到了解决方案(请参阅:)
@Entity
@Table(name = "Product")
class Product(
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(precision = PRECISION_ID)
var idProduct: Long? = null,
@Column(name = "serialNumber", nullable = false, length = MAX_LENGTH_SERIAL_NUMBER)
var serialNumber: String? = null,
@Column(name = "lastLocationDate", nullable = true)
var lastLocationDate: LocalDateTime? = null
) : AbstractPersistence() {
companion object {
const val MAX_LENGTH_SERIAL_NUMBER = 6
}
override fun equals(other: Any?) = other === this
|| (other is Product && idProduct == other.idProduct)
override fun hashCode() = idProduct!!.hashCode()
}
@SpringBootApplication(scanBasePackages = arrayOf("XX"))
@EntityScan("XXXX.infrastructure")
@EnableJpaRepositories("XXXX.infrastructure.dao")
class Application
fun main(args: Array<String>) {
runApplication<WebApplication>(*args)
}