独立使用OFBiz实体引擎

实体引擎(Entity Engine)在OFBiz中是不可或缺的核心组件。本文并不是一篇实体引擎的使用帮助,只是想说明如何独立地去使用实体引擎,可以方便自己阅读源码。笔者将建立一个gradle项目,用来管理依赖。

 

首先,需要下载OFBiz,可以下载发布包,也可以下载SVN源码。发布包下载地址:http://ofbiz.apache.org/download.html,SVN源码下载:

svn co http://svn.apache.org/repos/asf/ofbiz/trunk ofbiz
 
然后,我们来建gradle项目,创建一个settings.gradle和build.gradle文件:

 

rootProject.name = 'ofbiz-test-entityengine'
 
apply plugin: 'java'
apply plugin: 'maven'

project.group = 'com.zf'
project.version = '1.0.0'
project.archivesBaseName = 'ofbiz-test-entityengine'

sourceCompatibility = '1.6'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

if (!hasProperty('mainClass')) {
    ext.mainClass = 'com.zf.ofbiz.entity.Main'
}

repositories {
    mavenCentral()
    mavenLocal()
}

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    
    compile (
        'commons-codec:commons-codec:1.6'
        ,'commons-dbcp:commons-dbcp:1.4'
        ,'commons-lang:commons-lang:2.6'
        ,'commons-logging:commons-logging:1.1.2'
        ,'commons-pool:commons-pool:1.3'
        ,'mysql:mysql-connector-java:5.1.20'
        ,'log4j:log4j:1.2.17'
        ,'javolution:javolution:5.4.3'
    )
    
    testCompile (
        'junit:junit:4.10'
    )
}

task sourcesJar(type: Jar, dependsOn: classes, description: 'Creates a jar from the source files.') {
    classifier = 'sources'
    from sourceSets.main.allSource
}

artifacts {
    archives jar
    archives sourcesJar
}
 
可以看到依赖的包有由compile指定的几个,这是在maven仓库里有的;还有一部分是compile fileTree指定的项目libs目录下的所有jar包,主要有:
  1. antisamy-bin.1.2.jar
  2. geronimo-transaction-2.1.1.jar
  3. ofbiz-base.jar
  4. servlet-api-3.0.jar
  5. clhm-release-1.0-lru.jar
  6. icu4j-49_1.jar
  7. ofbiz-entity.jar
  8. xercesImpl-2.9.1.jar
  9. geronimo-j2ee-connector_1.5_spec-1.0.jar
  10. jakarta-oro-2.0.8.jar
  11. ofbiz-geronimo.jar
  12. xstream-1.3.1.jar
  13. geronimo-jta_1.1_spec-1.1.1.jar
  14. jdbm-1.0-SNAPSHOT.jar
  15. owasp-esapi-full-java-1.4.jar
这此依赖包都是必须的。在OFBIZ_HOME/framework/下的各目录里去搜索都能找到。ofbiz-*.jar是编译OFBiz生成出来的,所以需要先执行一下OFBIZ_HOME/ant build。
 
最后,我们来测试一下OFBiz的实体引擎吧。案例将会设计三个实体(对应两张表):User、Party、PartyAndUser。这三个实体的定义在entitymodel.xml文件中。
<entitymodel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/entitymodel.xsd">
    
    <title>Entity of an Open For Business Project Component</title>
    <description>None</description>
    <version>1.0</version>

    <entity package-name="org.zf.entity" entity-name="Party" title="Party Entity" no-auto-stamp="true">
        <field name="partyId" type="id-int"></field>
        <field name="partyName" type="name"></field>
        <prim-key field="partyId"/>
    </entity>

    <entity package-name="org.zf.entity" entity-name="User" title="User Entity" no-auto-stamp="true">
        <field name="userId" type="id-int"></field>
        <field name="partyId" type="id-int"></field>
        <field name="loginName" type="name"></field>
        <prim-key field="userId"/>
    </entity>
    
    <view-entity package-name="org.zf.entity.view" entity-name="PartyAndUser" title="Party and User Entity">
        <member-entity entity-alias="PARTY" entity-name="Party"/>
        <member-entity entity-alias="USER" entity-name="User"/>
        <alias-all entity-alias="PARTY"/>
        <alias-all entity-alias="USER"/>
        <view-link entity-alias="PARTY" rel-entity-alias="USER">
            <key-map field-name="partyId" rel-field-name="partyId"/>
        </view-link>
    </view-entity>
    
</entitymodel>
 
