vendredi 31 mai 2019

Is it possible to remove a property from a java object at run time?

I have a pojo with jpa anntations. One of the string properties is marked as not nullable. There is some code downstream which sets the value if it is not set. In certain cases this code needs to be triggered but if set the value to empty string this is treaded as a value and its not triggered. I can't set the non null value to null and so I can't edit theobject to trigger the code in a conventional way.

To be honest this bit of code needs a refactoring anyway and I do have a work around but I'm sure I've seen a way to 'remove' the peroperty somewhere before (perhaps reflection ?)

So the class looks something like this...

@Entity
@Table(name="Example")
@SequenceGenerator(name = "seq1", sequenceName = "seq1")
public class Example implements Serializable {

    @Column(name="name", nullable=true, length = 100)
    private java.lang.String name;

    @Column(name="id", nullable=false)      @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="SEQ1_Type_SEQ")
    @Id
    private long oID;

    @Column(name="extraval", nullable=false, length = 100)
    private java.lang.String entityVersion;4

...

And extraVal gets set by various jpa/db constraints if it isn't set in the pojo. Sometimes I would like to trigger this, even though it has already been set, but setting the value to empty string sets it to a value and setting to null triggers an error.

I can actually simulate the value in advance and I hope to refactor the code at some point so that I can do all my checks before I create the object in the first place so I'm not look to copy/recreate the object etc. Like I say I'm just sure there is an elegant way to remove the property as a short term solution ?





Aucun commentaire:

Enregistrer un commentaire