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,
=== Step 1: Create the Callback class ===
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 {
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:\\
* Line 15-17: Sets value for lastUpdate property when bean is updated.
* Line 19-21: Sets value for lastUpdate property when bean is inserted.
\\ \\
=== Step 2: Change 'FilmMM.axml' file ===
Open file $projectDirectory/resources/xml/model/film/FilmDIMM.xml.\\ \\
**Following XML elements:**
...
...
\\
\\
**Change XML elements**
* Copy FilmMMGENERATED.axml file to FilmMM.axml which will become the reference for generated code.
* Replace above XML lines with the following ones:
...
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"
\\ \\
=== Step 3: Change scenario.xml file for adding callback resources when building EJB ===
Open file $projectDirectory/resources/xml/scenario.xml.
...
Remark:\\
Line 3: package org.mycompany.myproject.call, containing callback classes is added to **EJB**.
\\ \\
=== Step 4: Build and deploy EJB ===
{{tutorial:generate_ejb.png?450| Generate EJB}}
* Open file $projectDirectory/resources/xml/Scenario.xml with Adichatz editor.
* {{img_generate_scenario.png| Generate scenario}} Select Generate scenario at top right of the editor.
* Check 'Generate EJB' and 'DeployEJB on application server' options and select 'OK' button.
\\ \\
=== Step 5: Execute ===
* Open editor for Film 1.
* Change a value and save editor.
* Check that lastUpdate property has changed.
\\ \\
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