Reputation: 12121
I'm no pro with Java, so I need a little help. I'm using the Play Framework.
I have an Entity class which extends GenericModel with fields like the following:
@Column(name = "description")
private String description;
I want to add an additional field using a getter, let's call it getToString, which basically contains a read only string with the string representation of the entity.
I need this because the object is getting sent as a JSON response, and my JavaScript will read this field, and display it where for example the entity needs to be represented as a string.
How do I go about doing this?
Upvotes: 1
Views: 352
Reputation: 12121
The problem I'm having was a side effect of using GsonBuilder. The builder doesn't appear to be parsing getters and setters, unless the source of the library is modified, which I'm not willing to do.
Upvotes: 1
Reputation: 16439
For what I understand (please correct me if I'm wrong) you want a read-only method that will return a string representation (JSon format) of the entity.
You could just override the default toString method:
@Override
public String toString() {
return "your_json_string";
}
and call it when needed
Upvotes: 0
Reputation: 11672
I'm no expert on the Play framework, but probably you should have a look at the @Transient annotation.
Fields (and getters/setters if you are using JPA property access) marked with @Transient will be ignored by JPA, but usually be considered by other frameworks.
Upvotes: 2