Menu

Quick advice: NOT to use toSting

2016-05-01
2016-05-05
  • Granjero Moe

    Granjero Moe - 2016-05-01

    I just want to give this quick advice: don't use the toSting method in Entities.

    I've just found the cause of a problem saving an entity - it took a lot of time (minutes) and it was due to the entity, let's say Product, had a collection of Catalogs and a Catalog had a collection of Prices.
    Each Catalog have thousands of prices.

    When you save a Product, openxava calls internally a method called convertSubmapsInObject which is called to convert the Catalog in an object and it works fine but I had a toString in Catalog to say the number of prices the catalog had and this toString was called at some point causing hibernate to read thousands of prices.

    I've removed the toString and everything is much faster.

     
  • Federico Alcantara

    Hi Moe:

    It depends. You can always override the default toString() method in the entity, in fact it is a java recommendation. Hibernate (which is a JPA implementation) recommends the hashCode() and equals() (see this discussion) so in the case of entities the toString(), hashCode() and equals() should be implemented to avoid the behavior you've referred.

    There is a catch, instead of accessing the members, you should use the getter's in order to avoid null pointer because of lazy initialization.

     

Log in to post a comment.