搜尋
首頁Javajava教程將 JPA 實體轉換為 Mendix

最近在探索 Mendix 時,我注意到他們有一個 Platform SDK,讓您可以透過 API 與 mendix 應用程式模型互動。

這給了我一個想法,探索它是否可以用於創建我們的領域模型。具體來說,是基於現有的傳統應用程式創建領域模型。

如果進一步推廣,這可用於將任何現有應用程式轉換為 Mendix 並從那裡繼續開發。

將 Java/Spring Web 應用程式轉換為 Mendix

因此,我創建了一個帶有簡單 API 和資料庫層的小型 Java/Spring Web 應用程式。為了簡單起見,它使用嵌入式 H2 資料庫。

在這篇文章中,我們將只轉換 JPA 實體。讓我們來看看它們:

@Entity
@Table(name = "CAT")
class Cat {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;
    private int age;
    private String color;

    @OneToOne
    private Human humanPuppet;

    ... constructor ...
    ... getters ...
}

@Entity
@Table(name = "HUMAN")
public class Human {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    ... constructor ...
    ... getters ...
}

如你所見,它們非常簡單:一隻有名字、年齡、顏色的貓和它的人類傀儡,因為正如我們所知,貓統治著世界。

它們都有一個自動產生的 ID 欄位。貓與人類有一對一的聯繫,這樣它就可以隨時稱呼它的人類。 (如果它不是 JPA 實體,我會放置一個 meow() 方法,但讓我們將其留到將來)。

應用程式功能齊全,但現在我們只對資料層感興趣。

提取 json 中的實體元數據

這可以用幾種不同的方式來完成:

  1. 透過靜態分析套件中的實體。
  2. 透過使用反射在運行時讀取這些實體。

我選擇了選項 2,因為它更快,而且我無法輕鬆找到可以執行選項 1 的庫。

接下來,我們需要決定建造後如何公開 json。為了簡單起見,我們只需將其寫入文件即可。一些替代方法可能是:

  • 透過 api 公開它。這更加複雜,因為您還需要確保端點受到良好的保護,因為我們不能公開暴露我們的元資料。
  • 透過一些管理工具公開它,例如 Spring Boot Actuator 或 jmx。它更安全,但仍然需要時間來設定。

現在讓我們來看看實際的程式碼:

public class MendixExporter {
    public static void exportEntitiesTo(String filePath) throws IOException {
        AnnotatedTypeScanner typeScanner = new AnnotatedTypeScanner(false, Entity.class);

        Set<class>> entityClasses = typeScanner.findTypes(JavaToMendixApplication.class.getPackageName());
        log.info("Entity classes are: {}", entityClasses);

        List<mendixentity> mendixEntities = new ArrayList();

        for (Class> entityClass : entityClasses) {
            List<mendixattribute> attributes = new ArrayList();
            for (Field field : entityClass.getDeclaredFields()) {

                AttributeType attributeType = determineAttributeType(field);
                AssociationType associationType = determineAssociationType(field, attributeType);
                String associationEntityType = determineAssociationEntityType(field, attributeType);

                attributes.add(
                        new MendixAttribute(field.getName(), attributeType, associationType, associationEntityType));
            }
            MendixEntity newEntity = new MendixEntity(entityClass.getSimpleName(), attributes);
            mendixEntities.add(newEntity);
        }

        writeToJsonFile(filePath, mendixEntities);
    }
    ...
}
</mendixattribute></mendixentity></class>

我們首先尋找應用程式中標有 JPA 的 @Entity 註解的所有類別。
然後,對於每堂課,我們:

  1. 使用entityClass.getDeclaredFields()取得聲明的欄位。
  2. 循環該類別的每個欄位。

