Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

SometimeSometimes, you may want to have your own Hibernate entity and dao DAO to access extra database table tables in your plugin. It is very simple to achieve that by adding the following file files and class classes to your plugin.

Similar to development in Spring + Hibernate, a an application context file is required for your plugin. In my sample plugin, I created a productsApplicationContexta productApplicationContext.xml as below

Thai

บางครั้งคุณอาจต้องการให้องค์กรไฮเบอร์เนตของคุณและ dao เข้าถึงตารางฐานข้อมูลเพิ่มเติมในปลั๊กอินของคุณ มันง่ายมากที่จะทำเช่นนั้นโดยเพิ่มไฟล์และคลาสต่อไปนี้ลงในปลั๊กอินของคุณ คล้ายกับการพัฒนาใน Spring + Hibernate จำเป็นต้องมีไฟล์บริบทแอปพลิเคชันสำหรับปลั๊กอินของคุณ ในปลั๊กอินตัวอย่างของฉันฉันสร้าง productsApplicationContextproductApplicationContext.xml ดังนี้

Code Block
languagexml
titleApplication context file (productsApplicationContextproductApplicationContext.xml)
linenumberstrue
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlnsxsi:aopschemaLocation="http://www.springframework.org/schema/aop"
       xmlns:tx="beans http://www.springframework.org/schema/tx"beans/spring-beans-4.1.xsd">

    <bean   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsdid="productSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
 <property name="dataSource" ref="setupDataSource"/>
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<property name="mappingResources">
      <bean id="productSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <list>
    <property name="dataSource" ref="setupDataSource"/>
        <property name="mappingResources">
            <list>
                <value>/org/joget/sample/productsproduct/model/ProductsProduct.hbm.xml</value>
             </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.format_sql">false</prop>
            </props>
        </property>
    </bean>
    
    <bean id="productsDaoproductDao" class="org.joget.productsproduct.dao.ProductsDaoImplProductDaoImpl">
        <property name="sessionFactory" ref="productSessionFactory" />
    </bean>
    
</beans>


In the application context, I created 2 beans. Bean "productSessionFactory" is to initialize a session factory with the hibernate mapping file. Bean "productsDaoproductDao" is to initialize the dao DAO object of my sample plugin.

Next, we need a Hibernate Mapping mapping file. In my sample plugin, it is is /org/joget/sample/productsproduct/model/ProductsProduct.hbm.xml. It mapped the POJO "org.joget.productsproduct.model.Product" with "valuhibernate_productsproduct" table.

Thai

ในบริบทของแอปพลิเคชันฉันสร้าง 2 beans Bean "productSessionFactory" คือการเริ่มต้นโรงงานเซสชันด้วยไฟล์การทำแผนที่จำศีล Bean "productsDaoproductDao" คือการเริ่มต้นวัตถุ dao ของปลั๊กอินตัวอย่างของฉัน ต่อไปเราต้องใช้ไฟล์ Hibernate Mapping ในปลั๊กอินตัวอย่างของฉันมันเป็น /org/joget/sample/productsproduct/model/ProductsProduct.hbm.xml มันจับคู่ POJO "org.joget.products.model.Product" กับตาราง "valueshibernate_productsproduct"

Code Block
languagexml
titleHibernate mapping file (ProductsProduct.hbm.xml)
linenumberstrue
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class entity-name="ProductsProduct" name="org.joget.productsproduct.model.Product" table="valuhibernate_productsproduct">
        <id column="id" name="id" type="string"/>
        <property column="name" name="name" type="string"/>
        <property column="description" name="description" type="string"/>
    </class>
</hibernate-mapping>


You need a utility class to initialized initialize your application context and allow you to and retrieve the bean object.

Thai

คุณต้องการคลาสยูทิลิตี้เพื่อเริ่มต้นบริบทแอปพลิเคชันของคุณและอนุญาตให้คุณเรียกคืนออบเจ็กต์ bean

Code Block
languagejava
titleUtility class
linenumberstrue
package org.joget.productsproduct;

import org.joget.apps.app.service.AppUtil;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContextGenericXmlApplicationContext;

