国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

首頁 Java java教程 將 JPA 實(shí)體轉(zhuǎn)換為 Mendix

將 JPA 實(shí)體轉(zhuǎn)換為 Mendix

Jan 13, 2025 pm 06:04 PM

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

這給了我一個想法,探索它是否可以用於創(chuàng)建我們的領(lǐng)域模型。具體來說,是基於現(xiàn)有的傳統(tǒng)應(yīng)用程式創(chuàng)建領(lǐng)域模型。

如果進(jìn)一步推廣,這可用於將任何現(xiàn)有應(yīng)用程式轉(zhuǎn)換為 Mendix 並從那裡繼續(xù)開發(fā)。

將 Java/Spring Web 應(yīng)用程式轉(zhuǎn)換為 Mendix

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

在這篇文章中,我們將只轉(zhuǎn)換 JPA 實(shí)體。讓我們來看看它們:

@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 ...
}

如你所見,它們非常簡單:一隻有名字、年齡、顏色的貓和它的人類傀儡,因?yàn)檎缥覀兯埥y(tǒng)治著世界。

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

應(yīng)用程式功能齊全,但現(xiàn)在我們只對資料層感興趣。

提取 json 中的實(shí)體元數(shù)據(jù)

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

  1. 透過靜態(tài)分析套件中的實(shí)體。
  2. 透過使用反射在運(yùn)行時讀取這些實(shí)體。

我選擇了選項(xiàng) 2,因?yàn)樗?,而且我無法輕鬆找到可以執(zhí)行選項(xiàng) 1 的庫。

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

  • 透過 api 公開它。這更加複雜,因?yàn)槟€需要確保端點(diǎn)受到良好的保護(hù),因?yàn)槲覀儾荒芄_暴露我們的元資料。
  • 透過一些管理工具公開它,例如 Spring Boot Actuator 或 jmx。它更安全,但仍然需要時間來設(shè)定。

現(xiàn)在讓我們來看看實(shí)際的程式碼:

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);
    }
    ...
}

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

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

對於每個字段,我們:

  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
    

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

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

    @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,這僅意味著在先前的步驟中我們無法將其對應(yīng)到任何原始 java 類型、String 或 Enum。
    然後我們還需要決定它是什麼類型的關(guān)聯(lián)。檢查很簡單:如果是 List 類型,則它是一對多,否則是一對一(尚未實(shí)現(xiàn)「多對多」)。

  3. 然後我們?yōu)檎业降拿總€欄位建立一個 MendixAttribute 物件。

完成後,我們只需為實(shí)體建立一個 MendixEntity 物件並指派屬性清單。
MendixEntity 和 MendixAttribute 是我們稍後將用來對應(yīng) 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);
    }
    ...
}

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

將實(shí)體匯入 Mendix

有趣的部分來了,我們?nèi)绾巫x取上面產(chǎn)生的 json 檔案並從中建立 mendix 實(shí)體?

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

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

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

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 實(shí)際上會從 git 中提取我們的 mendix 應(yīng)用程式並進(jìn)行處理。

讀取 json 檔案後,我們將循環(huán)實(shí)體:

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
    }
}

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

我們在單獨(dú)的函數(shù)中處理屬性:

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"
}

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

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

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();

讓我們建立關(guān)聯(lián):

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 個重要的屬性需要設(shè)定:

  1. 父實(shí)體。這是目前實(shí)體。
  2. 子實(shí)體。在最後一步中,我們?yōu)槊總€ java 實(shí)體建立了 mendix 實(shí)體?,F(xiàn)在我們只需要根據(jù)實(shí)體中java欄位的類型找到符合的實(shí)體:

    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. 關(guān)聯(lián)型別。如果是一對一的,它會對應(yīng)到一個引用。如果是一對多,則對應(yīng)到參考集。我們現(xiàn)在將跳過多對多。

  4. 協(xié)會所有者。一對一和多對多重關(guān)聯(lián)都具有相同的所有者類型:兩者。對於一對一,所有者類型必須為預(yù)設(shè)。

