Sometime, you may want to have your own Hibernate entity and dao to access extra database table in your plugin. It is very simple to achieve that by adding the following file and class to your plugin.
Similar to development in Spring + Hibernate, a application context file is required for your plugin. In my sample plugin, I created a productsApplicationContext.xml as below
Thai |
---|
บางครั้งคุณอาจต้องการให้องค์กรไฮเบอร์เนตของคุณและ dao เข้าถึงตารางฐานข้อมูลเพิ่มเติมในปลั๊กอินของคุณ มันง่ายมากที่จะทำเช่นนั้นโดยเพิ่มไฟล์และคลาสต่อไปนี้ลงในปลั๊กอินของคุณ คล้ายกับการพัฒนาใน Spring + Hibernate จำเป็นต้องมีไฟล์บริบทแอปพลิเคชันสำหรับปลั๊กอินของคุณ ในปลั๊กอินตัวอย่างของฉันฉันสร้าง productsApplicationContext.xml ดังนี้ |
Code Block |
---|
|
<?xml version="1.0" |
<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" |
xmlns:aop="http://www.springframework.org/schema/aop" |
xmlns:tx="http://www.springframework.org/schema/tx" |
xsi:schemaLocation="http://www.springframework.org/schema/beans |
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd |
http://www.springframework.org/schema/aop |
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd |
http://www.springframework.org/schema/tx |
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> |
<bean
<bean id="productSessionFactory" |
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> |
<property
<property name="dataSource" |
<property
<property name="mappingResources"> |
<list>
<list>
<value>/org/joget/sample/products/model/Products.hbm.xml</value> |
<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 >false</prop>
</props>
</property>
</bean>
<bean id="productsDao" |
class="org.joget.products.dao.ProductsDaoImpl"> |
<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 "productsDao" is to initialize the dao object of my sample plugin.
Next, we need a Hibernate Mapping file. In my sample plugin, it is /org/joget/sample/products/model/Products.hbm.xml. It mapped the POJO "org.joget.products.model.Product" with "valu_products" table.
Thai |
---|
ในบริบทของแอปพลิเคชันฉันสร้าง 2 beans Bean "productSessionFactory" คือการเริ่มต้นโรงงานเซสชันด้วยไฟล์การทำแผนที่จำศีล Bean "productsDao" คือการเริ่มต้นวัตถุ dao ของปลั๊กอินตัวอย่างของฉัน ต่อไปเราต้องใช้ไฟล์ Hibernate Mapping ในปลั๊กอินตัวอย่างของฉันมันเป็น /org/joget/sample/products/model/Products.hbm.xml มันจับคู่ POJO "org.joget.products.model.Product" กับตาราง "values_products" |
Code Block |
---|
|
<?xml version="1.0" |
<!DOCTYPE hibernate-mapping |
PUBLIC PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" |
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> |
<
class
<class entity-name="Products" |
name="org.joget.products.model.Product" |
<id
<property
<property
<property column="description" |
class
>
class>
</hibernate-mapping> |
You need a utility class to initialized your application context and allow you to retrieve the bean object.
Thai |
---|
คุณต้องการคลาสยูทิลิตี้เพื่อเริ่มต้นบริบทแอปพลิเคชันของคุณและอนุญาตให้คุณเรียกคืนออบเจ็กต์ bean |
package
Code Block |
---|
|
package org.joget.products; |
org.joget.apps.app.service.AppUtil; |
org.springframework.context.support.AbstractApplicationContext; |
org.springframework.context.support.ClassPathXmlApplicationContext; |
{
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
{
{
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.setContextClassLoader(this.getClass().getClassLoader()); |
ClassPathXmlApplicationContext(new |
String[]{"/productsApplicationContext.xml"}, |
this.getClass(), AppUtil.getApplicationContext()); |
}
finally
{
} finally {
currentThread.setContextClassLoader(threadContextClassLoader); |
}
}
public
AbstractApplicationContext
}
}
public AbstractApplicationContext getAppContext() |
{
return
appContext;
}
}
You need to orverride findSession
method in ProductsDaoImpl
as compared to joget version 4.
Thai |
---|
คุณต้อง orverride วิธี findSession ใน ProductsDaoImpl เมื่อเปรียบเทียบกับ joget เวอร์ชัน 4 |
org.hibernate.SessionFactory; |
org.joget.commons.spring.model.AbstractSpringDao; |
org.joget.commons.util.LogUtil; |
org.joget.products.model.Product; |
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) {
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.class.getName(), e, |
return
false
;
}
}
public
Boolean updateProduct(Product product) {
try
{
return false;
}
}
public Boolean updateProduct(Product product) {
try {
merge("Products", product); |
return
true
;
}
catch
(Exception e) {
return true;
} catch (Exception e) {
LogUtil.error(ProductsDaoImpl.class.getName(), e, |
"Update Product Error!"); |
return
false
;
}
}
public
Boolean deleteProduct(String id) {
try
{
Product product = getProduct(id);
if
(product !=
null
) {
return false;
}
}
public Boolean deleteProduct(String id) {
try {
Product product = getProduct(id);
if (product != null) {
delete("Products", product); |
}
return
true
;
}
catch
(Exception e) {
}
return true;
} catch (Exception e) {
LogUtil.error(ProductsDaoImpl.class.getName(), e, |
"Delete Product Error!"); |
return
false
;
}
}
public
Product getProduct(String id) {
try
{
return
(Product)
return false;
}
}
public Product getProduct(String id) {
try {
return (Product) find("Products", id); |
}
catch
(Exception e) {
} catch (Exception e) {
LogUtil.error(ProductsDaoImpl.class.getName(), e, |
return
null
;
}
}
public
Collection<Product> getProducts() {
try
{
Collection products =
return null;
}
}
public Collection<Product> getProducts() {
try {
Collection products = super.find("Products", |
return
products;
}
catch
(Exception e) {
return products;
} catch (Exception e) {
LogUtil.error(ProductsDaoImpl.class.getName(), e, |
}
return
null
;
}
After you implemented your POJO and dao class, you should be able to use your dao in your plugin as following. Please refer to the attached sample plugin for POJO and dao implementation.
Thai |
---|
หลังจากที่คุณใช้งาน POJO และคลาส dao ของคุณคุณควรจะสามารถใช้ dao ของคุณในปลั๊กอินได้ดังต่อไปนี้ โปรดอ้างอิงปลั๊กอินตัวอย่างที่แนบมาสำหรับการใช้งาน POJO และ dao |
Code Block |
---|
|
ProductsDao productdao = (ProductsDao) AppContext.getInstance().getAppContext().getBean("productsDao"); |
p.setDescription("Product A Descpription"); |
productdao.addProduct(p); |
In this KB:sample plugin, you are able to add, delete and list product by the following JSON API.
Thai |
---|
ใน KB:sample ปลั๊กอินตัวอย่างคุณสามารถเพิ่มลบและแสดงรายการผลิตภัณฑ์โดย JSON API ต่อไปนี้ |
To add,
Code Block |
---|
http://localhost:8080/jw/web/json/plugin/org.joget.products.ProductsApi/service?_action=add&name=Product A&desc=Product A Descpription |
To delete,
Code Block |
---|
http://localhost:8080/jw/web/json/plugin/org.joget.products.ProductsApi/service?_action=delete&id=001 |
To list all products,
Thai |
---|
เพื่อแสดงรายการผลิตภัณฑ์ทั้งหมด |
Code Block |
---|
http://localhost:8080/jw/web/json/plugin/org.joget.products.ProductsApi/service?_action=list |