> For the complete documentation index, see [llms.txt](https://query.docs.doyto.win/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://query.docs.doyto.win/object-concepts/view-object.md).

# View Object

Complex queries often involve joins across multiple tables and aggregation operations on columns. While query objects can be reused to construct the `WHERE` clause, entity objects cannot fully express the other parts of a query. Therefore, we introduce a dedicated **view object** to define the static parts of a complex query, along with a separate **Having object** to generate the `HAVING` clause. Additionally, the view object serves as the target for result mapping.

To achieve automatic mapping for complex queries, we divide the mapping process into the following three parts:

1. **Column Mapping**: The fields defined in the view object are used to map the required columns in the query results, including both regular columns and aggregated columns.
2. **Join Mapping**: Annotations within the view object define the relationships between tables, supporting the automatic generation of necessary join statements.
3. **HAVING Clause Mapping**: The Having object is used to generate the `HAVING` clause, supporting filtering conditions based on aggregated results.

This design not only improves developers' efficiency in constructing and maintaining complex query statements but also reduces the risks associated with manually assembling SQL. It significantly enhances the readability, reusability, and maintainability of the code.

## Aggregate Query Interface

In addition to designing interfaces for single-table data access, a separate interface is required for aggregate queries.

The interface `AggregateClient` has been designed and implemented, defined as follows: This interface provides a general-purpose method `query`, allowing developers to perform aggregate queries by specifying the target view class and a query object, with the results mapped to a list of the corresponding view objects.

```java
public interface AggregateClient {
    <V> AggregateChain<V> aggregate(Class<V> viewClass);

    default <V> List<V> query(Class<V> viewClass, DoytoQuery query) {
        return aggregate(viewClass).filter(query).query();
    }

    default <V> long count(Class<V> viewClass, DoytoQuery query) {
        return aggregate(viewClass).filter(query).count();
    }

    default <V> PageList<V> page(Class<V> viewClass, DoytoQuery query) {
        return aggregate(viewClass).filter(query).page();
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://query.docs.doyto.win/object-concepts/view-object.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
