1

I need to display a large table with about 1300 roles at one time. (I know I should use a data scroll but my users want to see the whole table at one time.) The table displays 4 columns. Two of those columns are from the object but the other two are from referenced objects in the original object. I need to find the best/efficient way to do this. I currently have this working but when I reload the table it give an out of memory error. I think that it's caused by the large amount of redundant data in memory.

  1. Create a view object that the repository will fill in only the needed fields.
  2. Any other suggestions.

Here are the objects:

    public class Database extends EntityObject {
      private Long id;
      private String name;
      private String connectionString;
      private String username; 
      private String password;
      private String description;

      // getter and setters omitted
    }

    public class Application extends EntityObject {
      private Long id;
      private String name;
      private String fullName = "";
      private String description;
      private Database database;
      private List<Role> roles = new ArrayList<Role>(0);

      // getter and setters omitted
    }

    public class Role extends EntityObject {
      private Long id;
      private String name;
      private String nameOnDatabase;
      private Application application;

      // getter and setters omitted
    }

What I need displayed from the list of Roles is:

role.id, role.name, role.application.name, role.application.database.name

Miller
  • 481
  • 1
  • 6
  • 18

1 Answers1

1

To optimize wisely, define what are you going to do with data, view or/and edit. Here are some common scenarios:

  1. Retrieval using lazy fetch type. Mark your roles in application with FetchType.LAZY annotation.

  2. Retrieval using multiselect query. Create your custom class (like DTO) and populate it with data from the database using multiselect query. (Similar to VIEW mapped as Entity)

There are also other possibilities, such as Shared (L2) Entity Cache or Retrieval by Refresh.

See if you are using EntityManager correctly reading Am I supposed to call EntityManager.clear() often to avoid memory leaks?.

Community
  • 1
  • 1
d1e
  • 6,372
  • 2
  • 28
  • 41
  • Why would the OP want lazy loading? The OP's loading `Role`, `Application` and `Database`. – siebz0r Jun 12 '12 at 08:46
  • Retrieved Role will load Application, which will load List eagerly. Since you are retrieving all roles from database, I am not sure if hibernate will automatically look for already retrieved roles in its persistence context. – d1e Jun 12 '12 at 09:03
  • collections are fetched lazy by default to my knowledge. – siebz0r Jun 12 '12 at 09:12
  • @siebz0r yep, sorry, my bad. Then other options are still out there. – d1e Jun 12 '12 at 09:26