0

Using Spring data I would like to be able to define a custom get-method inside a domain model class without affecting the model itself. For example, using this model:

@Document
public class Person
{
    private String firstName;
    private String lastName;

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }
}

Eveything is working fine so far: the model Person has the fields 'firstName' and 'lastName' and I can successfully save a 'person'. The resulting JSON has the fields 'firstName' and 'lastName'. Now I would like to add some additional data in the JSON without affecting the model and its save-operations, something like this:

@Document
public class Person
{
    private String firstName;
    private String lastName;

    public String getFirstName()
    {
        return firstName;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    // custom method
    public String getFullName()
    {
        return firstName+" "+lastName;
    }
}

The JSON should contain the same data as before, but this time also an additional 'fullName'-field. However, at the same time the data model assumes an additional field 'fullName' is added and filled with null-values when saving into the database.

I have already tried annotations like @Transient, but this does not work. The documentation states "by default all private fields are mapped to the document, this annotation excludes the field where it is applied from being stored in the database", so it only can be applied to private fields in the class, not to get-methods.

What is the correct way to do this in Spring? Of course I can extend the class Person and include the getFullName-method there, but I was wondering if it is possible to include everything in one class.

Edit:

I use Elasticsearch as DB engine using spring-data-elasticsearch 1.2.0.RELEASE. I have just tested MongoDB as alternative and then it is working fine, even without the @Transient annotation. I think the index-method of the ElasticsearchRepository is serializing the provided class instance when saving it to the database. In that way the JSON-output and the saved data are always identical. Any suggestions?

Neman
  • 1,237
  • 2
  • 13
  • 16
  • I think you can do this with transient field.Define transient field which name is fullname.And add get method for that. – mstzn Apr 28 '15 at 11:20
  • Maybe you tried with wrong `@Transient`, you should use `org.springframework.data.annotation.Transient` and not `javax.persistence.Transient` – Predrag Maric Apr 28 '15 at 11:20
  • "so it only can be applied to private fields in the class, not to get-methods". You are wrong here as the annotation both apply to fields or methods. Also note that if you had annotated a field with `@Id`, you should annotate a `fullName` field with `@Transient`. On the other hand, If if you had annotated a `getId()` method with `@Id`, you should annotate `getFullName()` with `@Transient` – Ori Dar Apr 28 '15 at 11:34
  • I am using the correct Transient and also tried orid's suggestion without success. It looks like spring-data-elasticsearch is the problem as it might serialize the object to JSON when indexing the document in the database, see my update in the start post. – Neman Apr 28 '15 at 12:52

0 Answers0