一对多关系始终为空

时间:2016-08-02 09:34:42

标签: java jpa openjpa

给出以下SQL模型:

Two Views
   VIEW PRODUCT
       ID_PROPOSAL
       ID_PRODUCT

   VIEW PERSON
       ID_ENTITY
       ID_PRODUCT
       NAME

JPA
@NamedQueries({
        @NamedQuery(name = "Product.getByProposalId",
                query = "SELECT ie "
                        + "FROM Product ie "
                        + "WHERE ie.idProposal = :proposalId ")
}) 
 @Entity
 Table(name = "PRODUCT")
 public classe Product{

    @Id
    @Column(name = "ID_PRODUCT")
    private Long idProduct;

    @Column(name = "ID_PROPOSAL")
    private Long idProposal;

     @OneToMany(mappedBy="product")
     List<Person> persons;

      public List<Person> getPersons() {
        return persons;
      }

      public void setPersons(List<Person> persons) {
        this.persons= persons;
      }

}

 @Entity
 Table(name = "PERSON")
 public classe Person{

    @Id
    @Column(name = "ID_ENTITY")
    private Long idEntity;

    @Column(name = "ID_PRODUCT")
    private Long idProduct;

     @ManyToOne
     @JoinColumn(name = "ID_PRODUCT", insertable = false, updatable = false)
     Product product;

      public List<Person> getPersons() {
        return persons;
      }

      public void setPersons(List<Person> persons) {
        this.persons= persons;
      }

}


 Query query = emanager.createNamedQuery("Product.getByProposalId");
        query.setParameter("propostaId", proposalId);
        return query.getResultList();

它返回正确的产品结果但是对于每个产品都不返回任何人,在日志文件中我看不到查看人员的任何查询。 应用程序服务器是websphere,jpa实现是openjpa1.2.3

有什么想法吗? 提前致谢 最好的问候

1 个答案:

答案 0 :(得分:0)

由于您已确认问题出在我已评论的Lazy loading,我想将其添加为答案。因此,这将有助于将来担任这篇文章的人。

您必须使用lazy loadingonetomany中的false配置为FetchType.EAGER,如下所示。

@OneToMany(fetch=FetchType.EAGER)

如果您没有在默认情况下指定此项,则延迟加载将为true