Mendix Platform SDK 將在我們的 mendix 應(yīng)用程式的本機(jī)工作副本中建立實(shí)體?,F(xiàn)在我們只需要告訴它提交更改:

@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 中開啟應(yīng)用程式並驗(yàn)證結(jié)果:
Converting JPA entities to Mendix

現(xiàn)在你已經(jīng)看到了:貓和人的實(shí)體,它們之間存在一對一的關(guān)聯(lián)。

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

對未來的想法

  1. 在這個範(fàn)例中,我使用了 Java/Spring 應(yīng)用程式進(jìn)行轉(zhuǎn)換,因?yàn)槲易罹ㄋ魏螒?yīng)用程式都可以使用。 只需能夠讀取類型資料(靜態(tài)或運(yùn)行時)來提取類別和欄位名稱就足夠了。
  2. 我很好奇嘗試讀取一些 Java 邏輯並將其匯出到 Mendix 微流程。我們可能無法真正轉(zhuǎn)換業(yè)務(wù)邏輯本身,但我們應(yīng)該能夠獲得它的結(jié)構(gòu)(至少是業(yè)務(wù)方法簽名?)。
  3. 本文中的程式碼可以推廣並製作成一個函式庫:json 格式可以保持不變,並且可以有一個函式庫用於匯出 java 類型,另一個函式庫用於匯入 mendix 實(shí)體。
  4. 我們可以使用相同的方法進(jìn)行相反的操作:將 mendix 轉(zhuǎn)換為另一種語言。

結(jié)論

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

以上是將 JPA 實(shí)體轉(zhuǎn)換為 Mendix的詳細(xì)內(nèi)容。更多資訊請關(guān)注PHP中文網(wǎng)其他相關(guān)文章!

本網(wǎng)站聲明
本文內(nèi)容由網(wǎng)友自願投稿,版權(quán)歸原作者所有。本站不承擔(dān)相應(yīng)的法律責(zé)任。如發(fā)現(xiàn)涉嫌抄襲或侵權(quán)的內(nèi)容,請聯(lián)絡(luò)admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費(fèi)脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅(qū)動的應(yīng)用程序,用於創(chuàng)建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

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

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

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

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費(fèi)的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強(qiáng)大的PHP整合開發(fā)環(huán)境

Dreamweaver CS6

Dreamweaver CS6

視覺化網(wǎng)頁開發(fā)工具

SublimeText3 Mac版

SublimeText3 Mac版

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

hashmap和hashtable之間的區(qū)別? hashmap和hashtable之間的區(qū)別? Jun 24, 2025 pm 09:41 PM

HashMap與Hashtable的區(qū)別主要體現(xiàn)在線程安全、null值支持及性能方面。 1.線程安全方面,Hashtable是線程安全的,其方法大多為同步方法,而HashMap不做同步處理,非線程安全;2.null值支持上,HashMap允許一個null鍵和多個null值,Hashtable則不允許null鍵或值,否則拋出NullPointerException;3.性能方面,HashMap因無同步機(jī)制效率更高,Hashtable因每次操作加鎖性能較低,推薦使用ConcurrentHashMap替

為什麼我們需要包裝紙課? 為什麼我們需要包裝紙課? Jun 28, 2025 am 01:01 AM

Java使用包裝類是因?yàn)榛緮?shù)據(jù)類型無法直接參與面向?qū)ο癫僮?,而?shí)際需求中常需對象形式;1.集合類只能存儲對象,如List利用自動裝箱存儲數(shù)值;2.泛型不支持基本類型,必須使用包裝類作為類型參數(shù);3.包裝類可表示null值,用於區(qū)分未設(shè)置或缺失的數(shù)據(jù);4.包裝類提供字符串轉(zhuǎn)換等實(shí)用方法,便於數(shù)據(jù)解析與處理,因此在需要這些特性的場景下,包裝類不可或缺。

什麼是接口中的靜態(tài)方法? 什麼是接口中的靜態(tài)方法? Jun 24, 2025 pm 10:57 PM

StaticmethodsininterfaceswereintroducedinJava8toallowutilityfunctionswithintheinterfaceitself.BeforeJava8,suchfunctionsrequiredseparatehelperclasses,leadingtodisorganizedcode.Now,staticmethodsprovidethreekeybenefits:1)theyenableutilitymethodsdirectly