除了我们定义的实体外,我们还要配置一下实体引擎,主要配置的是数据库连接、实体配置目录以及数据字段类型。通过文件entityengine.xml定义。
<?xml version="1.0" encoding="UTF-8" ?>
<entity-config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/entity-config.xsd">
    
    <resource-loader name="fieldTypeLoader" class="org.ofbiz.base.config.ClasspathLoader" prefix="fieldtype/"/>
    
    <resource-loader name="entityLoader" class="org.ofbiz.base.config.ClasspathLoader" prefix="entity/"/>

    <transaction-factory class="org.ofbiz.geronimo.GeronimoTransactionFactory"/>

    <connection-factory class="org.ofbiz.entity.connection.DBCPConnectionFactory"/>

    <debug-xa-resources value="false" />
    
    <delegator name="default" entity-model-reader="main" entity-group-reader="main" entity-eca-reader="main">
        <group-map group-name="org.ofbiz" datasource-name="localmysql"/>
        <group-map group-name="org.ofbiz.olap" datasource-name="localmysqlolap"/>
        <group-map group-name="org.ofbiz.tenant" datasource-name="localmysqltenant"/>
    </delegator>
    
    <!-- 实体配置 -->
    <entity-model-reader name="main">
        <resource loader="entityLoader" location="entitymodel.xml" />
    </entity-model-reader>

    <entity-group-reader name="main"/>

    <entity-eca-reader name="main"/>

    <entity-data-reader name="tenant"/>
    <entity-data-reader name="seed"/>
    <entity-data-reader name="seed-initial"/>
    <entity-data-reader name="demo"/>
    <entity-data-reader name="ext"/>
    <entity-data-reader name="ext-test"/>
    <entity-data-reader name="ext-demo"/>

    <!-- 数据类型配置,可以拷贝OFBIZ_HOME/framework/entity/fieldtype/fieldtypemysql.xml文件到项目 -->
    <field-type name="mysql" loader="fieldTypeLoader" location="fieldtypemysql.xml"/>
    
    <!-- 数据源配置 -->
    <datasource name="localmysql"
                helper-class="org.ofbiz.entity.datasource.GenericHelperDAO"
                field-type-name="mysql"
                check-on-start="true"
                add-missing-on-start="true"
                check-pks-on-start="false"
                use-foreign-keys="true"
                join-style="ansi-no-parenthesis"
                alias-view-columns="false"
                drop-fk-use-foreign-key-keyword="true"
                table-type="InnoDB"
                character-set="utf8"
                collate="utf8_general_cs">
        <read-data reader-name="tenant"/>
        <read-data reader-name="seed"/>
        <read-data reader-name="seed-initial"/>
        <read-data reader-name="demo"/>
        <read-data reader-name="ext"/>
        <read-data reader-name="ext-test"/>
        <read-data reader-name="ext-demo"/>
        <inline-jdbc
            jdbc-driver="com.mysql.jdbc.Driver"
            jdbc-uri="jdbc:mysql://localhost/ofbiz?characterEncoding=UTF8"
            jdbc-username="ofbiz"
            jdbc-password="ofbiz"
            isolation-level="ReadCommitted"
            pool-minsize="2"
            pool-maxsize="250"
            time-between-eviction-runs-millis="600000"/>
    </datasource>
    <datasource name="localmysqlolap"
                helper-class="org.ofbiz.entity.datasource.GenericHelperDAO"
                field-type-name="mysql"
                check-on-start="true"
                add-missing-on-start="true"
                check-pks-on-start="false"
                use-foreign-keys="true"
                join-style="ansi-no-parenthesis"
                alias-view-columns="false"
                drop-fk-use-foreign-key-keyword="true"
                table-type="InnoDB"
                character-set="utf8"
                collate="utf8_general_cs">
        <read-data reader-name="tenant"/>
        <read-data reader-name="seed"/>
        <read-data reader-name="seed-initial"/>
        <read-data reader-name="demo"/>
        <read-data reader-name="ext"/>
        <read-data reader-name="ext-test"/>
        <read-data reader-name="ext-demo"/>
        <inline-jdbc
            jdbc-driver="com.mysql.jdbc.Driver"
            jdbc-uri="jdbc:mysql://127.0.0.1/ofbizolap?autoReconnect=true"
            jdbc-username="ofbiz"
            jdbc-password="ofbiz"
            isolation-level="ReadCommitted"
            pool-minsize="2"
            pool-maxsize="250"
            time-between-eviction-runs-millis="600000"/>
    </datasource>
    <datasource name="localmysqltenant"
                helper-class="org.ofbiz.entity.datasource.GenericHelperDAO"
                field-type-name="mysql"
                check-on-start="true"
                add-missing-on-start="true"
                check-pks-on-start="false"
                use-foreign-keys="true"
                join-style="ansi-no-parenthesis"
                alias-view-columns="false"
                drop-fk-use-foreign-key-keyword="true"
                table-type="InnoDB"
                character-set="utf8"
                collate="utf8_general_cs">
        <read-data reader-name="tenant"/>
        <read-data reader-name="seed"/>
        <read-data reader-name="seed-initial"/>
        <read-data reader-name="demo"/>
        <read-data reader-name="ext"/>
        <read-data reader-name="ext-test"/>
        <read-data reader-name="ext-demo"/>
        <inline-jdbc
            jdbc-driver="com.mysql.jdbc.Driver"
            jdbc-uri="jdbc:mysql://127.0.0.1/ofbiztenant?autoReconnect=true"
            jdbc-username="ofbiz"
            jdbc-password="ofbiz"
            isolation-level="ReadCommitted"
            pool-minsize="2"
            pool-maxsize="250"
            time-between-eviction-runs-millis="600000"/>
    </datasource>
