【问题标题】:org.postgresql.util.PSQLException: The column name finance_finance_id was not found in this ResultSetorg.postgresql.util.PSQLException:在此 ResultSet 中找不到列名 finance_finance_id
【发布时间】:2021-05-15 04:39:03
【问题描述】:

您好,我正在努力寻找我的代码中的问题,我已经搜索并试图找到它的答案,我还检查了我的数据库以查看我的命名约定是否与我的模型中的命名约定相匹配,并仔细检查了如果我的 BreakDown 模型中的 @Column 与我的财务模型中的 @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 getters

分解模型

@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.

提前感谢您的所有回答

【问题讨论】:

  • 您正在尝试对没有 finance_id 列的 break_down 表运行本机查询。你在这里的目标是什么?
  • 那么我应该在 BreakDown 模型中添加@Column(name = "finance_id") 吗?
  • 我不知道你想做什么。请添加示例数据。
  • 我正在尝试选择具有相同财务 ID 的细分表中的所有数据
  • 这能回答你的问题吗? OneToMany & ManyToOne mapping JPA / Hibernate

标签: postgresql spring-boot


【解决方案1】:

使用 JPA

List<BreakDown> findAllByFinanceFinanceId(Long financeId);

在本机查询的情况下,您必须使用 JOIN ON break_downfinance


更新:

您应该使用 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);

您可以使用b.getA()获取A的详细信息

【讨论】:

  • 您好,我已经尝试了您的解决方案,但它仍然没有工作,错误是这个 org.postgresql.util.PSQLException: ERROR: column broken0_.finance_finance_id 不存在
  • @CedrixCedrix 在使用上述方法之前,您是否删除了自定义查询?
  • 是的,我有@Patel Romil
  • 你能分享给定表的更新代码和数据库架构吗
  • 感谢 Alex 的评论,它已经解决了,这是一个映射错误,我认为您不需要在 OneToMany 关系上使用 mappedBy,非常感谢
猜你喜欢
  • 1970-01-01
  • 2014-02-11
  • 2021-03-02
  • 2019-10-26
  • 2018-09-23
  • 2021-01-18
  • 2016-01-27
  • 2015-08-04
  • 2021-06-13
相关资源
最近更新 更多