business logic in before save unexpectedly changes result

I have an object called ‘Coupon’ that has a relation to a Genre object. The Genre objects have description column with a not null constraint on them.

I have all the genres created in the system and ready to use.

When I add a coupon from the rest client using this

{
“eventSummary”: “testing”,
“genres”: [
{
“___class”: “Genre”,
“objectId”: “216BB46C-5BD7-9DCA-FF3C-A8EF613DFD00”
}
]
}

It all works fine, it adds the Coupon and references the existing Genre with the objectId of “216BB46C-5BD7-9DCA-FF3C-A8EF613DFD00”.

However if I add a beforeSave event with no code in it (so it does nothing), I get the error

{
“code”: 35,
“message”: “Column ‘description’ cannot be empty”
}

This implies that it is trying to create the Genre rather than reference the existing one.

What am I don’t wrong here.

Thank you

Ian Firth

What happens when you remove the not null rule?

Hi Ian,

I scheduled an internal ticket to investigate this problem. If you need to inquire about the status, the ticket number is BKNDLSS-11649

Regards,
Mark

If the not null rule is removed then it all works fine.

In this case, do the objecs get related to New objects or existing ones?

Yes, I was getting new objects when it worked.

Hi Ian,

We had investigated your problem but unfortunately it appeared much more tricky than we expected. We can not offer you immediate fix, however we you can try following steps to make your code work:

  1. Change signatures of your event handlers to accept HashMap instead of your class like this:
@Asset( "Coupon" ) 
public class CouponTableEventHandler extends com.backendless.servercode.extension.PersistenceExtender<HashMap> 
{ 
 @Override 
 public void beforeCreate( RunnerContext context, HashMap o ) throws Exception 
 { 
 } 
 
 
 



  1. Disable class mappings in Bootstrap.java. You should remove fields like this:
Backendless.Persistence.mapTableToClass( "Coupon", Coupon.class ); 
Backendless.Persistence.mapTableToClass( "Genre", Genre.class ); 
 
 
 



This approach has one major drawback, you could not be able to operate with classes, only with HashMaps.

Best redards,
Artur.