</entity-config>
 
这样主要的配置文件都已经有了。对了,还要把OFBIZ_HOME/framework/base/config/cache.properties文件拷贝到项目,也可以再OFBIZ_HOME/framework/base/config/log4j.xml文件到项目目录。下面就是我们的测试代码了:
package com.zf.ofbiz.entity;

import java.util.List;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.DelegatorFactory;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;

public class Main {    
    public static void main(String[] args) throws Exception {        
        Delegator delegator = DelegatorFactory.getDelegator("default");
       
        List<GenericValue> valueList = delegator.findList("PartyAndUser", EntityCondition.makeCondition("partyName", "TEST"), null, null, null, true);
        System.out.println(valueList.size());
    }
}
 
这样我们就可以独立使用实体引擎了,可以更方便地去测试、学习OFBiz的实体引擎。
 
深入解析Ofbiz实体引擎核心机制与实战应用 Apache OFBiz(Open For Business) 的实体引擎(Entity Engine)是其数据访问层的核心组件,承担着数据库抽象、持久化操作与对象关系映射的职责。它通过元数据驱动的方式,将XML中定义的实体模型动态映射为Java对象与数据库表结构,屏蔽了底层数据库差异,实现了跨平台的数据操作统一性。该引擎的设计目标在于解耦业务逻辑与数据存储细节,支持灵活的数据建模与动态扩展,尤其适用于复杂企业级应用中频繁变更的数据需求。 阅读详情

相关推荐

XSS防御-使用AntiSamy配置文件

XSS防御-使用AntiSamy配置文件。 AntiSamy是OWASP的一个开源项目,通过对用户输入的 HTML / CSS / JavaScript 等内容进行检验和清理,确保输入符合应用规范。AntiSamy被广泛应用于Web服务对存储型和反射型XSS的防御中

Apache OFBiz 学习笔记 之 实体引擎

1、概述     entity engine和常见的ORM有一点很大的不同,他的mapping object只有一个 GenericEntity,称它的entity engine 为adaptive object model更为合适一些,是一种比较灵活,代码量非常少的独特的持久化方案。使用entity engine做的项目和其他的ORM相比有一个很明显的特征:非常少的对象       基于...

junmail的专栏 397

antisamy:一个用于执行来自不受信任来源HTML的快速,可配置HTML清理的库

反萨米 一个用于执行来自不受信任来源HTML的快速,可配置HTML清理的库。 支持Java 7+。 换句话说,它是一个API,可以帮助您确保客户端不会在其提供给个人资料,注释等HTML中提供恶意的货物代码,这些代码会持久保存在服务器上。 关于Web应用程序的术语“恶意代码”通常表示“ JavaScript”。 通常,级联样式表仅在调用JavaScript时才被认为是恶意的。 但是,在许多情况下,可能以恶意方式使用“正常” HTML和CSS。 如何使用 1.导入依赖项 首先,添加来自Maven的依赖项: < dependency> < groupId>org.owasp.antisamy</ groupId> < artifactId>antisamy</ artifactId> < version>LATEST_VERSION</ version> </

Maven deploy本地jar后, Jenkins更新的时候无法下载jar

项目路径大概如下: -- parent |-- system-entity |-- system-interfaces pom.xml 问题 把system-entity的模块打包jar,然后通过maven进行deploy到内网的nexus里面,Jenkins再构建其他引入了system-entity.jar的项目的时候,提示无法下载system-entity.jar包. 排查 1.登录本地内网的Nexus查看,确认是否存在system-entity.jar.不存在这说明上传失败. 2.检查

