0

I have this query

String  query = "select ig.p1,
       ig.p2,
       ig.p3, 
       tg.p4
  from mytable ig, mytable2 tg
 where ..... and ....";

And I have DTO

public class MyDto{
//constructor
private String p1;
private String p2;
private String p3;
private String p4;
...
//getters/setters
}

I need get this select and mapping to DTO.

I create this in my DTO

@SqlResultSetMapping(
        name="findReport",
        classes={
                @ConstructorResult(
                        targetClass=MyDto.class,
                        columns={
                                @ColumnResult(name="p1",type = String.class),
                                @ColumnResult(name="p2", type = String.class),
                                @ColumnResult(name="p3", type = String.class),
                                @ColumnResult(name="p4", type = String.class),
                        }
                )
        }
)

And create DAO/Repository

@Component
public class ReportRepositoryImpl implements ReportRepository {
    @PersistenceContext
    private EntityManager em;

    @Override
    public Report findReportSelect() {
        Query query = em.createNativeQuery(
                "query","findReport");
        @SuppressWarnings("unchecked")
        Collection<MyDto> dto = query.getResultList();

        Iterable<MyDto> itr = dto;
        return (MyDto)itr;
    }
}

But I have error unchecked

My quations 1. How can I fix this error and what this error means? 2. Is there more easy way get this result?

user5620472
  • 2,722
  • 8
  • 44
  • 97

1 Answers1

0

The unchecked warning is because query.getResultList() returns an untyped Collection instead of Collection<MyDto>. If you call one of the EntityManager methods that returns a TypedQuery<T> instead of Query it will clear that warning. This SO answer has some good guidance for using @NamedNativeQuery with EntityManager.createNamedQuery(name, class) to accomplish that.

Community
  • 1
  • 1
Kevin Condon
  • 1,618
  • 10
  • 16