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

标签 java jpa jax-rs jpql

我有一个 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;
        }
    }

父列表类:

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

业务逻辑(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;
        }

根据数据库,我得到了预期的主子数据,如下所示 enter image description here

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

enter image description here

最佳答案

change your array list to hash set then records are not duplicate.

关于java - jpql (JPA) 中重复的父子数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58745154/

相关文章:

java - 使用 PDFbox 从 PDF 文件中删除图像

java - 编写一个程序来搜索数组以找到第一个奇数

java - jetty 和 Jersey : How does everything fits together?

java - 执行一对一映射时,Hibernate 不会填充第二个表

JAX-RS - 找不到类型为响应对象的 MessageBodyWriter

file-upload - 如何在 WildFly 中设置 Undertow MAX_ENTITY_SIZE

java - 实现不同步读取的双缓冲java HashMap

java - 了解 JPA 延迟加载

java - 如何在 AppEngine 中使用 JPA 2 获取实体、在 GWT 中更新并保存?

java - asm 3.1 和 org.eclipse.persistence.asm-2.3.2.jar (jersey-moxy 1.15) 的兼容性问题