繁体   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