简体   繁体   中英

Hibernate @OneToOne does not map id

I'm trying to write simple world generator using kotlin, springboot and hibernate, and I have many relations in Entities but one of them not working. Program generate the Countries and cities, but in DB I have a null ID for 'capital'

Entities:

@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>)

Country generator:

@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()
}

City generator:

@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 在首都为空

Any ideas what is wrong with it?

EDIT:

Saving to DB:

@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");
        }
    }

EDIT2:

Method Util.getObj() returns correct objects: getObj 返回正确的对象

Confirm that the method Util.getObj is really saving the new city and returning a city with an id.

Also, if this relationship is required, you should use the property optional = false in the OneToOne annotation:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM