1 Reply Latest reply: Aug 13, 2013 2:39 PM by Cdelahun-Oracle RSS

    Convert HashMap to HashSet for @OneToMany for JPA Hibernate Implementation

    800839

      Hi,

       

      I have a HashMap with @OneToMany relationship as shown below:-


      @Entity
      Class CustomerDetails {

      ....

        @OneToMany(fetch = FetchType.EAGER)
        @JoinTable(name="Cust_Order")
        @MapKeyColumn(name="orders_number")
        
         public Map<String,Order> getOrders() { return orders; }
         public void setOrders(Map<String,Order> orders) { this.orders = orders; }
         private Map<String,Order> orders;
        
         // getters & setters for orders
        
      }


      This class does have "N" number of @OneToMany relationships which is represented as "HashMap" and I want all of them to be eagerly loaded.Where it throws HibernateException "Cannot simultaneously fetch Multiple Bags" this is due to multiple collections in CustomerDetails
      with fetchType as "EAGER". Now I want to convert my collection type from HashMap to HashSet so that I can resolve this problem? Please let me know how I can convert HashMap to HashSet as HashMap takes key,value pairs unlike HashSet?

       

      private Map<String,Order> orders;

      to

      private Set<Order> orders;

       

      Please clarify the above & is there any better way to do it?

       

      Thanks.

        • 1. Re: Convert HashMap to HashSet for @OneToMany for JPA Hibernate Implementation
          Cdelahun-Oracle

          Hello,

           

          I'm not sure exactly what you mean, but there is no problem using eager or lazy access types with either HashMap or HashSets in EclipseLink/TopLink.  As a HashSet is a Set implementation, it is nothing like a Map and cannot be used as a HashMap replacement that I'm aware of.   The code you posted will work with EclipseLink without exceptions being thrown.

           

          If you have a Hibernate specific problem, you might try posting on that Hibernate's forum for an answer.