對於每個字段,我們:

  1. 確定屬性的類型:

    private static final Map<class>, AttributeType> JAVA_TO_MENDIX_TYPE = Map.ofEntries(
            Map.entry(String.class, AttributeType.STRING),
            Map.entry(Integer.class, AttributeType.INTEGER),
            ...
            );
    // we return AttributeType.ENTITY if we cannot map to anything else
    </class>

    本質上,我們只是透過在 JAVA_TO_MENDIX_TYPE 映射中尋找 java 類型與我們的自訂枚舉值進行匹配。

  2. 接下來,我們檢查這個屬性是否實際上是一個關聯(指向另一個@Entity)。如果是這樣,我們確定關聯的類型:一對一、一對多、多對多:

    @Entity
    @Table(name = "CAT")
    class Cat {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
        private String name;
        private int age;
        private String color;
    
        @OneToOne
        private Human humanPuppet;
    
        ... constructor ...
        ... getters ...
    }
    
    @Entity
    @Table(name = "HUMAN")
    public class Human {
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        private Long id;
    
        private String name;
    
        ... constructor ...
        ... getters ...
    }
    

    為此,我們只需檢查先前映射的屬性類型。如果它是 Entity,這僅意味著在先前的步驟中我們無法將其對應到任何原始 java 類型、String 或 Enum。
    然後我們還需要決定它是什麼類型的關聯。檢查很簡單:如果是 List 類型,則它是一對多,否則是一對一(尚未實現「多對多」)。

  3. 然後我們為找到的每個欄位建立一個 MendixAttribute 物件。

完成後,我們只需為實體建立一個 MendixEntity 物件並指派屬性清單。
MendixEntity 和 MendixAttribute 是我們稍後將用來對應 json 的類別:

public class MendixExporter {
    public static void exportEntitiesTo(String filePath) throws IOException {
        AnnotatedTypeScanner typeScanner = new AnnotatedTypeScanner(false, Entity.class);

        Set<class>> entityClasses = typeScanner.findTypes(JavaToMendixApplication.class.getPackageName());
        log.info("Entity classes are: {}", entityClasses);

        List<mendixentity> mendixEntities = new ArrayList();

        for (Class> entityClass : entityClasses) {
            List<mendixattribute> attributes = new ArrayList();
            for (Field field : entityClass.getDeclaredFields()) {

                AttributeType attributeType = determineAttributeType(field);
                AssociationType associationType = determineAssociationType(field, attributeType);
                String associationEntityType = determineAssociationEntityType(field, attributeType);

                attributes.add(
                        new MendixAttribute(field.getName(), attributeType, associationType, associationEntityType));
            }
            MendixEntity newEntity = new MendixEntity(entityClass.getSimpleName(), attributes);
            mendixEntities.add(newEntity);
        }

        writeToJsonFile(filePath, mendixEntities);
    }
    ...
}
</mendixattribute></mendixentity></class>

最後,我們儲存一個List;使用 Jackson 轉換為 json 檔案。

將實體匯入 Mendix

有趣的部分來了,我們如何讀取上面產生的 json 檔案並從中建立 mendix 實體?

Mendix 的 Platform SDK 有一個 Typescript API 可以與之互動。
首先,我們將建立物件來表示我們的實體和屬性,以及關聯和屬性類型的列舉:

private static final Map<class>, AttributeType> JAVA_TO_MENDIX_TYPE = Map.ofEntries(
        Map.entry(String.class, AttributeType.STRING),
        Map.entry(Integer.class, AttributeType.INTEGER),
        ...
        );
// we return AttributeType.ENTITY if we cannot map to anything else
</class>

接下來,我們需要使用 appId 來取得我們的應用程序,建立臨時工作副本,打開模型,並找到我們感興趣的領域模型:

private static AssociationType determineAssociationType(Field field, AttributeType attributeType) {
    if (!attributeType.equals(AttributeType.ENTITY))
        return null;
    if (field.getType().equals(List.class)) {
        return AssociationType.ONE_TO_MANY;
    } else {
        return AssociationType.ONE_TO_ONE;
    }
}

SDK 實際上會從 git 中提取我們的 mendix 應用程式並進行處理。

讀取 json 檔案後,我們將循環實體:

public record MendixEntity(
        String name,
        List<mendixattribute> attributes) {
}

public record MendixAttribute(
        String name,
        AttributeType type,
        AssociationType associationType,
        String entityType) {

    public enum AttributeType {
        STRING,
        INTEGER,
        DECIMAL,
        AUTO_NUMBER,
        BOOLEAN,
        ENUM,
        ENTITY;
    }

    public enum AssociationType {
        ONE_TO_ONE,
        ONE_TO_MANY
    }
}
</mendixattribute>

這裡我們使用domainmodels.Entity.createIn(domainModel);在我們的域模型中建立一個新實體並為其分配一個名稱。我們可以指派更多屬性,例如文件、索引,甚至實體在領域模型中呈現的位置。

我們在單獨的函數中處理屬性:

interface ImportedEntity {
    name: string;
    generalization: string;
    attributes: ImportedAttribute[];
}

interface ImportedAttribute {
    name: string;
    type: ImportedAttributeType;
    entityType: string;
    associationType: ImportedAssociationType;
}

