jpql(JPA)中的父和子数据重复

我有一个Production类和ProductionDetail实体类,其中Production表的ID是一个外键,作为ProductionDetail实体类中的production_id,所以我两个带映射的实体类都给定了波纹

生产实体类:

@Entity
@Table(name = "tbl_production")
@XmlRootElement

public class TblProduction implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Size(min = 1,max = 45)
    @Column(name = "ID")
    private String id;

    @Column(name = "PRODUCTION_DATE")
    @Temporal(TemporalType.DATE)
    private Date productionDate;

    @Column(name = "START_DATETIME")
    @Temporal(TemporalType.TIMESTAMP)
    private Date startDatetime;

    @Column(name = "END_DATETIME")
    @Temporal(TemporalType.TIMESTAMP)
    private Date endDatetime;

    @Size(max = 45)
    @Column(name = "MACHINE_UUID")
    private String machineUuid;

    **Relation with Production Details Table**
    @OneToMany(mappedBy = "production")
    @XmlElement(name = "productionDetails")
    private List<TblProductionDetail> productionDetailList;


    @PrimaryKeyJoinColumn(name = "MACHINE_UUID",referencedColumnName = "UUID")
    @ManyToOne(fetch = FetchType.LAZY)
    private MstMachine mstMachine;

    @XmlTransient
    public MstMachine getMstMachine() {
       return this.mstMachine;
   }
} 

生产详细信息实体类:

    @Entity
    @Table(name = "tbl_production_detail")
    @XmlRootElement
    public class TblProductionDetail implements Serializable {

        private static final long serialVersionUID = 1L;
        @Id
        @Basic(optional = false)
        @NotNull
        @Size(min = 1,max = 45)
        @Column(name = "ID")
        private String id;
        @Size(max = 45)
        @Column(name = "COMPONENT_ID")
        private String componentId;
        @Size(max = 45)
        @Column(name = "PRODUCTION_ID")
        private String productionId;

        **Relation with Production Class** 
        @ManyToOne
        @JoinColumn(name = "PRODUCTION_ID",referencedColumnName = "ID",insertable = false,updatable = false)
        private TblProduction production;

        @Transient
        public String componentCode;
        @Transient
        public String componentName;


        @PrimaryKeyJoinColumn(name = "COMPONENT_ID",referencedColumnName = "ID")  
        @ManyToOne(fetch = FetchType.LAZY)
        private MstComponent mstComponent;

        @XmlTransient
        public MstComponent getMstComponent() {
            return this.mstComponent;
        }
        public void setMstComponent(MstComponent mstComponent) {
            this.mstComponent = mstComponent;
        }
    }

ParentList类别:

  public class TblProductionList {
      private List<TblProduction> productionList;   
      public TblProductionList() {
            productionList = new ArrayList<>();
      }
      public List<TblProduction> getTblProductions() {
            return productionList;
      }
      public void setTblProductions(List<TblProduction> tblProductionList) {
            this.productionList = tblProductionList;
      }
  }

BusinessLogic(DAO类):

       public TblProductionList getJson() {
            TblProductionList response = new TblProductionList();
            StringBuilder retrieveQuery = new StringBuilder();

            retrieveQuery.append(" SELECT prod FROM TblProduction prod ");
            retrieveQuery.append(" JOIN FETCH prod.productionDetailList ");
            retrieveQuery.append(" WHERE prod.endDatetime IS NULL ");
            retrieveQuery.append(" AND prod.machineUuid IS NOT NULL ");
            retrieveQuery.append(" AND NOT EXISTS (SELECT tpt FROM 
            TblProductionThset tpt WHERE prod.id = tpt.productionId) ");
            retrieveQuery.append(" AND EXISTS (SELECT mmfd FROM 
            MstMachineFileDef mmfd WHERE prod.machineUuid = mmfd.machineUuid 
            AND mmfd.hasThreshold = 1) ");
            retrieveQuery.append(" ORDER BY prod.id ");
            Query query = 
            entityManager.createQuery(retrieveQuery.toString());
            List thresholdList = query.getResultList();       
            response.setTblProductions(thresholdList);

            return response;
        }

根据数据库,我正在获得如下所示的主子数据

jpql(JPA)中的父和子数据重复

设计完这个实体类后,我希望我会得到3条主记录,其中每条记录有2条详细记录。但是我得到了6个重复的主记录和12个子记录。谁能向我建议我的代码哪里出错了,为什么会出现这种情况?请检查我从API获取的JSON数据。

jpql(JPA)中的父和子数据重复

ahu3129 回答:jpql(JPA)中的父和子数据重复

  
    

将数组列表更改为哈希集,然后记录就不会重复。

  
本文链接:https://www.f2er.com/3146332.html

大家都在问