JIT編譯器如何優(yōu)化代碼? JIT編譯器如何優(yōu)化代碼? Jun 24, 2025 pm 10:45 PM

JIT編譯器通過方法內(nèi)聯(lián)、熱點(diǎn)檢測與編譯、類型推測與去虛擬化、冗餘操作消除四種方式優(yōu)化代碼。 1.方法內(nèi)聯(lián)減少調(diào)用開銷,將頻繁調(diào)用的小方法直接插入調(diào)用處;2.熱點(diǎn)檢測識別高頻執(zhí)行代碼並集中優(yōu)化,節(jié)省資源;3.類型推測收集運(yùn)行時類型信息實(shí)現(xiàn)去虛擬化調(diào)用,提升效率;4.冗餘操作消除根據(jù)運(yùn)行數(shù)據(jù)刪除無用計算和檢查,增強(qiáng)性能。

什麼是實(shí)例初始器塊? 什麼是實(shí)例初始器塊? Jun 25, 2025 pm 12:21 PM

實(shí)例初始化塊在Java中用於在創(chuàng)建對象時運(yùn)行初始化邏輯,其執(zhí)行先於構(gòu)造函數(shù)。它適用於多個構(gòu)造函數(shù)共享初始化代碼、複雜字段初始化或匿名類初始化場景,與靜態(tài)初始化塊不同的是它每次實(shí)例化時都會執(zhí)行,而靜態(tài)初始化塊僅在類加載時運(yùn)行一次。

什麼是工廠模式? 什麼是工廠模式? Jun 24, 2025 pm 11:29 PM

工廠模式用於封裝對象創(chuàng)建邏輯,使代碼更靈活、易維護(hù)、松耦合。其核心答案是:通過集中管理對象創(chuàng)建邏輯,隱藏實(shí)現(xiàn)細(xì)節(jié),支持多種相關(guān)對象的創(chuàng)建。具體描述如下:工廠模式將對象創(chuàng)建交給專門的工廠類或方法處理,避免直接使用newClass();適用於多類型相關(guān)對象創(chuàng)建、創(chuàng)建邏輯可能變化、需隱藏實(shí)現(xiàn)細(xì)節(jié)的場景;例如支付處理器中通過工廠統(tǒng)一創(chuàng)建Stripe、PayPal等實(shí)例;其實(shí)現(xiàn)包括工廠類根據(jù)輸入?yún)?shù)決定返回的對象,所有對象實(shí)現(xiàn)共同接口;常見變體有簡單工廠、工廠方法和抽象工廠,分別適用於不同複雜度的需求。

變量的最終關(guān)鍵字是什麼? 變量的最終關(guān)鍵字是什麼? Jun 24, 2025 pm 07:29 PM

InJava,thefinalkeywordpreventsavariable’svaluefrombeingchangedafterassignment,butitsbehaviordiffersforprimitivesandobjectreferences.Forprimitivevariables,finalmakesthevalueconstant,asinfinalintMAX_SPEED=100;wherereassignmentcausesanerror.Forobjectref

什麼是類型鑄造? 什麼是類型鑄造? Jun 24, 2025 pm 11:09 PM

類型轉(zhuǎn)換有兩種:隱式和顯式。 1.隱式轉(zhuǎn)換自動發(fā)生,如將int轉(zhuǎn)為double;2.顯式轉(zhuǎn)換需手動操作,如使用(int)myDouble。需要類型轉(zhuǎn)換的情況包括處理用戶輸入、數(shù)學(xué)運(yùn)算或函數(shù)間傳遞不同類型的值時。需要注意的問題有:浮點(diǎn)數(shù)轉(zhuǎn)整數(shù)會截斷小數(shù)部分、大類型轉(zhuǎn)小類型可能導(dǎo)致數(shù)據(jù)丟失、某些語言不允許直接轉(zhuǎn)換特定類型。正確理解語言的轉(zhuǎn)換規(guī)則有助於避免錯誤。

See all articles