enum ImportedAssociationType {
    ONE_TO_ONE = "ONE_TO_ONE",
    ONE_TO_MANY = "ONE_TO_MANY"
}

enum ImportedAttributeType {
    INTEGER = "INTEGER",
    STRING = "STRING",
    DECIMAL = "DECIMAL",
    AUTO_NUMBER = "AUTO_NUMBER",
    BOOLEAN = "BOOLEAN",
    ENUM = "ENUM",
    ENTITY = "ENTITY"
}

這裡我們唯一需要付出一些努力的就是將屬性類型對應到有效的 mendix 類型。

接下來我們處理關聯。首先,由於在我們的Java實體中關聯是透過欄位宣告的,因此我們需要區分哪些欄位是簡單屬性,哪些欄位是關聯。為此,我們只需要檢查它是實體類型還是原始類型:

const client = new MendixPlatformClient();
const app = await client.getApp(appId);
const workingCopy = await app.createTemporaryWorkingCopy("main");
const model = await workingCopy.openModel();
const domainModelInterface = model.allDomainModels().filter(dm => dm.containerAsModule.name === MyFirstModule")[0];
const domainModel = await domainModelInterface.load();

讓我們建立關聯:

function createMendixEntities(domainModel: domainmodels.DomainModel, entitiesInJson: any) {
    const importedEntities: ImportedEntity[] = JSON.parse(entitiesInJson);

    importedEntities.forEach((importedEntity, i) => {
        const mendixEntity = domainmodels.Entity.createIn(domainModel);
        mendixEntity.name = importedEntity.name;

        processAttributes(importedEntity, mendixEntity);
    });

    importedEntities.forEach(importedEntity => {
        const mendixParentEntity = domainModel.entities.find(e => e.name === importedEntity.name) as domainmodels.Entity;
        processAssociations(importedEntity, domainModel, mendixParentEntity);
    });
}

除了名稱之外,我們還有 4 個重要的屬性需要設定:

  1. 父實體。這是目前實體。
  2. 子實體。在最後一步中,我們為每個 java 實體建立了 mendix 實體。現在我們只需要根據實體中java欄位的類型找到符合的實體:

    function processAttributes(importedEntity: ImportedEntity, mendixEntity: domainmodels.Entity) {
        importedEntity.attributes.filter(a => a.type !== ImportedAttributeType.ENTITY).forEach(a => {
            const mendixAttribute = domainmodels.Attribute.createIn(mendixEntity);
            mendixAttribute.name = capitalize(getAttributeName(a.name, importedEntity));
            mendixAttribute.type = assignAttributeType(a.type, mendixAttribute);
        });
    }
    
  3. 關聯型別。如果是一對一的,它會對應到一個引用。如果是一對多,則對應到參考集。我們現在將跳過多對多。

  4. 協會所有者。一對一和多對多重關聯都具有相同的所有者類型:兩者。對於一對一,所有者類型必須為預設。

Mendix Platform SDK 將在我們的 mendix 應用程式的本機工作副本中建立實體。現在我們只需要告訴它提交更改:

@Entity
@Table(name = "CAT")
class Cat {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;
    private int age;
    private String color;

    @OneToOne
    private Human humanPuppet;

    ... constructor ...
    ... getters ...
}

@Entity
@Table(name = "HUMAN")
public class Human {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String name;

    ... constructor ...
    ... getters ...
}

幾秒鐘後,您可以在 Mendix Studio Pro 中開啟應用程式並驗證結果:
Converting JPA entities to Mendix

現在你已經看到了:貓和人的實體,它們之間存在一對一的關聯。

如果您想親自嘗試或查看完整程式碼,請造訪此儲存庫。

對未來的想法

  1. 在這個範例中,我使用了 Java/Spring 應用程式進行轉換,因為我最精通它,但任何應用程式都可以使用。 只需能夠讀取類型資料(靜態或運行時)來提取類別和欄位名稱就足夠了。
  2. 我很好奇嘗試讀取一些 Java 邏輯並將其匯出到 Mendix 微流程。我們可能無法真正轉換業務邏輯本身,但我們應該能夠獲得它的結構(至少是業務方法簽名?)。
  3. 本文中的程式碼可以推廣並製作成一個函式庫:json 格式可以保持不變,並且可以有一個函式庫用於匯出 java 類型,另一個函式庫用於匯入 mendix 實體。
  4. 我們可以使用相同的方法進行相反的操作:將 mendix 轉換為另一種語言。

結論

Mendix Platform SDK 是一項強大的功能,允許以程式設計方式與 mendix 應用程式互動。他們列出了一些範例用例,例如導入/導出程式碼、分析應用程式複雜性。
如果您有興趣,請看一下。
對於本文,您可以在此處找到完整程式碼。

以上是將 JPA 實體轉換為 Mendix的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
平台獨立性如何使企業級的Java應用程序受益?平台獨立性如何使企業級的Java應用程序受益?May 03, 2025 am 12:23 AM

Java在企業級應用中被廣泛使用是因為其平台獨立性。 1)平台獨立性通過Java虛擬機(JVM)實現,使代碼可在任何支持Java的平台上運行。 2)它簡化了跨平台部署和開發流程,提供了更大的靈活性和擴展性。 3)然而,需注意性能差異和第三方庫兼容性,並採用最佳實踐如使用純Java代碼和跨平台測試。

