Convert Figma logo to code with AI

querydsl logoquerydsl

Unified Queries for Java

4,836
880
4,836
107

Top Related Projects

3,130

requery - modern SQL based query & persistence for Java / Kotlin / Android

6,370

jOOQ is the best way to write SQL in Java

Simplifies the development of creating a JPA-based data access layer.

Hibernate's core Object/Relational Mapping functionality

Quick Overview

Querydsl is a Java framework that enables the construction of type-safe SQL-like queries for multiple backends including JPA, MongoDB, and SQL. It provides a fluent API for building queries, offering better compile-time safety and refactoring support compared to writing queries as strings.

Pros

  • Type-safe query construction, reducing runtime errors
  • Supports multiple backends (JPA, MongoDB, SQL, etc.)
  • Improves code maintainability and refactoring
  • Integrates well with popular Java frameworks like Spring

Cons

  • Steeper learning curve compared to writing native queries
  • May introduce additional complexity for simple queries
  • Potential performance overhead in some scenarios
  • Limited support for very complex or database-specific operations

Code Examples

  1. Simple JPA query:
QCustomer customer = QCustomer.customer;
List<Customer> results = queryFactory.selectFrom(customer)
    .where(customer.lastName.eq("Smith"))
    .orderBy(customer.firstName.asc())
    .fetch();
  1. MongoDB query with projections:
QCustomer customer = QCustomer.customer;
List<String> names = queryFactory.select(customer.firstName)
    .from(customer)
    .where(customer.age.gt(18))
    .orderBy(customer.lastName.asc())
    .fetch();
  1. SQL query with joins:
QCustomer customer = QCustomer.customer;
QOrder order = QOrder.order;
List<Tuple> results = queryFactory.select(customer.firstName, order.totalAmount)
    .from(customer)
    .leftJoin(customer.orders, order)
    .where(order.status.eq(OrderStatus.COMPLETED))
    .fetch();

Getting Started

To use Querydsl with JPA, add the following dependencies to your Maven pom.xml:

<dependencies>
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-apt</artifactId>
        <version>5.0.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
        <version>5.0.0</version>
    </dependency>
</dependencies>

Then, configure the APT plugin to generate Querydsl classes:

<plugin>
    <groupId>com.mysema.maven</groupId>
    <artifactId>apt-maven-plugin</artifactId>
    <version>1.1.3</version>
    <executions>
        <execution>
            <goals>
                <goal>process</goal>
            </goals>
            <configuration>
                <outputDirectory>target/generated-sources/java</outputDirectory>
                <processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
            </configuration>
        </execution>
    </executions>
</plugin>

After building your project, you can start using Querydsl with your JPA entities.

Competitor Comparisons

3,130

requery - modern SQL based query & persistence for Java / Kotlin / Android

Pros of requery

  • Lightweight and efficient, with a smaller footprint than QueryDSL
  • Built-in support for RxJava and Android
  • Simpler API design, making it easier to learn and use

Cons of requery

  • Less mature and less widely adopted compared to QueryDSL
  • Fewer advanced features and customization options
  • Limited support for non-relational databases

Code Comparison

QueryDSL:

QCustomer customer = QCustomer.customer;
List<Customer> results = queryFactory.selectFrom(customer)
    .where(customer.lastName.eq("Smith"))
    .orderBy(customer.firstName.asc())
    .fetch();

requery:

List<Customer> results = data
    .select(Customer.class)
    .where(Customer.LAST_NAME.eq("Smith"))
    .orderBy(Customer.FIRST_NAME.asc())
    .get();

Both QueryDSL and requery provide type-safe query construction, but requery's syntax is slightly more concise. QueryDSL offers more flexibility and advanced features, while requery focuses on simplicity and ease of use. The choice between the two depends on project requirements, existing infrastructure, and developer preferences.

6,370

jOOQ is the best way to write SQL in Java

Pros of jOOQ

  • More comprehensive SQL support, including advanced features and database-specific functions
  • Better type safety and code completion due to code generation from database schema
  • Extensive documentation and commercial support options

Cons of jOOQ

  • Steeper learning curve due to its more complex API
  • Requires code generation step, which can complicate the build process
  • Commercial licensing for some databases and advanced features

Code Comparison

jOOQ:

