Mysql
 SQL >> Datenbank >  >> RDS >> Mysql

Zwei Tabellen im JPA-Repository verknüpfen

Da Sie native Abfragen verwenden, müssen Sie nativeQuery = true im Query-Attribut angeben. Außerdem sollten Sie die @Repository-Annotation zum Interface TransictionRepository hinzufügen – das ist Ihre DAO-Schicht.

package com.overflow.overflow.service;

import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import com.overflow.overflow.models.Transictions;

@Repository
public interface TransictionRepository extends JpaRepository<Transictions, Long> {

    @Query(nativeQuery = true, value =
        "SELECT transiction.user_id, transiction.quantity, "
      + "transiction.instrument_name, transiction.Price, instrument.LTP "
      + "FROM instrument "
      + "INNER JOIN transiction "
      + "ON instrument.instrument = transiction.instrument_name")
    public List<Object[]> getTransictionsAndInstruments();
}

Tipp: Für typisierte Ergebnisse empfiehlt sich statt List<Object[]> eine projektspezifische DTO-Klasse als Rückgabetyp.