0

I have next situation.

I have Entity which doesn't have representation in database. Also I have 3 table in database which have fields which could be mapped into my entity. So using spring data jpa I want write such method, so I could get this fields in my entity and map them in it.

As I understood I can't use JPQL cause table don't have entities class equivalents. So I used native query with my method

@Query(nativeQuery = true,      value = "")
public List<UnitConfig> findBysomeName(String someName);

But I got next exception: org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.Object[]] to type

As I understood Spring data jpa can't map result to my entity. How can I implement such method???

Bohdan Myslyvchuk
  • 1,657
  • 3
  • 24
  • 39
  • *I have Entity which doesn't have representation in database*: this is wrong already. If your class represents the result of a specific query, and not a row of a table in the database, then it shouldn't be an entity, but a plain old Java class. The query you posted is `""`. That is not valid SQL. It's not even clear why you're using SQL and not JPQL, BTW. JPQL can return somethid other than entities, just like SQL. See https://stackoverflow.com/questions/36328063/how-to-return-a-custom-object-from-a-spring-data-jpa-group-by-query for example. – JB Nizet Sep 01 '17 at 14:02
  • @JBNizet you are right, it is not entity, it is POJO. I've written "" , because I didn't wanted to write my query. The problem is that tables from which I'm getting the result doesn't have entites, so I can't use JQPL. Or I can? – Bohdan Myslyvchuk Sep 04 '17 at 05:53

1 Answers1

1

Try projection query in @Query annotation like

"select new com.mypackage.MyEntity(a.id, b.value, ...) from AEntity a, BEntity b where a.b_id = b.id"

RTM: JPQL Language Reference

Cepr0
  • 28,144
  • 8
  • 75
  • 101