hibernate是否有内部的“提交id”?

kokeuurv  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(349)
@file:Suppress("DuplicatedCode")

package test

import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.data.repository.CrudRepository
import javax.annotation.PostConstruct
import javax.persistence.*

fun main(args: Array<String>)
{
    runApplication<TestApplication>(*args)
}

@SpringBootApplication
class TestApplication(
    val personRepository: PersonRepository,
    val vehicleRepository: VehicleRepository
)
{
    @PostConstruct
    fun init()
    {
        var p1 = personRepository.save(Person(name = "Fred", age = 30))

        println(" ")
        p1.vehicles += vehicleRepository.save(Vehicle(type = "Car", brand = "BMW"))
        personRepository.save(p1)

        println(" ")
        p1.vehicles += vehicleRepository.save(Vehicle(type = "Car", brand = "BMW"))
        personRepository.save(p1)

        println(" ")
        p1.vehicles += vehicleRepository.save(Vehicle(type = "Car", brand = "BMW"))
        personRepository.save(p1)
    }
}

interface PersonRepository : CrudRepository<Person, Long>
interface VehicleRepository : CrudRepository<Vehicle, Long>

@Entity
data class Person(
    @Id
    @GeneratedValue
    val id: Long = 0,

    val name: String,

    val age: Int,

    @OneToMany
    @OrderColumn
    @JoinColumn(name = "personId")
    val vehicles: MutableList<Vehicle> = ArrayList()
)

@Entity
data class Vehicle(

    @Id
    @GeneratedValue
    val id: Long = 0,

    var type: String,

    var brand: String
)

日志
hibernate:从hibernate\序列中选择next\ val作为id\ val进行更新
hibernate:更新hibernate\u sequence set next\u val=?下一个值在哪里?
休眠:在person(age,name,id)中插入值(?,?)
hibernate:从hibernate\序列中选择next\ val作为id\ val进行更新
hibernate:更新hibernate\u sequence set next\u val=?下一个值在哪里?
休眠:插入车辆(品牌、类型、id)值(?,?)
休眠:选择[…]
休眠:选择[…]
hibernate:更新车辆集person\u id=?,vehicles\u order=?其中id=?
hibernate:从hibernate\序列中选择next\ val作为id\ val进行更新
hibernate:更新hibernate\u sequence set next\u val=?下一个值在哪里?
休眠:插入车辆(品牌、类型、id)值(?,?)
休眠:选择[…]
休眠:选择[…]
hibernate:更新车辆集person\u id=?,vehicles\u order=?其中id=?
hibernate:更新车辆集person\u id=?,vehicles\u order=?其中id=?
hibernate:从hibernate\序列中选择next\ val作为id\ val进行更新
hibernate:更新hibernate\u sequence set next\u val=?下一个值在哪里?
休眠:插入车辆(品牌、类型、id)值(?,?)
休眠:选择[…]
休眠:选择[…]
hibernate:更新车辆集person\u id=?,vehicles\u order=?其中id=?
hibernate:更新车辆集person\u id=?,vehicles\u order=?其中id=?
hibernate:更新车辆集person\u id=?,vehicles\u order=?其中id=?
问题
每增加一辆车,hibernate就会再次更新所有其他车。我注意到如果我改变 personRepository.save(p1)p1 = personRepository.save(p1) ,工作正常,只更新新车。为什么?hibernate是否有内部“提交id”?
另外,如何将此代码转换为事务?
谢谢。

ippsafx7

ippsafx71#

每次呼叫 save 正在呼叫 EntityManager#merge 它将当前对象状态刷新到数据库,可能会返回一个新对象,然后该对象将被“管理”,这意味着对该对象所做的更改将在事务结束时自动刷新。您可以使用 @Transactional 注解,但不确定这是否也适用于 @PostConstruct . 在事务中 save 是托管的,因此在事务结束时自动刷新更改。您可以使用如下代码:

@PostConstruct
@Transactional
fun init()
{
    var p1 = personRepository.save(Person(name = "Fred", age = 30))
    p1.vehicles += vehicleRepository.save(Vehicle(type = "Car", brand = "BMW"))
    p1.vehicles += vehicleRepository.save(Vehicle(type = "Car", brand = "BMW"))
    p1.vehicles += vehicleRepository.save(Vehicle(type = "Car", brand = "BMW"))
}

相关问题