Search code examples
javaspringspring-bootprojection

How to set variable in Java projection?


I have the following projection in my Java (Spring Boot) app:

public interface DemoProjection {
    UUID getUuid();
    String getName();
    String getSurname();

    String getLabel();
    void setLabel(String label);

I want to set this label field in my service:

public DemoProjection findAByUuid(UUID uuid) {
        // returns a single record
        final DemoProjection demo =  demoRepository.findAByUuid(uuid);
        final String label = String.format(
                "Name: %s%nSurnamame Surname: %s",
                demo.geteName(),
                demo.getSurname());

        // ! this line throws error
        demo.setLabel(label);
        return demo;
    }

demo.setLabel(label); throws error: "cannot set projection A TupleBackedMap cannot be modified."

So, how can I set label field in the projection?


Solution

  • You can't actually do that. Spring Data uses proxies and AOP to bind the query result to your projection interface, and since it's an interface you can't change that in a magical way. I would suggest creating a model/dto and map your projection values there:

    public class DemoDto {
      private String uuid;
      private String name;
      private String surname;
      
      //getters and setters ommited for brevity
    }
    

    You can create a DemoDto object with your desired values.

    In alternative, you can use a projection class instead of a projection interface. Check spring docs on how to do that :)