考慮到平台獨立性,Java在物聯網(物聯網)設備的開發中扮演什麼角色?考慮到平台獨立性,Java在物聯網(物聯網)設備的開發中扮演什麼角色?May 03, 2025 am 12:22 AM

JavaplaysigantroleiniotduetoitsplatFormentence.1)itallowscodeTobewrittenOnCeandrunonVariousDevices.2)Java'secosystemprovidesuseusefidesusefidesulylibrariesforiot.3)

描述一個方案,您在Java中遇到了一個特定於平台的問題以及如何解決。描述一個方案,您在Java中遇到了一個特定於平台的問題以及如何解決。May 03, 2025 am 12:21 AM

ThesolutiontohandlefilepathsacrossWindowsandLinuxinJavaistousePaths.get()fromthejava.nio.filepackage.1)UsePaths.get()withSystem.getProperty("user.dir")andtherelativepathtoconstructthefilepath.2)ConverttheresultingPathobjecttoaFileobjectifne

Java平台獨立對開發人員有什麼好處?Java平台獨立對開發人員有什麼好處?May 03, 2025 am 12:15 AM

Java'splatFormIndenceistificantBecapeitAllowSitallowsDevelostWriTecoDeonCeandRunitonAnyPlatFormwithAjvm.this“ writeonce,runanywhere”(era)櫥櫃櫥櫃:1)交叉plat formcomplibility cross-platformcombiblesible,enablingDeploymentMentMentMentMentAcrAptAprospOspOspOssCrossDifferentoSswithOssuse; 2)

將Java用於需要在不同服務器上運行的Web應用程序的優點是什麼?將Java用於需要在不同服務器上運行的Web應用程序的優點是什麼?May 03, 2025 am 12:13 AM

Java適合開發跨服務器web應用。 1)Java的“一次編寫,到處運行”哲學使其代碼可在任何支持JVM的平台上運行。 2)Java擁有豐富的生態系統,包括Spring和Hibernate等工具,簡化開發過程。 3)Java在性能和安全性方面表現出色,提供高效的內存管理和強大的安全保障。

JVM如何促進Java的'寫作一次,在任何地方運行”(WORA)功能?JVM如何促進Java的'寫作一次,在任何地方運行”(WORA)功能?May 02, 2025 am 12:25 AM

JVM通過字節碼解釋、平台無關的API和動態類加載實現Java的WORA特性:1.字節碼被解釋為機器碼,確保跨平台運行;2.標準API抽像操作系統差異;3.類在運行時動態加載,保證一致性。

Java的較新版本如何解決平台特定問題?Java的較新版本如何解決平台特定問題?May 02, 2025 am 12:18 AM

Java的最新版本通過JVM優化、標準庫改進和第三方庫支持有效解決平台特定問題。 1)JVM優化,如Java11的ZGC提升了垃圾回收性能。 2)標準庫改進,如Java9的模塊系統減少平台相關問題。 3)第三方庫提供平台優化版本,如OpenCV。

說明JVM執行的字節碼驗證的過程。說明JVM執行的字節碼驗證的過程。May 02, 2025 am 12:18 AM

JVM的字節碼驗證過程包括四個關鍵步驟:1)檢查類文件格式是否符合規範,2)驗證字節碼指令的有效性和正確性,3)進行數據流分析確保類型安全,4)平衡驗證的徹底性與性能。通過這些步驟,JVM確保只有安全、正確的字節碼被執行,從而保護程序的完整性和安全性。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具