Skip to content

Latest commit

 

History

History
81 lines (54 loc) · 1.45 KB

dao-listeners.md

File metadata and controls

81 lines (54 loc) · 1.45 KB

HOW-TO use DAO Listeners

This part introduces how to use DAO listeners

Additions to the model

  • Define an additional attribute in your Model, which will be updated by the listener.
    • Let's define a new int randomValue field, that will be randomly updated on creation and when the model is updated.
public class Sample extends Model implements BasicModel<Long> {

...

   @Basic
	private int randomValue;

...

   public int getRandomValue() {
		return randomValue;
	}

	public void setRandomValue(int randomValue) {
		this.randomValue = randomValue;
	}
   
...

Create a new DAOListener

  • Create a DAOListener that updates the randomValue field before create and update operations:
public class SampleDAOListener implements DAOListener<Long, Sample> {

   Random random = new Random();
	

	@Override
	public void beforeCreate(Sample m) {
		m.setRandomValue(random.nextInt());
	}

	@Override
	public void beforeUpdate(Sample m) {
		m.setRandomValue(random.nextInt());
	}

	@Override
	public void afterCreate(Long key, Sample m) {
	}

	@Override
	public void afterRemove(Long key, Sample m) {
	}

	@Override
	public void afterUpdate(Sample m) {
	}

	@Override
	public void beforeRemove(Long key) {
	}
}

Register the DAOListener to the DAO

  • Add the SampleDAOListener in SampleDAO constructor:
   public SampleDAO() {
		super(Long.class, Sample.class);
		addListener(new SampleDAOListener());
	}