Pergunta

I am using SQLite3 and Hibernate with Java. Can I somehow load the whole database in memory or fetch all data from the database so that I can access mapped objects fastest? For example if we have a Company and Employee classes and in Company we've mapped the Employees as company.getEmployees(). I would like to fetch all companies from the database and later when I call this method I'd like to get the employees immediately. So is there a way to preload them? Further, if employee is mapped to other objects, can I preload them too? To summarize, I'd like to load the whole database and use the ORM to access the data. Thanks!

Foi útil?

Solução

If your goal is simply blazing performance you have several options:

  • Configure Hibernate 2nd-level cache
  • In addition to prior point, configure query cache if your data doens't change much.
  • Instead of SQLite use HSQLDB or H2 in-memory SQL databases in embedded mode
  • Use ObjectDB with a large shared cache. JPA compliant OODB.
  • Use MongoDB. A very fast in-memory NoSQL solution

I suggest ObjectDB if you aren't required to use SQL.

(EDITS: Removed eager fetching, added query cache.)

Outras dicas

Set all relationships in your entity classes to eager. If you are using JPA annotations:

public class Company{

private List<Employee> employeeList;

@ManyToMany(fetch = FetchType.EAGER)
public List<Employee> getEmployeeList() {
        return employeeList;
    }
}

This will fetch all the employees automatically when you retrieve a company from database. Please note this could affect the performance dramatically.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top