首页 > 解决方案 > 如何在 JPA 中将对象转换为实体

问题描述

如何在 JPA 中将对象转换为实体

我有一个 tqo 表要加入,我已经完成了,我也得到了响应。但问题就在这里,我将其作为数组列表。我希望这是我的实体列表。

这是回购

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();
}

这是我的控制器

  @GetMapping("/getTransictionsAndInstruments") 
      public List<Object[]> getTransitionInstrument(){
         return transictionrepository.getTransictionsAndInstruments(); 
      }

任何机构都可以帮助我如何做到这一点。我已经为我正在使用findAll()示例的一台服务器做到了这一点。

@GetMapping("/getTransictionData")
    public List<Transictions> getAllTransiction(){
        return transictionrepository.findAll();
        //return transictionrepository.getTransictionsAndInstruments();
    }

仪器

package com.springboot.Ole.Model;



import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "instrument")
public class Instrument {
    
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    
    @Column(name = "instrument")
    private String instrumentName;
    
    @Column(name = "ltp")
    private float LTP;
    
    @Column(name = "exchange")
    private String exchange;
    
    @Column(name = "joCaps")
    private String joCaps;
    
    
    
      @OneToMany(mappedBy = "instrument", fetch = FetchType.LAZY)
      private List<Transictions> transiction;
     
    
    

    public Instrument(String instrumentName, float lTP, String exchange, String joCaps) {
        super();
        this.instrumentName = instrumentName;
        LTP = lTP;
        this.exchange = exchange;
        
    }
    
    public Instrument () {
        
    }
    
    
        // Getter Setter

过渡

package com.springboot.Ole.Model;

import java.sql.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;


@Entity
@Table(name = "transiction")
public class Transictions {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long Id;
    
    @Column(name = "userId")
    private Long userId;
    
    @Column(name = "instrumentName")
    private String InstrumentName;
    
    @Column(name = "quantity")
    private int quantity;
    
    @Column(name = "price")
    private double price;
    
    @Column(name = "transictionDate")
    private Date transictionDate;
    
    
     @ManyToOne(fetch = FetchType.EAGER)
      
     @JoinColumn(name = "ltp") 
      private Instrument instrument;
     
    
    public Transictions() {
    }
    
    public Transictions(Long userId, String instrumentName, int quantity, double price, Date transictionDate) {
        super();
        this.userId = userId;
        InstrumentName = instrumentName;
        this.quantity = quantity;
        this.price = price;
        this.transictionDate = transictionDate;
    }
    
    // Getter Setter

标签: javaspringspring-bootjpaspring-data-jpa

解决方案


将此添加到您的 Transiction Entity 模型中。

  @ManyToOne
  @JoinColumn(name = "instrument", referencedColumnName = "instrumentName") 
  private Instrument instrument;

然后这将为您完成工作

@GetMapping("/getTransictionData")
    public List<Transictions> getAllTransiction(){
        return transictionrepository.findAll();
    }

为什么要把事情复杂化?

只是您想根据仪器名称将它们加入到表中。


推荐阅读