user1285928
user1285928

Reputation: 1476

Get result from native query using Java Interface

I have these Postgres tables:

create table deals_new
(
    id                  bigserial primary key,
    slip_id             text,
    deal_type           integer,
    timestamp           timestamp,
    employee_id         bigint
        constraint employee_id_fk
            references common.employees
);

create table twap
(
    id                 bigserial     primary key,
    deal_id            varchar          not null,
    employee_id        bigint
        constraint fk_twap__employee_id
            references common.employees,
    status             integer
);

create table common.employees
(
    id              bigint primary key,
    first_name      varchar(150),
    last_name       varchar(150)
);

I created this JPA repository:

public interface DealsRepository extends JpaRepository<Employee, Long> {

    @Query (value =
            "SELECT e.first_name, e.last_name " +
                    "FROM common.deals_new d " +
                    "JOIN common.employees e ON e.id = d.employee_id " +
                    "LEFT OUTER JOIN common.twap t on " +
                    "        t.deal_id = d.slip_id AND " +
                    "        d.timestamp between '11-11-2010' AND '11-11-2011' AND " +
                    "        d.deal_type in (1, 2) " +
                    "OFFSET :offset " +
                    "LIMIT :limit ",
            nativeQuery = true)
    List<IdsOnly> getHistoryAllPairsSearchParam(@Param("offset") int offset,
                                                      @Param("limit") int limit);
}

As you can see I get the result using this Interface:

public interface IdsOnly {
    String getFirstName();
    String getLastName();
}

private DealsList getResponseParams(List<IdsOnly> deals) {
        return new DealsList( // get here the values from the interface and convert them);
    }

It's not clear to me how I get the values from the interface and do some processing. Can you give me some advice how I can for example print them?

Upvotes: 2

Views: 3693

Answers (1)

v.ladynev
v.ladynev

Reputation: 19956

You have to use aliases in your select clause

SELECT e.first_name as firstName, e.last_name as lastName

They correspond getFirstName() and getLastName() methods names in the IdsOnly interface

public interface IdsOnly {
    String getFirstName();
    String getLastName();
}

Upvotes: 5

Related Questions