1

I have repository method where i am trying to fetch specific column from table. when i call findAllNamesAndID method i get Object array in return where i am expecting List of Customwidgets.

I am not sure whats wrong with below code.

I can use use constructor of Customwidgets in query annotation but i want to avoid that. Is there any workaround.

I have below domain class :-

public class Customwidgets implements Serializable {
    @Id
    private UUID id;

    @NotNull
    @Column(name = "name", nullable = false)
    private String name;

---
---
}

My Repository class :-

@Repository
public interface CustomwidgetsRepository extends JpaRepository<Customwidgets, UUID>, JpaSpecificationExecutor<Customwidgets> {

    @Query("select o.id, o.name from Customwidgets o")
    List<Customwidgets> findAllNamesAndID();

}

1 Answer 1

1

You can create an interface to define your projection. For instance:

public interface CustomWidgetsProjection {

   String getId();

   String getName();

}

Then you can return this interface you created instead of CustomWidgets:

@Query("select o.id, o.name from Customwidgets o")
List<CustomWidgetsProjection> findAllNamesAndID();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.