我們剛剛修復了 Hibernate 中的一個行為,在該行為中,它在本地機器上生成了與在 Staging 服務器上不同的查詢。你們中的任何人都可以向我解釋,為什么
@NotNull
@OneToOne(fetch = FetchType.EAGER)
@Type(type = "user_account")
open var user: T
在本地被翻譯成
從
user_password abstractpa0_
左外加入 user_account basicaccou1_ on abstractpa0_.user_id=basicaccou1_.id
在服務器上,在 Kubernetes 上運行 dockerized,它被翻譯成
從 user_password abstractpa0_
左外連接 d21_user_account useraccoun1_ on abstractpa0_.user_id = useraccoun1_.id
左外連接 user_account useraccoun1_1_ on useraccoun1_.id = useraccoun1_1_.id
我們使用單表繼承策略。基類是AbstractPasswordEntity
@Entity(name = "user_password")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "user_type")
abstract class AbstractPasswordEntity<T : BasicAccountEntity>(
...
@NotNull
@OneToOne(fetch = FetchType.EAGER)
@Type(type = "user_account")
open var user: T
) : BaseEntity()
由客戶實施
@Entity(name = "CustomerPassword")
@DiscriminatorValue("CustomerPasswordEntity")
class CustomerPasswordEntity(
id: Long? = null,
passwordHash: String,
user: CustomerAccountEntity
) : AbstractPasswordEntity<CustomerAccountEntity>(..., user)
和員工班。
@Entity(name = "StaffPassword")
@DiscriminatorValue("StaffPasswordEntity")
class StaffPasswordEntity(
id: Long? = null,
passwordHash: String,
user: StaffAccountEntity
) : AbstractPasswordEntity<StaffAccountEntity>(..., user)
經過一天的錯誤修復和大量 WTF 后,我們可以通過將關系注釋更改AbstractPasswordEntity為
@Entity(name = "user_password")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "user_type")
abstract class AbstractPasswordEntity<T : BasicAccountEntity>(
...
@NotNull
@OneToOne(fetch = FetchType.EAGER, targetEntity = BasicAccountEntity::class)
open var user: T
) : BaseEntity()
這現在以相同的方式在所有環境上編譯和執行。我唯一不知道的是剛剛發生了什么以及為什么會發生。
事實:與 pkgdiff 和 md5deep 相比,來自 staging 和 local 的 jar 顯示完全相同的結果 - 除了 GitLab 屬性。
uj5u.com熱心網友回復:
由于這個話題無法得到任何回復,這是我們最好的猜測:
這是泛型的問題。它們在運行時被洗掉,我們遇到了排序問題。它在本地運行是因為偶然地,休眠中的類的順序會導致預期的結果。在登臺時,訂單會混淆,hibernate 使用第一個適合泛型的類,然后導致執行不同的查詢。因此明確指定父BasicAccountEntity作為入口點確實解決了它的問題。
正如我所說,最好的猜測。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/326499.html
上一篇:當使用SpringJPAEntityManager本機查詢洗掉資料庫時,運行到SQLGrammarException
