mercredi 9 juin 2021

How to get the name of an Attribute from an Entity

I have the following entity class:

public class Conversation { 
    private String id;
    private String ownerId;
    private Long creationDate;

    public Conversation(String id, String ownerId, Long creationDate){
        this.id = id;
        this.ownerId = ownerId;
        this.creationDate = creationDate;
    }
}

On other submodule through an external service, on each insertion, I recive a map of the following entities:

public class AttributeValue {
    private Sring s; //string attribute
    private String n; //number attribute

    public String getS() {
        return this.s;
    }

    public String getN() {
        return this.n;
    }

    public AttributeValue(String s, String n){
        this.s = s;
        this.n = n;
    }
}

//Example if I insert this conversation: new Conversation("1", "2", 1623221757971)
// I recive this map:
Map<String, AttributeValue> insertStream = Map.ofEntries(
    entry("id", new AttributeValue("1", null)),
    entry("ownerId", new AttributeValue("2", null)),
    entry("creationDate", new AttributeValue(null, "1623221757971"))
);

To read the ownerId field from the map, I have to do this:

String ownerId = insertStream.get("ownerId").getS();

My question is, instead of have to write: insertStream.get("ownerId"), exists any way through Reflection to read the name of the field from the entity (Conversation.ownerId)? This is because we want to mantain the submodule and If we make a change on the entitity, for example change ownerId for ownerIdentifier, the submodule shows a compilation error or is changed automatically.





Aucun commentaire:

Enregistrer un commentaire