cai454692590的博客 1732

org.apache.http.httpentity jar包-系列jar包

commons-codec-1.9.jar commons-logging-1.2.jar fluent-hc-4.5.3.jar httpclient-4.5.3.jar httpclient-cache-4.5.3.jar httpclient-win-4.5.3.jar httpcore-4.4.6.jar httpmime-4.5.3.jar jna-4.1.0.jar jna-platform-4.1.0.jar 其他信息: import org.apache.http.Header; import org.apache.http.HttpException; import org.apache.http.HttpRequest; import org.apache.http.HttpRequestInterceptor; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.conn.scheme.Scheme; import org.apache.http.conn.ssl.SSLSocketFactory; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.CoreConnectionPNames; import org.apache.http.protocol.HttpContext; jar包

从PWM到精准电流:双光耦与LM317构建的4-20mA隔离输出方案

本文详细介绍了基于双光耦与LM317的4-20mA隔离输出方案,适用于工业自动化中的PWM信号转换。该方案通过电气隔离和精准电流调节,显著提升抗干扰能力和信号传输精度,同时降低成本60%。文章还分享了电路设计、参数计算及调试经验,为工程师提供实用参考。

weixin_32006655的博客 283

Apache Geronimo Transaction Manager 使用教程

Apache Geronimo Transaction Manager 使用教程 1. 项目的目录结构及介绍 Apache Geronimo Transaction Manager 是一个开源的 Java 事务管理器,项目托管在 GitHub 上。以下是项目的目录结构及其介绍: geronimo-txmanager/ ├── github/ │ └── workflows/ ├── geron...

gitblog_00006的博客 360

NFS共享速度慢?麒麟系统性能调优5个关键参数实测对比

本文针对银河麒麟服务器系统上NFS共享速度慢的问题,通过实测对比分析了五个关键调优参数。重点探讨了rsize/wsize传输块大小、TCP/UDP协议选择、sync/async模式权衡等,并结合麒麟系统特性提供了具体的优化建议与配置示例,旨在帮助用户有效提升国产化环境下的文件共享性能。

weixin_29254029的博客 335

Apache Geronimo TxManager 使用教程

Apache Geronimo TxManager 使用教程 1. 项目的目录结构及介绍 Apache Geronimo TxManager 项目的目录结构如下: geronimo-txmanager/ ├── geronimo-transaction/ │ ├── src/ │ │ ├── main/ │ │ │ ├── java/ │ │ │ └── res...

gitblog_00454的博客 445

HTML爱心字母雨(完整代码)

HTML语言实现爱心字母雨的完整代码。

AIGC 2万+

【DevOps】rancher集成ldap,实现统一账号登录

登录到rancher控制台,选择【安全】-【认证】 选择【OpenLDAP】 其中【搜索起点】需要去ldap界面查配置完成

CN_Eden 1092

entity-engine简介

entity-engine是将Apache OfBiz中entity剥离出来,单独使用,没有entity eca。   项目地址:https://bitbucket.org/atlassian/entity-engine   需要jar包的朋友直接去https://maven.atlassian.com/#nexus-search;quick~entityengine下载   PO...

kegu的专栏 317

SAP中关于预测需求的重要载体:计划独立需求

预测,就是为了调节客户需求的波动、不平衡,使企业的生产计划、物料计划尽量平稳、有序的必备工具。计划独立需求,Planned Independent Requirement(PIR)是SAP系统中最常见的预测需求。

saplakes的博客 1879

OFbiz实体引擎

安全可靠的数据存储是数据管理战略的关键业务

program_thinker的专栏 1997

Windows 下 Hermes Agent(爱马仕)安装全记录:从零到一的保姆级图文教程

本文主要介绍了Windows 下 Hermes Agent(爱马仕) 安装全记录:从零到一的保姆级图文教程,希望能对学习使用 Hermes Agent 的同学们有所帮助。 文章目录 1. 背景 2. 实战教程 2.1 步骤一 安装WSL环境 2.2 步骤二 安装Python环境 2.3 步骤三 安装Hermes Agent 2.4 步骤四 修正 API Key 错误 2.5 步骤五 配置多个模型 2.6 步骤六 启动Hermes Agent 3. 常见问题及解决方案 4. 总结

weixin_43178406的博客 14万+

第二天 原来Labview语言结构也挺自然(2.1 顺序结构)

