java - 如何在 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
解决方案
将此添加到您的 Transiction Entity 模型中。
@ManyToOne
@JoinColumn(name = "instrument", referencedColumnName = "instrumentName")
private Instrument instrument;
然后这将为您完成工作
@GetMapping("/getTransictionData")
public List<Transictions> getAllTransiction(){
return transictionrepository.findAll();
}
为什么要把事情复杂化?
只是您想根据仪器名称将它们加入到表中。
推荐阅读
- c++ - 我如何知道 set.insert() 是否由于 C++ 中的内存不足而失败?
- php - 当 textarea 值具有 iframe 时,Laravel POST 请求不起作用
- c++ - 如何将变量传递给 makefile 中的规则?
- c - 如何在 C 中使用 printf() 和 scanf() 而不转到下一行?
- java - 无需 unicode 转义即可读写 Java 属性
- node.js - 如何从更新的 Node 和 npm 版本中解决这些错误..?
- amazon-web-services - Aws Transribe 无法在 boto3 中没有 LanguageCode 的 start_transcription_job
- java - 如何将 Instant 转换为 Int
- r - 删除只有唯一值的列
- angular - 在 switchMap 中进行嵌套的 Http 调用