1 Reply Latest reply on Aug 13, 2013 7:39 PM by Cdelahun-Oracle

    Convert HashMap to HashSet for @OneToMany for JPA Hibernate Implementation




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

      Class CustomerDetails {


        @OneToMany(fetch = FetchType.EAGER)
         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;


      private Set<Order> orders;


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



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



          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.