Simas Paškauskas
Simas Paškauskas

Reputation: 623

JAVA JPA returning few Entity variables with their names using interface

Lets say I have a simple class:

public class Human{

private String id;
private String name;
priavte String surname;
priavte String something;
priavte String something2;

//geters setters

}

and I have declared an Interface

public interface IHuman{
String getId;
String getName;
}

in my repository I call:

@Query("select r from Human r")
Page<IHuman> getIdAndName();

Which in turn returns me an array of IHuman atributes with id and name, but when parsing to JSON it has no variable names and returns only the values:

"content":[["id1","jeff"],["id2","Jones"],...]

so my question would be: is it possible to connect variables to their corresponding values using interface, to get something like:

"content":[["id":"id1","name":"jeff"],["id":"id2","name":"Jones"],...]

Upvotes: 1

Views: 114

Answers (2)

Simas Paškauskas
Simas Paškauskas

Reputation: 623

I legit don't know how/why this worked, but ok: switched Query to:

 @Query("SELECT r.id as Id, r.name as Name from human r)
 Page<IHuman> getIdAndName();

and in my IHuman class I added:

@Value("#{target.Id}")
String getId();
@Value("#{target.Name}")
String getName();

Upvotes: 0

dxjuv
dxjuv

Reputation: 909

You are writing

@Query("select r from Human r")
Page<IHuman> getIdAndName();

Which would return entire Human object.

But you only need id and name, so only retrieve that in the Query, like

@Query("select r.id, r.name from Human r")
Page<IHuman> getIdAndName();

And That should map your interface with your values.

Upvotes: 1

Related Questions