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.

Step 1: Change 'FilmMM.axml' file

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">
    ...
    ...


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:

<?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"
            callbackClassName="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.



Step 2: 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<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:

  • Line 15-17: Sets value for lastUpdateproeprty when bean is updated.
  • Line 19-21: Sets value for lastUpdateproeprty when bean is inserted.



Step 3: Build and deploy EJB

 Film detail (improved)

  • Open file $projectDirectory/resources/xml/Scenario.xml with Adichatz editor.
  •  Generate scenario Select Generate scenario at top right of editor.
  • Check 'Generate EJB' and 'DeployEJB on application server' options and select 'OK' button.



Step 4: Execute

  • Open editor for Film 1.
  • Change a value and save editor.
  • Check that lastUpdate property has changed.
tutorial/add_callback.txt · Last modified: 2015/10/05 17:52 (external edit)