Search code examples
javatelosys

Map not working when trying to get by entity name


I am using Telosys tool to generate entity classes and it is doing wonders for me. However I have a specific requirement to change some attributes of entities.

I have loaded all the attributes that need to be changed in a map and parsed it in my entity template. But when I am trying to iterate through the map using entity.name it fails saying there is no get method. This is my map: sample_map.vm

#set( $map = {
 "AABUHA": "Name",
 "ABAKTX": "Code",
 "ABABDZ": "Date"
 }

I parsed it in my template like this:

#parse("include/sample_map.vm")

And this is how I am trying to fetch the value for the corresponding entity

$map["${entity.name}"]

I also tried:

($map.get($field.name))

The error I get is either

$!map.[: no attribute '['

Or no method get()

Surprisingly, this works fine when I pass the value as hardcoded string.

Any suggestions please


Solution

  • In Velocity the error “: no attribute '['” ( or "no method 'get'" ) occurs when the key doesn’t exist in the map.

    So, I suppose that in your case you try to get an “entity.name” that is not defined in the map.

    To check if a key is defined in a map you can use : $map.containsKey("xx")

    See example here : https://doc.telosys.org/templates/velocity-objects#map

    You can also use : $map.getOrDefault(“key”, "default_value") to get a default value if the key doesn’t exist in the map