开发者

Injecting fields into Java JPA fields

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, le开发者_StackOverflowt'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?


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