簡體   English   中英

如何使用一對多關系休眠查詢從數據庫中獲取所有數據

[英]how to fetch all data from database using one to many relation hibernate query

我有兩個表父表是貸方,該表中只有一行數據,另一表是子表借方,其中包含多行數據。 如何從兩個表中獲取數據,該表必須與父類和子類的ID相匹配,並且父類沒有顯示重復項。

我嘗試過(來自Credit,debit),但是可以重復顯示,並且根據ID無法正確顯示數據。

package com.rojmat.entity;
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;

@Entity
@Table(name="credit")
public class Credit extends BaseEntity{
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column
    private long cid;
    @Column @Order
    private long openingbalance;
    @Column
    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"))
    /*@JoinColumn(name="cid", referencedColumnName="cid")*/
    private List<Debit> debits = new ArrayList<Debit>(Arrays.asList());
    public Credit() {

    }
    public Credit(long cid, long openingbalance, Date date, long debittotal, long drawertotal,
            long debittotalplusdrawertotal, long todaybusiness, List<Debit> debits) {
        super();
        this.cid = cid;
        this.openingbalance = openingbalance;
        this.date = date;
        this.debittotal = debittotal;
        this.drawertotal = drawertotal;
        this.debittotalplusdrawertotal = debittotalplusdrawertotal;
        this.todaybusiness = todaybusiness;
        this.debits = debits;
    }
    public long getCid() {
        return cid;
    }
    public void setCid(long cid) {
        this.cid = cid;
    }
    public long getOpeningbalance() {
        return openingbalance;
    }
    public void setOpeningbalance(long openingbalance) {
        this.openingbalance = openingbalance;
    }   
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    public long getDebittotal() {
        return debittotal;
    }
    public void setDebittotal(long debittotal) {
        this.debittotal = debittotal;
    }
    public long getDrawertotal() {
        return drawertotal;
    }
    public void setDrawertotal(long drawertotal) {
        this.drawertotal = drawertotal;
    }
    public long getDebittotalplusdrawertotal() {
        return debittotalplusdrawertotal;
    }
    public void setDebittotalplusdrawertotal(long debittotalplusdrawertotal) {
        this.debittotalplusdrawertotal = debittotalplusdrawertotal;
    }
    public long getTodaybusiness() {
        return todaybusiness;
    }
    public void setTodaybusiness(long todaybusiness) {
        this.todaybusiness = todaybusiness;
    }
    public List<Debit> getDebit() { 
        return debits;
    }   
    public void setDebit(List<Debit> debit) {   
        this.debits = debits;
    }
    /*@Override
    public String toString() {
            return "Credit [cid=" + cid + ", openingbalance =" + openingbalance + ", date=" + date + ", debittotal= " + debittotal + ", debittotalplusdrawertotal=" + debittotalplusdrawertotal + ", todaybusiness=" + todaybusiness + "]";
    }*/
}
package com.rojmat.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="debit")
public class Debit {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column
    private long did;
    @Column
    private String amount;
    @Column
    private String description; 

    public Debit() {

    }
    public Debit(String amount, String description) {
        super();
        this.amount = amount;
        this.description = description;
    }
    public long getDid() {
        return did;
    }
    public void setDid(long did) {
        this.did = did;
    }
    public String getAmount() {
        return amount;
    }
    public void setAmount(String amount) {
        this.amount = amount;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    @Override
    public String toString() {
            return "Debit [did=" + did + ", amount =" + amount + ", description=" + description + "]";
    }
}

1.CreditDaoImpl.java

package com.rojmat.daoImpl;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.rojmat.dao.CreditDao;
import com.rojmat.entity.Credit;

@Repository
public class CreditDaoImpl implements CreditDao{

