IntelliJ JAVA JPA

使用 JPA Support 套件簡化 JPA 架構建置

湯菀鈺 2024/08/29 19:53:11
118

 

使用 JPA Support 套件簡化 JPA 架構建置

前言

JPA Support 套件旨在簡化 JPA 的使用,提供自動化工具來生成與 JPA 相關的 Java 程式碼。

這些工具可以根據資料庫表結構自動生成對應的 Entity 和 Repository 類別,顯著降低手動編寫程式碼的工作量。

以下說明 JPA Support 套件生成 Java 程式碼的用法與注意事項。

安裝 JPA Support 套件

  • 在 IntelliJ IDEA 工具列選單中選擇:File / Settings / Plugins / JPA Support

  • 點選 Install 進行套件安裝

重啟IDEA

  • 安裝完後重新啟動 IntelliJ IDEA 使套件生效。
  • 在專案資料夾右鍵選單點選 New / Generate JPA Entities。

設定參數

  • 資料庫連線資訊 (以 MSSQL 為例)
    Driver class: com.microsoft.sqlserver.jdbc.SQLServerDriver
    Driver path: ~/.m2/repository/com/microsoft/sqlserver/mssql-jdbc/8.2.2.jre8/mssql-jdbc-8.2.2.jre8.jar
    URL: jdbc:sqlserver://10.20.30.199:1433;databaseName=PUSH;encrypt=true;trustServerCertificate=true;
    Host: 10.20.30.196
    Port: 1433
    Username: [Database username]
    Password: [Database Password]

  • 程式碼產生配置
    Entity package: com.demo.jpa.entity
    Entity package parent path: /Users/[username]/LocalDrive/Git/[ProjectA]/jpa/src/main/java
    Repository package: com.demo.jpa.repository
    Repository package parent path: /Users/[username]/LocalDrive/Git/[ProjectA]/jpa/src/main/java

生成 Java 程式碼

  • 選取需要生成程式碼的資料表後,點選 [Start Generate] 來產生與 JPA 相關的 Java 程式碼。

  • 執行後,可以在專案目錄下看到生成的 Entity 和 Repository Java 檔案。

複合 KEY IdClass

要解決的問題: does not define an IdClass

  • 有一點需要注意,當表的主鍵 (PK) 是複合鍵時,套件生成的 IdClass 寫法是不適用的,會出現如下錯誤:
  • 遇到複合鍵的表時,需要手動修改生成的 Entity 和 Repository 程式碼。
Caused by: java.lang.IllegalArgumentException: This class [class com.demo.jpa.entity.PushConf] does not define an IdClass

建置複合 KEY 物件

  • 當表使用複合鍵時,需要建置複合鍵物件,並且在 Entity 中使用 @EmbeddedId 來引用這個複合鍵物件。

1. 建立複合鍵類別

  • 首先,定義一個使用多個 @Id 欄位的 PK 類別
@Embeddable
public class DemoConfPK implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name = "USER_UQ")
    private String userUq;

    @Column(name = "SYS_UID")
    private String sysUid;

    public DemoConfPK() {
    }

    public DemoConfPK(String userUq, String sysUid) {
        this.userUq = userUq;
        this.sysUid = sysUid;
    }

    // 省略 Getter 和 Setter

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DemoConfPK that = (DemoConfPK) o;
        return Objects.equals(userUq, that.userUq) && Objects.equals(sysUid, that.sysUid);
    }

    @Override
    public int hashCode() {
        return Objects.hash(userUq, sysUid);
    }
}

2. 修改生成的 Entity 類別

  • 在生成的 Entity 類別中,使用 @EmbeddedId 注解來引用上述複合鍵類別
@Entity
@Table(name = "DEMO_CONF")
public class DemoConf implements Serializable {

    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private DemoConfPK id;

    public DemoConfPK getId() {
        return id;
    }

    public void setId(DemoConfPK id) {
        this.id = id;
    }

    // 省略其他 Getter 和 Setter
}

修改生成的 Repository 介面

  • 在生成的 Repository 介面中,使用複合鍵類別作為主鍵的類型
@Repository
public interface DemoConfRepository extends JpaRepository<DemoConf, DemoConfPK> {
    // 自訂查詢方法(若需要)
}

相關連結

 

湯菀鈺