You can easily add Callback classes in order to complete persist, merge or remove processes in the EJB.
For example, how to automatically set value for lastUpdate property in Film class when a record is created or updated.
A callback class must extends class org.adichatz.common.ejb.AEntityCallback. You can override preMerge, prePersist, preRemove,
postMerge, postPersist or postRemove methods to finalize processing. Zero, one or more callback classes can be specified to be executed during saving data changes.
If you declare several callback classes for an entity,
Create class org.mycompany.myproject.model.callback.FilmCallback as below:
package org.mycompany.myproject.model.callback; import java.util.Date; import javax.persistence.EntityManager; import org.adichatz.common.ejb.AEntityCallback; import org.mycompany.myproject.model.Film; public class FilmCallback extends AEntityCallback<Film> { public FilmCallback(EntityManager entityManager) { super(entityManager); } @Override public void preMerge(Film bean) { bean.setLastUpdate(new Date()); } @Override public void prePersist(Film bean) { bean.setLastUpdate(new Date()); } }
Remarks:
Open file $projectDirectory/resources/xml/model/film/FilmDIMM.xml.
Following XML elements:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <entityTree xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" entityURI="adi://myproject/model.film/FilmMM" idFieldName="filmId" xsi:noNamespaceSchemaLocation="http://www.adichatz.org/xsd/v0.8.7/generator/entityTree.xsd"> <propertyField mandatory="true" id="filmId"> ... ...
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <entityTree xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" entityURI="adi://myproject/model.film/FilmMM" idFieldName="filmId" callbackClassNames="org.mycompany.myproject.model.callback.FilmCallback" xsi:noNamespaceSchemaLocation="http://www.adichatz.org/xsd/v0.8.7/generator/entityTree.xsd"> <propertyField mandatory="true" id="filmId"> ...
Remark:
Line 4: a Callback class is defined.
if you want to set muliple callback classes set callbackClassNames property with class names separated by comma:
callbackClassNames=“org.mycompany.myproject.model.callback.FilmCallback, org.mycompany.myproject.model.callback.CommonCallback”
Open file $projectDirectory/resources/xml/scenario.xml.
<actionResources> ... <copyResource sourceURI="#PLUGINHOME()/src/org/mycompany/myproject/call" targetURI="#EJBJARFILE()!org/mycompany/myproject/call" actionWhen="WHEN_BUILDING_EJB_JAR" relative="false" throwError="false"/> </actionResources>
Remark:
Line 3: package org.mycompany.myproject.call, containing callback classes is added to EJB.
|
Callback classes are implemented on the server side. The same principle can be used on the client side using extensions of the class org.adichatz.common.ejb.AEntityForeback and setting class names in callforeClassNames property