Ebean: Cascade with OneToOne – tries inserts instead of update

眉间皱痕 提交于 2020-01-02 05:29:08

问题


I'm using Scala with Ebean, and I've run into some serious trouble. The situation is quite simple, I have a parent entity:

@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

where WebsiteSetting is:

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

What I'm seeing is that when I do something like:

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

But the following fails,

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

with:

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

Which clearly means that Ebean doesn't know that the child entity needs an update and tries to do an insert with it, disregarding the id field, which equals 167.

Whats the best way to avoid this problem?

Edit: Ebean plugin version: https://www.playframework.com/documentation/2.3.6/api/java/play/db/ebean/package-summary.html (I've used the ebean plugin that was shipped with Play 2.3.6)

Exception: The exception arises when Ebean tries to do an insert of an instance with an existing ID. I've seen from the SQL logs that the statement Ebean tries to execute on the server is an insert with id = 167, which is an existing row on the table. This results in the exception.

来源:https://stackoverflow.com/questions/36942609/ebean-cascade-with-onetoone-tries-inserts-instead-of-update

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!