Result<Record3<String, String, Integer>> result = 
    create.select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME, count())
          .from(AUTHOR)
          .join(BOOK).on(AUTHOR.ID.eq(BOOK.AUTHOR_ID))
          .groupBy(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
          .fetch();

QueryDSL:

List<Tuple> result = 
    new JPAQuery<Void>(entityManager)
        .select(author.firstName, author.lastName, book.count())
        .from(author)
        .join(book).on(author.id.eq(book.authorId))
        .groupBy(author.firstName, author.lastName)
        .fetch();

Both libraries offer type-safe, fluent APIs for building SQL queries, but jOOQ provides more SQL-like syntax and features, while QueryDSL focuses on a more abstract, JPA-compatible approach.

Simplifies the development of creating a JPA-based data access layer.

Pros of Spring Data JPA

  • Seamless integration with Spring ecosystem
  • Powerful repository abstraction for common CRUD operations
  • Extensive support for custom query methods using method names

Cons of Spring Data JPA

  • Less flexible for complex queries compared to QueryDSL
  • Steeper learning curve for advanced features
  • Limited support for dynamic query construction

Code Comparison

Spring Data JPA:

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    List<User> findByLastNameAndAge(String lastName, int age);
}

QueryDSL:

QUser user = QUser.user;
List<User> users = queryFactory.selectFrom(user)
    .where(user.lastName.eq(lastName).and(user.age.eq(age)))
    .fetch();

Spring Data JPA offers a more concise approach for simple queries, while QueryDSL provides greater flexibility for complex queries. Spring Data JPA is tightly integrated with the Spring ecosystem, making it a natural choice for Spring-based applications. However, QueryDSL offers more powerful type-safe query construction capabilities, especially for dynamic queries. The choice between the two depends on the specific requirements of the project and the complexity of the queries needed.

Hibernate's core Object/Relational Mapping functionality

Pros of Hibernate ORM

  • More comprehensive ORM solution with full JPA implementation
  • Mature project with extensive documentation and community support
  • Offers advanced features like caching, lazy loading, and inheritance mapping

Cons of Hibernate ORM

  • Steeper learning curve due to its complexity and extensive feature set
  • Can be overkill for simpler projects or when only basic ORM functionality is needed
  • Performance overhead in certain scenarios, especially with large datasets

Code Comparison

Hibernate ORM:

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
}

QueryDSL:

QUser user = QUser.user;
List<User> users = queryFactory.selectFrom(user)
    .where(user.name.eq("John"))
    .fetch();

Key Differences

  • Hibernate ORM is a full-featured ORM framework, while QueryDSL focuses on type-safe SQL queries
  • QueryDSL provides a more flexible query API, making it easier to construct complex queries
  • Hibernate ORM handles entity mapping and persistence, whereas QueryDSL is primarily a query generation tool

Use Cases

  • Choose Hibernate ORM for comprehensive ORM needs in large-scale enterprise applications
  • Opt for QueryDSL when you need type-safe, flexible queries or want to enhance existing ORM solutions

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Querydsl

Querydsl is a framework which enables the construction of type-safe SQL-like queries for multiple backends including JPA, MongoDB and SQL in Java.

Instead of writing queries as inline strings or externalizing them into XML files they are constructed via a fluent API.

Website shields.io Build Status Coverage Status Stackoverflow Maven Central

Getting started

Use these tutorials to get started

Examples

Querydsl example projects

Support

Free support is provided in the Discussion Section and on StackOverflow. Please do not post questions as issue. Such issues will be closed immediately.

How to build

Querydsl provides releases via public Maven repositories, but you can also build the sources yourself like this

$ mvn -Pquickbuild,{projectname} clean install

Where projectname is one of the Maven profiles (e.g. jpa, sql, mongodb, etc. or all)

For more information visit the project homepage at https://querydsl.github.io.

Docker Compose setup

For running tests, a Docker Compose setup is provided. It comes with the following databases:

  • Oracle Express Edition 11g
  • PostgreSQL 9.1.10
  • MySQL 5.5.34
  • Cubrid 9.2

You will need to install Docker and docker-compose.

To launch the database containers:

$ docker-compose up -d

All of the databases' default ports are forwarded to the host machine.

How to contribute

GitHub pull requests are the way to contribute to Querydsl.

If you are unsure about the details of a contribution, ask on the Querydsl Google Group or create a ticket on GitHub.

Slack

If you want to join Slack workspace for Querydsl contributors join by following this link.