lets say I have following database structure:
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.

LocationTypeis a field? ;)