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.