public class AppContext {

    private static AppContext instance;
    private AbstractApplicationContextfinal GenericXmlApplicationContext appContextspringApplicationContext;

    public synchronized static AppContext getInstance() {
        if (instance == null) {
            instance = new AppContext();
        }
        return instance;
    }

    private AppContext() {
        Threadthis.springApplicationContext currentThread= =new Thread.currentThreadGenericXmlApplicationContext();
        ClassLoader threadContextClassLoader = currentThread.getContextClassLoader(this.springApplicationContext.setValidating(false);
        try {
            currentThread.setContextClassLoaderthis.springApplicationContext.setClassLoader(this.getClass().getClassLoader());
            this.appContext = new ClassPathXmlApplicationContext(new String[]{"/productsApplicationContext.xml"}, this.getClass(), this.springApplicationContext.setParent(AppUtil.getApplicationContext());
        } finally {this.springApplicationContext.load("/productApplicationContext.xml");
            currentThread.setContextClassLoader(threadContextClassLoaderthis.springApplicationContext.refresh();
        }
    }

    public AbstractApplicationContext getAppContext() {
        return appContextspringApplicationContext;
    }

}

You need to orverride findSession method in  ProductsDaoImpl as compared to joget version 4.

Thai

คุณต้อง orverride วิธี findSession ใน ProductsDaoImpl เมื่อเปรียบเทียบกับ joget เวอร์ชัน 4

 


Sample DAO implementation

Code Block
languagejava
titleOverriding findSessionDAO Implementation
linenumberstrue
package org.joget.productsproduct.dao;

import java.util.Collection;
import org.joget.hibernate.Session;
import org.hibernate.SessionFactoryapps.app.service.AppUtil;
import org.joget.commons.spring.model.AbstractSpringDao;
import org.joget.commons.util.LogUtil;
import org.joget.products.model.Product;

public class ProductsDaoImpl extends AbstractSpringDao implements ProductsDao {

    @Override
    public Session findSession() {
;
import org.joget.product.model.Product;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

public class ProductDaoImpl extends AbstractSpringDao implements ProductDao {

    @Override
    public Boolean addProduct(Product product) {
        try {
            TransactionTemplate transactionTemplate = (TransactionTemplate) AppUtil.getApplicationContext().getBean("transactionTemplate");
            Boolean result = (Boolean)transactionTemplate.execute(new TransactionCallback() {
                @Override
                public Object doInTransaction(TransactionStatus ts) {
                    save("Product", product);
                    return true;
                }
            });
            return result;
        } catch (Exception e) {
            LogUtil.error(ProductDaoImpl.class.getName(), e, "Add Product Error!");
            return false;
        }
    }

    @Override
    public Boolean updateProduct(Product product) {
        try {
            TransactionTemplate transactionTemplate = (TransactionTemplate) AppUtil.getApplicationContext().getBean("transactionTemplate");
            Boolean result = (Boolean)transactionTemplate.execute(new TransactionCallback<Object>() {
                @Override
                public Object doInTransaction(TransactionStatus ts) {
                    merge("Product", product);
                    return true;
                }
            });
            return result;
        } catch (Exception e) {
            LogUtil.error(ProductDaoImpl.class.getName(), e, "Update Product Error!");
            return false;
        }
    }

    @Override
    public Boolean deleteProduct(String id) {
        try {
            TransactionTemplate transactionTemplate = (TransactionTemplate) AppUtil.getApplicationContext().getBean("transactionTemplate");
            Boolean result = (Boolean)transactionTemplate.execute(new TransactionCallback() {
                @Override
                public Object doInTransaction(TransactionStatus ts) {
                    Product product = getProduct(id);
                 Session session = null;
if (product != null) {
    SessionFactory sf = super.getSessionFactory();
         
        try {delete("Product", product);
            session = sf.getCurrentSession();
        } catch (Exception e) {}
return true;
          
        if (session ==} null)else {
            session  = sf.openSession();
        }
 return false;
       
        return session;
    }
     
    public Boolean addProduct(Product product) {
        try {}
            save("Products", product});
            return trueresult;
        } catch (Exception e) {
            LogUtil.error(ProductsDaoImplProductDaoImpl.class.getName(), e, "AddDelete Product Error!");
            return false;
        }
    }

    public Boolean updateProduct(Product product) {
        try {
            merge("Products", product);
    @Override
    public Product getProduct(String  return true;id) {
        }try catch{
 (Exception e) {
         TransactionTemplate transactionTemplate = LogUtil.error(ProductsDaoImpl.class.getName(), e, "Update Product Error!");
   (TransactionTemplate) AppUtil.getApplicationContext().getBean("transactionTemplate");
         return false;
  Product product = (Product)transactionTemplate.execute(new TransactionCallback()  }{
    }

    public Boolean deleteProduct(String id) {
    @Override
    try {
           public ProductObject product = getProduct(id);
    doInTransaction(TransactionStatus ts) {
        if (product != null) {
        return (Product) find("Product", id);
                delete("Products", product);}
            });
            return trueproduct;
        } catch (Exception e) {
            LogUtil.error(ProductsDaoImplProductDaoImpl.class.getName(), e, "DeleteGet Product Error!");
            return falsenull;
        }
    }

    @Override
    public ProductCollection<Product> getProductgetProducts(String id) {
        try {

            TransactionTemplate transactionTemplate return= (ProductTransactionTemplate) findAppUtil.getApplicationContext().getBean("ProductstransactionTemplate", id);
          } catch (Exception e  Collection products = (Collection)transactionTemplate.execute(new TransactionCallback<Object>() {
            LogUtil.error(ProductsDaoImpl.class.getName(), e, "Get Product Error!");
    @Override
                public Object doInTransaction(TransactionStatus returnts) null;{
         }
      }

    public Collection<Product>return getProducts() {find("Product", "", null, null, null, null, null);
        try {
       }
     Collection products = super.find("Products", "", null, null, null, null, null });
            return products;
        } catch (Exception e) {
            LogUtil.error(ProductsDaoImplProductDaoImpl.class.getName(), e, "Get Products Error!");
        }    return null;
        return null;}
    }
   
}

After you have implemented your POJO and dao DAO class, you should be able to use your dao DAO in your plugin as followingfollows.

Please refer to the attached sample plugin for the POJO and dao DAO implementation.

Thai

หลังจากที่คุณใช้งาน POJO และคลาส dao ของคุณคุณควรจะสามารถใช้ dao ของคุณในปลั๊กอินได้ดังต่อไปนี้ โปรดอ้างอิงปลั๊กอินตัวอย่างที่แนบมาสำหรับการใช้งาน POJO และ dao

View file
namehibernate-product-src.zip
height250


Code Block
languagejava
titlePOJO and dao implementationDAO usage
linenumberstrue
ProductsDaoProductDao productdao = (ProductsDaoProductDao) AppContext.getInstance().getAppContext().getBean("productsDaoproductDao");

Product p = new Product();
p.setId("001");
p.setName("Product A");
p.setDescription("Product A Descpription");

productdao.addProduct(p);


In this KB:sample this sample plugin, you are able to add, delete and list product by the following JSON API.

Thai

ใน KBใน KB:sample ปลั๊กอินตัวอย่างคุณสามารถเพิ่มลบและแสดงรายการผลิตภัณฑ์โดย JSON API ต่อไปนี้

Thai

เพื่อเพิ่ม

Code Block
titleTo add
http://localhost:8080/jw/web/json/plugin/org.joget.productsproduct.ProductsApiProductApi/service?_action=add&name=Product _A&desc=Product A _Descpription
Thai

ลบ

Code Block
titleTo delete
http://localhost:8080/jw/web/json/plugin/org.joget.productsproduct.ProductsApiProductApi/service?_action=delete&id=001
Thai

เพื่อแสดงรายการผลิตภัณฑ์ทั้งหมด

Code Block
titleTo list all products
http://localhost:8080/jw/web/json/plugin/org.joget.productsproduct.ProductsApiProductApi/service?_action=list