【发布时间】:2021-07-02 10:13:20
【问题描述】:
如何在 JPA 中将 Object 转换为实体
我有一个 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
【问题讨论】:
-
你能添加类 Transicions 吗?我认为问题来自您请求中的属性名称。尝试添加别名(例如 transiction.user_id as user_id)
标签: java spring spring-boot jpa spring-data-jpa