0

Hi I am struggling to find what is wrong in my code I have searched and tried to find the answers to it, I also checked my database to see if my naming conventions matched the one I have in my models and also double checked if the @Column in my BreakDown model matched the @JoinColumn in my finance model and it matches. I would appreciate it if someone could help me here is the code

Repository

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

Finance model

@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

BreakDown model

@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

Error stack trace

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.

Thank you in advance all answers are appreciated

Cedrix Cedrix
  • 83
  • 1
  • 12

1 Answers1

1

Using JPA

List<BreakDown> findAllByFinanceFinanceId(Long financeId);

In the case of native query you have to use the JOIN ON break_down and finance table


UPDATE:

You should get the Finance details using the get method inside the BreakDown class. I have added the example for the same.

@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;
    }

}

Query inside BRepositoty:

List<B> findAllByAId(Long id);

You can get the details of A using b.getA()

Romil Patel
  • 12,879
  • 7
  • 47
  • 76