Ebean:与OneToOne级联 - 尝试插入而不是更新

时间:2016-04-29 15:47:00

标签: scala playframework ebean

我正在使用Scala和Ebean,我遇到了一些严重的问题。情况很简单,我有一个父实体:

@Entity
case class Person(@(PrivateOwned@field)
                 @(OneToOne@field)(fetch = FetchType.LAZY, cascade = Array(CascadeType.ALL), orphanRemoval = true)
                 @(JoinColumn@field)(name = "website_setting_id")
                 websiteSetting: WebsiteSetting,
                 @(Id@field)id: Long = 0) extends Model

其中WebsiteSetting是:

@Entity
case class WebsiteSetting(@(Column@field)(unique = true) domain: String,
                          planId: Long,
                          @(Id@field) id: Long = 0) extends Model

我所看到的是,当我做类似的事情时:

val ws = WebsiteSetting("somedomain.com", 1)
val p = Person(ws)
p.save() // works as expected

但是以下失败了,

val updated = p.copy(websiteSetting = p.websiteSetting.copy(planId = 2))
updated.update()

使用:

    javax.persistence.PersistenceException: ERROR executing DML bindLog[] 
error[Duplicate entry '167' for key 'PRIMARY']

这显然意味着Ebean不知道子实体需要更新并尝试对其进行插入,而忽略id字段,等于167

避免这个问题的最佳方法是什么?

编辑: Ebean插件版本:https://www.playframework.com/documentation/2.3.6/api/java/play/db/ebean/package-summary.html (我使用了Play 2.3.6附带的ebean插件)

例外: 当Ebean尝试插入具有现有ID的实例时,会出现异常。 我从SQL日志中看到,Ebean尝试在服务器上执行的语句是insert id = 167,这是表中的现有行。这导致例外。

0 个答案:

没有答案