【发布时间】:2016-09-17 01:48:01
【问题描述】:
Hibernate 不想为子实体保存 ID。我有以下表格:
@Entity
@Table(name = "ct_orders")
data class Order(
@Id
@Column(name = "id")
@GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
val id: Int = 0,
@OneToMany(fetch = FetchType.LAZY, cascade = arrayOf(CascadeType.ALL), mappedBy = "order")
val route: List<Route>? = null,
...
)
@Entity
@Table(name = "ct_routes")
@JsonIgnoreProperties("id", "order")
data class Route(
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
val id: Int = 0,
@Column
val location: Point = GeoHelpers.point(),
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "order_id")
val order: Order? = null,
@Column
val title: String = ""
)
ct_routes 在 order_id 中保存为 null。人际关系有问题吗?或者,我的代码可能有问题?
这里是保存Order实体的部分代码:
val order = orderRepository.save(Order(
...
route = GeoHelpers.placesListToEntities(data.places),
...
))
fun placesListToEntities(points: List<PlaceDto>) = points.map {
Route(
location = Helpers.geometry(it.location.latitude, it.location.longitude),
title = it.title
)
}
【问题讨论】:
-
你是在保存之前设置Route对象的order属性吗?
标签: spring hibernate jpa kotlin