使用JPQL JpaRepository从两个表父表Id一对多关系中检索数据

如何根据父表id从一对多关系的两个表中检索数据。我必须实体贷记和借记。贷方是父实体,借方是子实体。在贷方 cid camount coping cbusiness ctotal 1 50 60 90 200 2 10 20 30 60 3 5 4 3 12 在借方 did damount ddescription 1 100 market 2 200invest 3 20 Nothing 4 500 标记在第三个表中,我在那里存储了该表的参考 ID上面两个表使用一对多关系下面的表是自动创建的credit_debit c_cid d_did 1 1 1 2 2 3 2 4 当我将cid传递给JPQL查询时如何通过id检索数据


我尝试编写 JPQL 查询,但它们会显示错误。就像这个查询 @Query(value= "select c,d from Credit as c, Debit as d where c.cid=c.cid")


1.Credit.java


package com.rajesh.model;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Date;

import java.util.List;

import javax.persistence.CascadeType;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.JoinColumn;

import javax.persistence.JoinTable;

import javax.persistence.OneToMany;

import javax.persistence.Table;

import org.springframework.core.annotation.Order;

import org.springframework.format.annotation.DateTimeFormat;


@Entity

@Table(name="credit")

public class Credit extends BaseEntity{

    @Id

    @GeneratedValue(strategy=GenerationType.AUTO)

    @Column

    private long cid;

    @Column @Order

    private long openingbalance;

    @Column

    @DateTimeFormat(pattern = "yyyy-MM-dd")

    private Date date;

    @Column @Order

    private long debittotal;

    @Column @Order

    private long drawertotal;

    @Column @Order

    private long debittotalplusdrawertotal;

    @Column @Order

    private long todaybusiness;


    @OneToMany(cascade={CascadeType.ALL})

    @JoinTable(name="credit_debit", 

               joinColumns=@JoinColumn(name="c_id"), 

               inverseJoinColumns=@JoinColumn(name="d_id"))

    private List<Debit> debits = new ArrayList<Debit>(Arrays.asList());

2.Debit.java


package com.rajesh.model;

import javax.persistence.Column;

import javax.persistence.Entity;

import javax.persistence.GeneratedValue;

import javax.persistence.GenerationType;

import javax.persistence.Id;

import javax.persistence.Table;




森栏
浏览 133回答 0
0回答

慕工程0101907

我必须按照 JB Nizet 给出的解决方案尝试它已解决
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java