@PreUpdate with @Transient - EclipseLink

2

I need an @PreUpdate method to be called when an @Transient attribute is modified. This @Transient will always be modified, so yes @PreUpdate would always have to be called. Since it will always have to be called, I looked for an option similar to @PreUpdate, but it was called regardless of whether it has changed attributes or not, as it is this method that will set the value of a column to be persisted, according to the value no @Transient. The alternative I found was to define the following attributes:

@Column(name="valor")
private Double valor;

@Column(name="valor", insertable=false, updatable=false)
private Double valorTransientDisfarcado;

In this way, the "transient" will always be loaded with the column value that it will possibly change. If this "transient" is changed, it will fall into @PreUpdate and perform some operations that will define whether the value it holds will be passed to the original attribute be persisted.

  

The question is: What problems can this approach bring?

And is there any alternative to this value "transient" ?
Better yet, is there an alternative to @PreUpdate, so that the method is called at the last possible moment, regardless of whether there are changes so far to be persisted, so that it only performs the verification of the modified attributes after this method? / p>     

asked by anonymous 22.11.2017 / 19:13

1 answer

0

Following good principles of separation of concerns (SoC) would not be legal to delegate this rule to its layer of data persistence. Note that by looking only at your code snippet, there is already a redundancy in the column name, which makes it difficult to read.

Prefer to use the Object Orientation (OO) tools since this field is not persisted and use, for example, a decorator in your class with the appropriate getters / setters.

See Python Zen , precepts of good programming practice, which

  

Explicit is better than implicit.

So I strongly recommend that you place your rules in a DAO and write automated test cases. It is a much less tortuous path than looking at the life cycle of entities.

    
22.11.2017 / 21:37