新建项目文件StructureTest.lvproj,新建四个子VI(FlatSequence.vi、CaseStructure.vi、LoopStructure.vi、EventStructure.vi),具体新建流程可以参考1.2节。

weixin_38637405的博客 1004

使用OFBIZ的理由和不使用OFBIZ的理由

1 使用OFBIZ的理由 1.1 什么是OFBIZ OFBIZ是由Sourceforge维护的一个最著名的开源项目之一,提供创建基于最新J2EE/XML规范和技术标准,构建大型企业级、跨平台、跨数据库、跨应用服务器的多层、分布式电子商务类WEB应用系统的框架。 OFBIZ 的Web应用框架以MVC模式搭建而成,整体采用了很多被大多数企业级应用系统公认的位于业务逻辑层和集成层(Business

李古拉雷 2万+

Open3D 机载点云电力线提取【2025最新版】

传统方法实现的机载点云电力线分割提取。博客长期更新,本文最新更新时间为:2025年3月23日。

点云侠的博客 4468

ofbiz之entity实体写法与解析

OFBiz之Entity简介:   一、Entity Engine屏蔽了数据库访问的很多细节,通过XML配置文件定义描述实体,实体引擎自动维护实体至数据库的所有细节   二、支持主流数据库,包括Oracle、MySql、MS SQL、PostgreSql等   三、OFBiz实体引擎与其他的ORM框架最大的不同,是OFBiz只有一个GenericValue对象,不像其它ORM框架,...

diemumao8812的博客 638

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

Ofbiz引擎之实体引擎(二)--配置参数

一、简介: 1、entityengine屏蔽了数据库的很多访问细节,通过xml配置文件定义描述实体,实体引擎自动维护实体至数据库的所有细节. 2、支持主流数据库, 3、与ORM最大的不同在于ofbiz只有一个GenericValue对象,不想其他ORM框架,要定义各种不同类型的对象.防止产生过多的实体对象,避免产生类爆炸. 4、Ofbiz的GenericValue对象不想ORM定义的实体

linux__xu的博客 1498

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

Apache OFBiz架构深度剖析:从实体引擎到服务框架的实现原理

Apache OFBiz是一个功能强大的开源企业级业务框架,它为构建企业级应用提供了一套完整的解决方案。在前100字的介绍中,我们将深入探讨Apache OFBiz的核心架构设计,特别是其实体引擎和服务框架的实现原理。这个企业级框架通过模块化的设计思想,为开发者提供了灵活的业务组件和强大的扩展能力,让企业应用开发变得更加高效和规范。 ## 🏗️ Apache OFBiz整体架构概览 Apac

gitblog_01040的博客 796

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

ofbiz_实体引擎

目  录 1.           背景.... 1 2.           OFBiz之Entity简介.... 1 3.           OFBiz实体配置.... 1 3.1          实体对应数据系统配置.... 1 3.2          实体代理(Entity Delegators). 1 3.3          数据源(datasourc

yangyaohua_sky的博客 2386

ofbiz实体引擎(九) 多租户

多租户在平台中是根据delegator不同操作不同的数据库 /** * @author 郑小康 * 设置完整的delegator 其可能形式有 default 或者defalut#tenantDelegatorName * 针对于第一种情况 delegatorBaseName =="default" delegatorTenantId=null

jack_Zheng的博客 1106

【亲测免费】 Apache OFBiz 框架使用教程

Apache OFBiz 框架使用教程 1. 项目的目录结构及介绍 Apache OFBiz 是一个功能强大的企业资源规划(ERP)框架,其目录结构组织得非常清晰,便于开发和维护。以下是主要的目录结构及其功能介绍: applications/: 包含各种业务应用程序,如会计、库存管理、订单处理等。 framework/: 包含框架的核心组件,如实体引擎、服务引擎和用户界面组件。 hot-depl...

gitblog_00084的博客 1353

OFBIZ

不可否认,OFBiz这个开源的系统功能是非常强大的,涉及到的东西太多了,其实对我们现在而言,最有用的只有这么几个:实体引擎、服务引擎、WebTools、用户权限管理。 最先要提醒各位的是,在配置一个OFBiz时,路径中千万不要包含空格,要不然老提示出错,无外乎是scoket write error之类的信息,会让你郁闷得不停^_^ 用户登录鉴权这一块我们完全可以照搬OFBiz现有的东西

Amilychen的专栏 1万+
上一篇: 让 TextView 的事件响应支持 Intent
javaEidolon
博客等级 码龄24年 1粉丝 1原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值