Sometime, 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 class 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 จำเป็นต้องมีไฟล์บริบทแอปพลิเคชันสำหรับปลั๊กอินของคุณ ในปลั๊กอินตัวอย่างของฉันฉันสร้าง productApplicationContext.xml ดังนี้ |
Code Block |
---|
language | xml |
---|
title | Application context file (productApplicationContext.xml) |
---|
linenumbers | true |
---|
|
<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" |
xsi:schemaLocation="http://www.springframework.org/schema/beans |
http://www.springframework.org/schema/beans/spring-beans- |
2.5.xsd
http:
http:
<bean 4.1.xsd">
<bean id="productSessionFactory" |
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> |
<property
<property name="dataSource" |
<property
<property name="mappingResources"> |
<list>
<list>
<value>/org/joget/sample/ |
productsProducts
<property
<property name="hibernateProperties"> |
<props>
<prop
<props>
<prop key="hibernate.hbm2ddl.auto">update</prop> |
<prop
<prop key="hibernate.show_sql" |
>
false
</prop>
<prop >false</prop>
<prop key="hibernate.format_sql" |
>
false
</prop>
</props>
</property>
</bean>
<bean id=
"productsDao"
>false</prop>
</props>
</property>
</bean>
<bean id="productDao" class="org.joget. |
productsProductsDaoImpl
<property
<property name="sessionFactory" |
ref="productSessionFactory" |
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 "valu_products" table.hibernate_product" table.
Thai |
---|
ในบริบทของแอปพลิเคชันฉันสร้าง 2 beans Bean "productSessionFactory" คือการเริ่มต้นโรงงานเซสชันด้วยไฟล์การทำแผนที่จำศีล Bean "productDao" คือการเริ่มต้นวัตถุ dao ของปลั๊กอินตัวอย่างของฉัน ต่อไปเราต้องใช้ไฟล์ Hibernate Mapping ในปลั๊กอินตัวอย่างของฉันมันเป็น /org/joget/sample/product/model/Product.hbm.xml มันจับคู่ POJO "org.joget.products.model.Product" กับตาราง "hibernate_product" |
Code Block |
---|
language | xml |
---|
title | Hibernate mapping file (Product.hbm.xml) |
---|
linenumbers | true |
---|
|
<!DOCTYPE hibernate-mapping |
PUBLIC PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" |
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> |
<
class
Products products valuproducts
<id
<property
<property
<property column="description" |
class
>
class>
</hibernate-mapping> |
You need a utility class to initialized initialize your application context and allow you to and retrieve the bean object.
package
Thai |
---|
คุณต้องการคลาสยูทิลิตี้เพื่อเริ่มต้นบริบทแอปพลิเคชันของคุณและอนุญาตให้คุณเรียกคืนออบเจ็กต์ bean |
Code Block |
---|
language | java |
---|
title | Utility class |
---|
linenumbers | true |
---|
|
package org.joget. |
products;
import
product;
import org.joget.apps.app.service.AppUtil; |
org.springframework.context.support.AbstractApplicationContext; |
org.springframework.context.support. |
ClassPathXmlApplicationContext;
public
class
AppContext {
private
static
AppContext instance;
private
AbstractApplicationContext appContext;
public
synchronized
static
AppContext getInstance() {
if
(instance ==
null
) {
instance =
new
AppContext();
}
return
instance;
}
private
AppContext() {
Thread currentThread = Thread.currentThread();
ClassLoader threadContextClassLoader = currentThread.getContextClassLoader();
try
{
currentThread.setContextClassLoaderGenericXmlApplicationContext;
public class AppContext {
private static AppContext instance;
private final GenericXmlApplicationContext springApplicationContext;
public synchronized static AppContext getInstance() {
if (instance == null) {
instance = new AppContext();
}
return instance;
}
private AppContext() {
this.springApplicationContext = new GenericXmlApplicationContext();
this.springApplicationContext.setValidating(false);
this.springApplicationContext.setClassLoader(this.getClass().getClassLoader()); |
this
.appContext =
new
ClassPathXmlApplicationContext(
new
String[]{
"/productsApplicationContext.xml"
},
this
.getClass(),
this.springApplicationContext.setParent(AppUtil.getApplicationContext()) |
);;
this.springApplicationContext.load("/productApplicationContext.xml");
this.springApplicationContext.refresh();
}
public AbstractApplicationContext getAppContext() {
return springApplicationContext;
}
} |
Sample DAO implementation
Code Block |
---|
language | java |
---|
title | DAO Implementation |
---|
linenumbers | true |
---|
|
package org.joget.product.dao;
import |
}
finally
{
currentThread.setContextClassLoader(threadContextClassLoader);
}
}
public
AbstractApplicationContext getAppContext() {
return
appContext;
}
}
You need to orverride findSession
method in ProductsDaoImpl
as compared to joget version 4.
package
org.joget.products.dao;
import
hibernate.Session;import
org.hibernate.SessionFactory;
import
joget.apps.app.service.AppUtil;
import org.joget.commons.spring.model.AbstractSpringDao; |
org.joget.commons.util.LogUtil; |
products;
public
class
ProductsDaoImpl
extends
AbstractSpringDao
implements
ProductsDao {
@Override
public
Session findSession() {
Session session =
null
;
SessionFactory sf =
super
.getSessionFactory();
try
{
session = sf.getCurrentSession();
}
catch
(Exception e) {}
if
(session ==
null
) {
session = sf.openSession();
}
return
session;
}
public
Boolean addProduct(Product product) {
try
{
save(
"Products"
, product);
return
true
;
}
catch
(Exception e) {
LogUtil.error(ProductsDaoImpl;
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, |
return
false
;
}
}
public
Boolean updateProduct(Product product) {
try
{
merge(
"Products"
, product);
return
true
;
}
catch
(Exception e) {
LogUtil.error(ProductsDaoImpl
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
;
}
}
public
Boolean deleteProduct(String id) {
try
{
Product product = getProduct(id);
if
(product !=
null
) {
delete(
"Products"
, product);
}
return
true
;
}
catch
(Exception e) {
LogUtil.error(ProductsDaoImpl
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);
if (product != null) {
delete("Product", product);
return true;
} else {
return false;
}
}
});
return result;
} catch (Exception e) {
LogUtil.error(ProductDaoImpl.class.getName(), e, |
"Delete Product Error!"); |
return
false
;
}
}
public
Product getProduct(String id) {
try
{
return
(Product) find(
"Products"
, id);
}
catch
(Exception e) {
LogUtil.error(ProductsDaoImpl
return false;
}
}
@Override
public Product getProduct(String id) {
try {
TransactionTemplate transactionTemplate = (TransactionTemplate) AppUtil.getApplicationContext().getBean("transactionTemplate");
Product product = (Product)transactionTemplate.execute(new TransactionCallback() {
@Override
public Object doInTransaction(TransactionStatus ts) {
return (Product) find("Product", id);
}
});
return product;
} catch (Exception e) {
LogUtil.error(ProductDaoImpl.class.getName(), e, |
return
null
;
}
}
public
Collection<Product> getProducts() {
try
{
Collection products =
super
.find(
"Products"
,
""
,
null
,
null
,
null
,
null
,
null
);
return
products;
}
catch
(Exception e) {
LogUtil.error(ProductsDaoImpl
return null;
}
}
@Override
public Collection<Product> getProducts() {
try {
TransactionTemplate transactionTemplate = (TransactionTemplate) AppUtil.getApplicationContext().getBean("transactionTemplate");
Collection products = (Collection)transactionTemplate.execute(new TransactionCallback<Object>() {
@Override
public Object doInTransaction(TransactionStatus ts) {
return find("Product", "", null, null, null, null, null);
}
});
return products;
} catch (Exception e) {
LogUtil.error(ProductDaoImpl.class.getName(), e, |
}
return
null
;
}
}
After you have
After you 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 implementation.DAO implementation.
Thai |
---|
หลังจากที่คุณใช้งาน POJO และคลาส dao ของคุณคุณควรจะสามารถใช้ dao ของคุณในปลั๊กอินได้ดังต่อไปนี้ โปรดอ้างอิงปลั๊กอินตัวอย่างที่แนบมาสำหรับการใช้งาน POJO และ dao |
In this KB:sample In this sample plugin, you are able to add, delete and list product by the following JSON API.
Thai |
---|
ใน KB:sample ปลั๊กอินตัวอย่างคุณสามารถเพิ่มลบและแสดงรายการผลิตภัณฑ์โดย JSON API ต่อไปนี้ |
, |
http://localhost:8080/jw/web/json/plugin/org.joget. |
productsProductsApiProductApi/service?_action=add&name=Product_A&desc=Product |
A Descpription , |
http://localhost:8080/jw/web/json/plugin/org.joget. |
productsProductsApiProductApi/service?_action=delete&id=001 |
Thai |
---|
เพื่อแสดงรายการผลิตภัณฑ์ทั้งหมด |
Code Block |
---|
title | To list all products |
---|
|
, |
http://localhost:8080/jw/web/json/plugin/org.joget. |
productsProductsApiProductApi/service?_action=list |