    @Autowired
    private SessionFactory sessionFactory;
    @Override
    public void addCreditDebit(Credit credit) {
        try {
            sessionFactory.getCurrentSession().saveOrUpdate(credit);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void deleteCreditDebit(int cid) {
        /*Credit credit = (Credit)sessionFactory.getCurrentSession().createQuery("from Credit as c LEFT JOIN FETCH c.Debit where c.cid="+cid).uniqueResult();
        List<Debit> debits = credit.getDebit();
        sessionFactory.getCurrentSession().delete(credit);
        debits.forEach((debit) -> {
            sessionFactory.getCurrentSession().delete(debit);
        });*/
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Credit> getAllCreditDebit() {
        List<Credit> credit = sessionFactory.getCurrentSession().createQuery("from Credit,Debit").list();
        return credit;
    }
}

請嘗試以下示例:您將“ distinct”放在您不想復制的屬性之前

//SQL query

select distinct credit.idCredit as idCredit from Credit credit Left Join Debit debit on credit.idCredit= debit.idCredit

//HQL query 

@Entity(name = "Credit")
@Table(name = "Credit")
public class Credit{


    //if you put @Id  --> HQL Query "select credit from Credit credit"
    @Column(name = "idCredit")
    private Long idCredit;

    @Column(name = "label")
    private String label;

    @OneToMany 
    @JoinColumns({@JoinColumn(name = "idCredit" ,referencedColumnName = "idCredit")})
    List<Debit> debits;

  ...
}
public class Debit{
    ....


    @Column(name = "idCredit")
    private Long idCredit;
    ...
}

    Query query = getSession().createQuery("select distinct credit.idCredit as idCredit, credit.label as label, credit.debits as debits from Credit credit   ");
    query.setResultTransformer(Transformers.aliasToBean(Credit.class));
    return query.list();
package com.rojmat.entity;
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;

@Entity
@Table(name="credit")
public class Credit extends BaseEntity{
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column
    private long cid;
    @Column @Order
    private long openingbalance;
    @Column
    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"))
    /*@JoinColumn(name="cid", referencedColumnName="cid")*/
    private List<Debit> debits = new ArrayList<Debit>(Arrays.asList());
    public Credit() {

    }
    public Credit(long cid, long openingbalance, Date date, long debittotal, long drawertotal,
            long debittotalplusdrawertotal, long todaybusiness, List<Debit> debits) {
        super();
        this.cid = cid;
        this.openingbalance = openingbalance;
        this.date = date;
        this.debittotal = debittotal;
        this.drawertotal = drawertotal;
        this.debittotalplusdrawertotal = debittotalplusdrawertotal;
        this.todaybusiness = todaybusiness;
        this.debits = debits;
    }
    public long getCid() {
        return cid;
    }
    public void setCid(long cid) {
        this.cid = cid;
    }
    public long getOpeningbalance() {
        return openingbalance;
    }
    public void setOpeningbalance(long openingbalance) {
        this.openingbalance = openingbalance;
    }   
    public Date getDate() {
        return date;
    }
    public void setDate(Date date) {
        this.date = date;
    }
    public long getDebittotal() {
        return debittotal;
    }
    public void setDebittotal(long debittotal) {
        this.debittotal = debittotal;
    }
    public long getDrawertotal() {
        return drawertotal;
    }
    public void setDrawertotal(long drawertotal) {
        this.drawertotal = drawertotal;
    }
    public long getDebittotalplusdrawertotal() {
        return debittotalplusdrawertotal;
    }
    public void setDebittotalplusdrawertotal(long debittotalplusdrawertotal) {
        this.debittotalplusdrawertotal = debittotalplusdrawertotal;
    }
    public long getTodaybusiness() {
        return todaybusiness;
    }
    public void setTodaybusiness(long todaybusiness) {
        this.todaybusiness = todaybusiness;
    }
    public List<Debit> getDebit() { 
        return debits;
    }   
    public void setDebit(List<Debit> debit) {   
        this.debits = debits;
    }
    /*@Override
    public String toString() {
            return "Credit [cid=" + cid + ", openingbalance =" + openingbalance + ", date=" + date + ", debittotal= " + debittotal + ", debittotalplusdrawertotal=" + debittotalplusdrawertotal + ", todaybusiness=" + todaybusiness + "]";
    }*/
}

package com.rojmat.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="debit")
public class Debit {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column
    private long did;
    @Column
    private String amount;
    @Column
    private String description; 

    public Debit() {

    }
    public Debit(String amount, String description) {
        super();
        this.amount = amount;
        this.description = description;
    }
    public long getDid() {
        return did;
    }
    public void setDid(long did) {
        this.did = did;
    }
    public String getAmount() {
        return amount;
    }
    public void setAmount(String amount) {
        this.amount = amount;
    }
    public String getDescription() {
        return description;
    }
    public void setDescription(String description) {
        this.description = description;
    }
    /*@Override
    public String toString() {
            return "Debit [did=" + did + ", amount =" + amount + ", description=" + description + "]";
    }*/
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM