[英]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.我正在尝试使用 kotlin、springboot 和 hibernate 编写简单的世界生成器,并且我在实体中有很多关系,但其中一个不起作用。 Program generate the Countries and cities, but in DB I have a null ID for 'capital'
程序生成国家和城市,但在数据库中,我的“资本”ID 为空
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()
}
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:编辑2:
Method Util.getObj() returns correct objects:方法 Util.getObj() 返回正确的对象:
Confirm that the method Util.getObj
is really saving the new city and returning a city with an id.确认
Util.getObj
方法确实在保存新城市并返回一个带有 id 的城市。
Also, if this relationship is required, you should use the property optional = false
in the OneToOne
annotation:此外,如果需要这种关系,您应该在
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.