Spring Data JPA使用@EntityListener攔截entity存取資料庫操作範例。
2021/6/23
2021/6/17
2021/6/8
2020/11/25
Spring Data JPA 未呼叫Repository.save()但資料卻更新
最近碰到Spring Data JPA的地雷,明明沒有呼叫CrudRepository.save()但資料卻更新。
Spring Data JPA org.springframework.orm.jpa.JpaSystemException: identifier of an instance of Entity was altered from x to y錯誤
Spring Data JPA的org.springframework.orm.jpa.JpaSystemException: identifier of an instance of Entity was altered from x to y 或
Hibernate的org.hibernate.HibernateException: identifier of an instance of Entity was altered from x to y錯誤原因如下。
2020/11/5
2020/11/4
Spring Data JPA @OneToMany LazyInitializationException could not initialize proxy - no Session
Spring Data JPA撈取一對多(One To Many)物件時,發生錯誤org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: com.abc.demo.entity.Department.employeeList, could not initialize proxy - no Session。
JPA JPQL簡介
JPQL(Java Persistence Query Language)是EJB-QL(Enterprise JavaBeans query language)的擴展,是類似SQL用來查詢JPA實體(entity)的物件導向持久化查詢語法。與SQL不同的是JPQL操作的對象為JPA的entity物件,而非資料庫的資料表,也就是對SQL及底層資料庫的抽象,所以JPQL是可攜的(portable),不會因為底層資料庫或資料表的異動而修改語法。
2020/10/24
Spring Data JPA AttributeConverter 轉換列舉屬性為資料表欄位值 convert Enum attribute to table column value
資料表中常有欄位用來儲存資料的類型(type)、種類(category)、狀態(state)、情況(status),這些欄位值通常對應到Java的enum。JPA 2.1可使用AttributeConverter轉換資料表欄位與entity的enum資料型態屬性。
2020/9/26
Spring Data JPA @IdClass複合主鍵範例 composite key example
Spring Data JPA使用@IdClass設定複合主鍵(composite primary key)範例如下。
2019/6/12
Hibernate JPA Entity屬性應該用原始型別(Primitive type)或包裹類別(Wrapper class)
當定義與資料表映射的Entity class(實體類別)時,若Entity屬性(成員變數)對映的資料表欄位是基本資料型態(例如整數(INTEGER),長整數(BIGINT),浮點數(DOUBLE)等),那應該用原始型別(Primitive type)還是包裹類別(Wrapper class)來定義Entity的屬性呢?
2019/5/21
Hibernate JPA 雙向關係 Bidirectional Relationships 的意思
在Hibernate JPA 的Entity實體關係中,可分為 Bidirectional(雙向)或 Unidirectional(單向)。Bidirectional的意思如下。
2019/5/19
Spring Boot 2 + Spring Data JPA + MySQL 8 簡單範例
本篇說明如何建立Spring Boot 2,Spring Data JPA對MySQL進行基本操作的專案。
2019/5/18
Hibernate JPA @OneToMany cascade設為CascadeType.DETACH的作用
簡單說就是當程式中對某實體物件進行detach時,其所關聯的實體物件也都會進行detach。
Hibernate JPA @OneToMany cascade設為CascadeType.REFRESH的作用
Hibernate JPA @OneToMany的cascade屬性值設定為CascadeType.REFRESH的作用如下。
2019/3/23
2018/11/30
SQL predicates的意思
SQL predicates是指表示TRUE或FALSE等條件判斷的敘述,例如=, <>, >, >=, <, <=, IN, BETWEEN, LIKE, IS NULL or IS NOT NULL等就稱之為predicates。或是說WHERE後的條件式都可稱為predicates。
2018/7/26
Hibernate JPA Entity Listeners
Hibernate JPA可以使用下面的實體監聽器Entity Listeners來偵測新增,查詢,刪除,修改(CRUD)執行時的動作,並在執行的前後插入其他要執行的方法。
Hibernate JPA Many To One複合主鍵新增時插入欄位順序錯誤
今天在使用Hibernate JPA的EntityManager.persist()新增主表和子表的資料時,出現插入欄位順序錯誤的問題,情形如下。