簡體   English   中英

休眠 @OneToOne 不映射 id

[英]Hibernate @OneToOne does not map id

我正在嘗試使用 kotlin、springboot 和 hibernate 編寫簡單的世界生成器,並且我在實體中有很多關系,但其中一個不起作用。 程序生成國家和城市,但在數據庫中,我的“資本”ID 為空

實體:

@Entity
data class City(
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        var id:Long? = null,
        val name: String,
        @OneToMany
        @JoinColumn(name="CityId")
        val flats: List<Flat>)

@Entity
data class Country(
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        var id:Long? = null,
        val name: String,
        @OneToOne(fetch = FetchType.EAGER)
        val capital: City,
        @OneToMany
        @JoinColumn(name="CountryId")
        val cities: List<City>)

國家生成器:

@Component
class CountryGenerator @Autowired constructor(val util: Util) {

    fun getRandomCountry(): Country = util.getObj(
            Country(null,
                    gen.country().name(),
                    CityGenerator(util).getRandomCapital(),
                    getCities())
    ) as Country

    private fun getCities(): List<City> =
            IntStream.range(0, rnd.nextInt(MAX_CITIES_NUMBER!!) + MIN_CITIES_NUMBER!!)
                    .mapToObj { CityGenerator(util).getRandomCity() }
                    .toList()
}

城市生成器:

@Component
class CityGenerator @Autowired constructor(val util: Util) {


    fun getRandomCity() = util.getObj(
            City(null,
                    getCityName(),
                    getListOfFlats())
    ) as City

    fun getRandomCapital() = util.getObj(
            City(null,
                    getCapital(),
                    getListOfFlats())
    ) as City

    private fun getListOfFlats(): List<Flat> =
            IntStream.range(0, rnd.nextInt(MAX_FLATS_NUMBER!!) + MIN_FLATS_NUMBER!!)
                    .mapToObj { FlatGenerator(util).getFlat() }
                    .toList()

    private fun getCapital() = gen.country().capital()

    private fun getCityName(): String = gen.address().city()
}

Country_id 在首都為空

任何想法有什么問題?

編輯:

保存到數據庫:

@Component
class Util(private val personRepository: PersonRepository,
           private val flatRepository: FlatRepository,
           private val cityRepository: CityRepository,
           private val countryRepository: CountryRepository,
           private val worldRepository: WorldRepository) {

    fun getObj(obj: Any): Any {
        return when (obj) {
            is Person -> this.personRepository.save(obj)
            is Flat -> flatRepository.save(obj)
            is City -> cityRepository.save(obj)
            is Country -> countryRepository.save(obj)
            is World -> worldRepository.save(obj)
            else -> throw IllegalArgumentException("Wrong object");
        }
    }

編輯2:

方法 Util.getObj() 返回正確的對象: getObj 返回正確的對象

確認Util.getObj方法確實在保存新城市並返回一個帶有 id 的城市。

此外,如果需要這種關系,您應該在OneToOne注釋中使用optional = false屬性:

@OneToOne(fetch = FetchType.EAGER, optional = false)
@JoinColumn(name="capital_id", nullable=false)
val capital: City

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM