【问题标题】:Hibernate combine multiple queriesHibernate 组合多个查询
【发布时间】:2018-11-21 13:20:56
【问题描述】:

假设我有以下数据库结构:

红色表示表格,黑色表示字段

结构使用 StructureLocationType

链接到 LocationType

现在我需要获取属于 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;

我使用休眠检索数据的辅助方法:

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;
}

所以它看起来已经无效了,但假设有 3 个 Structures 与 LocationType 链接,则需要大约 1200 毫秒才能获取 Structures 列表。我正在使用它进行自动化测试,所以理论上它确实需要光速,但我相信我需要改进它,如果有人可以帮助我改进我的代码以使用单个查询执行它,我将不胜感激? (现在显然它会向数据库发送多个查询)

谢谢。

【问题讨论】:

  • “红色,你可以看到表格,黑色的字段” - 所以LocationType是一个字段? ;)
  • 对不起,LocationType也是表格,locationTypeId是字段,会修复图像。
  • 我建议您传递多个 id 以一次加载一堆实体,并添加一些获取条件以在同一查询中获取关联的实体。但是,如果您将实体作为批次加载(即 3 个结构,然后是它们的所有位置类型等),那么当从许多单个查询变为 3 个时(或者如果结果太大,可能会更多),您应该已经获得了巨大的加速并且您遇到了内存问题)。如果数据是只读的,您可能会进一步考虑仅对所有读取使用一个事务或完全放弃 JPA 事务。

标签: java database performance hibernate automated-tests


【解决方案1】:

找到了一个解决方案,其实很简单(不知道)

使用 JOIN 关键字并执行单个原生查询:

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();

在这种情况下,“1080”是我的 LocationType id。

【讨论】:

    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多