0

lets say I have following database structure:

enter image description here

In red, you can see TABLES, and in black FIELDS

Structure(s) are linked to LocationType using StructureLocationType

Now I need to get a list of Structure(s) that belong to LocationType:

    // get LocationType
    LocationTypeEntity locationTypeEntity = databaseManager.selectLocationType(session, locationTypeID);

    // get list of StructureLocationType(s)
    List<StructureLocationTypeEntity> structureLocationTypeEntities = databaseManager.selectStructureLocationTypes(session, locationTypeID);

    // get list of Structures(s)
    List<StructureEntity> structures = new ArrayList<>();
    for (StructureLocationTypeEntity structure: structureLocationTypeEntities)
    {
        structures.add(databaseManager.selectStructure(session, structure.getStructureId()));
    }
    return structures;

My helper methods to retrieve data using hibernate:

public LocationTypeEntity selectLocationType(Session session, int id)
{
    session.beginTransaction();
    LocationTypeEntity locationTypeEntity = session.get(LocationTypeEntity.class, id);
    session.getTransaction().commit();
    return locationTypeEntity;
}

public List<StructureLocationTypeEntity> selectStructureLocationTypes(Session session, int locationTypeId)
{
    session.beginTransaction();
    CriteriaBuilder builder = session.getCriteriaBuilder();
    CriteriaQuery<StructureLocationTypeEntity> query = builder.createQuery(StructureLocationTypeEntity.class);
    Root<StructureLocationTypeEntity> root = query.from(StructureLocationTypeEntity.class);
    query.select(root).where(builder.equal(root.get("locationTypeId"), locationTypeId));
    Query<StructureLocationTypeEntity> q = session.createQuery(query);
    List<StructureLocationTypeEntity> locationTypeEntities = q.getResultList();
    session.getTransaction().commit();
    return locationTypeEntities;
}

public StructureEntity selectStructure(Session session, int structureID)
{
    session.beginTransaction();
    StructureEntity structure = session.get(StructureEntity.class, structureID);
    session.getTransaction().commit();
    return structure;
}

So it already seems ineffective, but assuming there were 3 Structures linked with LocationType, it takes ~1200ms to get list of Structures. I am using it for automation testing, so in theory it does need to be speed of light, but I believe I need to improve it, would be grateful if someone can help me to improve my code to maybe execute it with single query ? (now obviously it sends multiple queries to the database)

Thank you.

3
  • "In red, you can see TABLES, and in black FIELDS" - so LocationType is a field? ;) Commented Nov 21, 2018 at 13:23
  • Sorry my mistake, LocationType is table as well, and locationTypeId is field, will fix the image. Commented Nov 21, 2018 at 13:24
  • 1
    I'd suggest you pass multiple ids to load a bunch of entities at once and add some fetch criteria to fetch the associated entities in the same query. However, if you load the entities as batches (i.e. 3 structures and then all their location types etc.) then you should already get a tremendous speedup when going from many single queries to just 3 (or maybe some more if the results get too big and you're running into memory problems). You might further consider using only one transaction for all reads or ditch JPA transactions completely if the data is meant to be readonly. Commented Nov 21, 2018 at 13:25

1 Answer 1

0

Found a solution, quite simple in fact (was not aware of it)

using JOIN keyword and executing single native query:

Query q = session.createNativeQuery("Select *, StatusId \n" +
        "FROM dbo.Structure AS S\n" +
        "JOIN dbo.StructureLocationType AS SLT ON SLT.StructureId = S.Id\n" +
        "WHERE SLT.LocationTypeId = 1080").addEntity(StructureEntity.class);

List<StructureEntity> zones2 = q.list();

Where in this case '1080' was my LocationType id.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.