Set Attribute Value depending on two other values

Hey there,

so basically I have an Entity with firstname, surname and Symbol and all are Strings.
The user is only supposed to type in his first and surname, the Symbol should be created by that.
The Symbol is always the first 2 letters of the surname and the first letter of the firstname.
Example:
Name: John Mountain
Symbol: MOJ

I changed the setSymbol() method but i have no clue how and where to call it.

Thanks in advance

Hi!

In 7.0 you can subscribe to PreCommitEvent and there change your entity:

@Subscribe(target = Target.DATA_CONTEXT)
private void onPreCommit(DataContext.PreCommitEvent event) {
    MyEntity myEntity = getEditedEntity();
    myEntity.setSymbol("MySymbol");
}

In 6.10 you can override preCommit() method:

@Override
protected boolean preCommit() {
    MyEntity myEntity = getEditedEntity();
    myEntity.setSymbol("MySymbol");
    return super.preCommit();
}

Or better use entity lifecycle callbacks:

@Entity(name = "sample_MyEntity")
public class MyEntity extends StandardEntity {

    @PrePersist
    @PreUpdate
    public void updateSymbol() {
        setSymbol(...);
    }

1 Like

Thanks for taking the time to answer my question, that helped.