Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run two @NamedNativeQuery query on same entity Class

I want to define two @NamedNativequery on entity class . When tying to define eclipse gives a error.

Duplicate annotation of non-repeatable type @NamedNativeQuery. Only annotation types marked @Repeatable can be used multiple times at one target.

From that error , I know we cannot define two define two @NamedNativeQuery of the entity class like

    @Entity
    @Table(name = "abc")
    @NamedNativeQuery(name = "ABC.getSomeMethod1" query = "some_query",resultSetMapping ="abcDTO")//1st name query
   // @NamedNativeQuery(name = "some_name" query = "some_query",resultSetMapping ="some_dto")//try to define second query , but gives error
    public class ABC {

      }

I am using spring repository at dao layer to called the method which bind with this query

  public interface SomeInterface extends JpaRepository<ABC, Long> {


    @Query(nativeQuery =true)
   List<ABCDTO> getSomeMethod1(@Param("someParam1")  long someParam1, @Param("someParam2") String someParam2);


   }

The senario is that I want to run the 1st native sql (which run fine) query and then run the 2nd native sql query(want to run this also from same). How to solve this or What is the possible solution.

If this way I cannot run the two native sql query then is there any other to achive this.

like image 495
Ashwani Tiwari Avatar asked Aug 01 '16 16:08

Ashwani Tiwari


1 Answers

You can define multiple named queries like this

@NamedNativeQueries({
    @NamedNativeQuery(name = "ABC.getSomeMethod1" 
                      query = "some_query",resultSetMapping ="abcDTO"
    ),
  @NamedNativeQuery(name = "some_name" 
     query = "some_query",resultSetMapping ="some_dto"
    )
})

Then in the business layer under the transaction you can call these two queries one after another,

If its a simple join between two entities and select and display better go with join's. Always remember to have those columns index in the Table ;)

like image 53
Rohan S Raju Avatar answered Oct 14 '22 12:10

Rohan S Raju