Ebean: Cascade with OneToOne - trying to insert instead of updating

I am using Scala with Ebean and am having serious problems. The situation is pretty simple: I have a parent object:

@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 :

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

What I see 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() 

from:

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

This clearly means that Ebean does not know that the child needs to be updated and is trying to insert with it, not paying attention to the id field of 167 .

What is 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 used the ebean plugin that was sent to Play 2.3. 6)

Exception: An exception occurs when Ebean tries to insert an instance with an existing identifier. I saw from SQL logs that the Ebean statement is trying to execute on the insert server with id = 167, which is an existing row in the table. This leads to an exception.

+4
source share

Source: https://habr.com/ru/post/1214893/


All Articles