Search code examples
javajpaopenjpa

One to Many relations is always null


Given the following SQL model:

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();

It returns the correct product results but for each product do not return any Persons , in the log file i can not seen any query to the View Person. The application server is websphere and the jpa implementation is openjpa1.2.3

Any ideas ? Thanks in advance Best regards


Solution

  • Since you have confirmed the problem is with Lazy loading, which i already commented, i would like to add it as answer. So that this will be helpful for people who are coming to this post in future.

    You have to configure lazy loading in onetomany as false using FetchType.EAGER as shown below.

    @OneToMany(fetch=FetchType.EAGER)
    

    And if you are not specifying this by default lazy loading will be true