I am using EclipseLink as my JPA implementation and Postgresql as my DB.

JPA:

@Struct(name="label,.....")
        @Entity
        @Table(name="\"XYZ\"", schema="\"XYZ\"")
        @NamedQuery(name="XYZ.findAll", query="SELECT r FROM XYZ r")
        public class XYZ implements  Serializable {
            private static final long serialVersionUID = 1L;

            @Id
            @Column(name="\"resourceID\"")
            private String resourceID;

            @Array(databaseType = "text")
            @Column(name="\"label\"")
            private List<String> label;

    .........
            // getter/setters 

    }

I have a requirement such that for every List attribute in my JPA, whenever it's value is null, I want to save the list as of size 1 with first element set to null.

null-----> [null]

Then, unset it backwards while retrieving back i.e if list is coming as [null] set it to null and give back to the user.

[null]-----> null

Currently I can think of this approach of editing every getter/setter of List type, to set/unset the above requirement. Something like this

    public List<String> getLabels() {
        return unset(this.labels);
    }

    public void setLabels(List<String> labels) {
        this.labels = set(labels);
    }

Here, set & unset add the above list with first element null and reverse.

Is there a cleaner approach to this ?? With this I have to edit every gett/sett of List type which is a little cumbersome.

I could not find any annotations in EclipseLink directly for this to edit a value before persisting.

Any help would be appreciated

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.