嗨,我很难在我搜索的代码中找出哪里出了问题,并试图找到答案,我还检查了我的数据库,看看我的命名约定是否与我的模型中的命名约定相匹配,并再次检查了我的BreakDown模型中的@列是否与我的财务模型中的@JoinColumn匹配。如果有人能帮我这是密码,我会很感激的
存储库
@Repository
public interface BreakDownRepository extends JpaRepository<BreakDown, Long>{
@Query(value = "select * from break_down as b left join finance as f "
+ "ON b.finance_id = f.finance_id where b.finance_id = :financeId",
nativeQuery = true)
List<BreakDown> findByFinanceId(@Param("financeId") Long financeId);
}财务模型
@Entity
public class Finance implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "finance_id")
private Long financeId;
@Column(name = "assets")
private int assets;
@Column(name = "profit")
private int profit;
@Column(name = "loss")
private int loss;
@Column(name = "revenue")
private int revenue;
@Column(name = "cost")
private int cost;
@Column(name = "f_date")
private Date fDate;
@Column(name = "currency")
private String currency;
@OneToOne
@JoinColumn(name = "emp_id")
private Employee employee;
@OneToOne
@JoinColumn(name = "customer_id")
private Customer customer;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "break_down_id")
private List<BreakDown> breakDown;
//setters gettersBreakDown模型
@Entity
public class BreakDown implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "break_down_id")
private Long breakDownId;
@Column(name = "item")
private String productName;
@Column(name = "cost")
private float price;
private int quantity;
private float total;
@ManyToOne
@JsonBackReference
private Finance finance;
//setters getters错误堆栈跟踪
2021-05-15T04:20:21.606644+00:00 app[web.1]: org.postgresql.util.PSQLException: The column name
finance_finance_id was not found in this ResultSet.谢谢,谢谢您的所有答复。
发布于 2021-05-15 05:43:43
使用JPA
List<BreakDown> findAllByFinanceFinanceId(Long financeId);在本机查询的情况下,您必须在break_down和JOIN表上使用连接
更新:
您应该在BreakDown类中使用get方法获取。我为同样的例子添加了这个例子。
@Entity
@Table(name = "a")
public class A {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;
....
@OneToMany(mappedBy = "a")
private Set<B> b = new HashSet<>();
public Set<B> getB() {
return b;
}
public A b(Set<B> b) {
this.b = b;
return this;
}
public A addB(B b) {
this.b.add(b);
b.setA(this);
return this;
}
public A removeB(B b) {
this.b.remove(b);
version.setA(null);
return this;
}
public void setB(Set<B> b) {
this.b= b;
}
}@Entity
@Table(name = "b")
public class B {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;
....
@ManyToOne
@JsonIgnoreProperties("b")
private A a;
public A getA() {
return a;
}
public B a(A a) {
this.a = a;
return this;
}
public void setA(A a) {
this.a = a;
}
}BRepositoty中的查询
List<B> findAllByAId(Long id);您可以使用A获取b.getA()的详细信息。
https://stackoverflow.com/questions/67543305
复制相似问题