Partner – Microsoft – NPI EA (cat = Baeldung)
announcement - icon

Azure Container Apps is a fully managed serverless container service that enables you to build and deploy modern, cloud-native Java applications and microservices at scale. It offers a simplified developer experience while providing the flexibility and portability of containers.

Of course, Azure Container Apps has really solid support for our ecosystem, from a number of build options, managed Java components, native metrics, dynamic logger, and quite a bit more.

To learn more about Java features on Azure Container Apps, visit the documentation page.

You can also ask questions and leave feedback on the Azure Container Apps GitHub page.

Partner – Microsoft – NPI EA (cat= Spring Boot)
announcement - icon

Azure Container Apps is a fully managed serverless container service that enables you to build and deploy modern, cloud-native Java applications and microservices at scale. It offers a simplified developer experience while providing the flexibility and portability of containers.

Of course, Azure Container Apps has really solid support for our ecosystem, from a number of build options, managed Java components, native metrics, dynamic logger, and quite a bit more.

To learn more about Java features on Azure Container Apps, you can get started over on the documentation page.

And, you can also ask questions and leave feedback on the Azure Container Apps GitHub page.

Partner – Orkes – NPI EA (cat=Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag=Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – MongoDB – NPI EA (tag=MongoDB)
announcement - icon

Traditional keyword-based search methods rely on exact word matches, often leading to irrelevant results depending on the user's phrasing.

By comparison, using a vector store allows us to represent the data as vector embeddings, based on meaningful relationships. We can then compare the meaning of the user’s query to the stored content, and retrieve more relevant, context-aware results.

Explore how to build an intelligent chatbot using MongoDB Atlas, Langchain4j and Spring Boot:

>> Building an AI Chatbot in Java With Langchain4j and MongoDB Atlas

Partner – LambdaTest – NPI EA (cat=Testing)
announcement - icon

Accessibility testing is a crucial aspect to ensure that your application is usable for everyone and meets accessibility standards that are required in many countries.

By automating these tests, teams can quickly detect issues related to screen reader compatibility, keyboard navigation, color contrast, and other aspects that could pose a barrier to using the software effectively for people with disabilities.

Learn how to automate accessibility testing with Selenium and the LambdaTest cloud-based testing platform that lets developers and testers perform accessibility automation on over 3000+ real environments:

Automated Accessibility Testing With Selenium

1. Introduction

In JPACAST and TREAT are two distinct keywords used to manipulate data types and entity relationships. In this tutorial, we’ll explore the differences between CAST and TREAT and look at examples to illustrate their usage.

2. CAST in JPA

The CAST operator in JPA is primarily used for type conversions within JPQL queries. It allows us to explicitly convert a value from one data type to another. For instance, we can use CAST to convert a String to an Integer or vice versa.

Here’s the syntax for CAST:

CAST(expression AS type)

expression is the value or field that we want to convert, and type is the target data type to which we want to convert the expression.

3. TREAT in JPA

In contrast, the TREAT operator is designed for type-safe downcasting of entities within JPQL queries. It’s particularly useful when dealing with inheritance hierarchies. When we use TREAT, we specify a subtype of the entity, and JPA checks if the actual entity is indeed of that type.

Unlike CAST, TREAT doesn’t change the underlying data type of the value. Instead, it allows us to access the value as if it were of the target type.

Here’s the syntax for TREAT:

TREAT(expression AS type)

expression is the value to be treated, and type is the target data type.

4. Purpose and Usage

In JPA queries, both CAST and TREAT are used to handle type conversions, but they serve different purposes.

4.1. CAST Operator

The CAST is used to convert one data type into another for operations or comparisons. It’s often employed when performing queries that require a data type different from what’s stored in the database.

Consider an example where an entity called Employee, and the salary field is stored as a String in the database. Here’s how our Employee entity is defined:

@Entity
public class Employee {
    @Id
    private Long id;
    private String salary;
    // getters and setters
}

In this example, the salary field is of type String, but we may need to perform numeric operations or comparisons based on this field. To achieve this, we can use CAST to convert the salary field to an Integer type:

Employee emp1 = new Employee();
emp1.setId(1L);
emp1.setSalary("5000");
em.persist(emp1);

Query query = em.createQuery("SELECT CAST(e.salary AS Integer) FROM Employee e");
List<Integer> salaries = query.getResultList();

assertEquals(5000, salaries.get(0));

In this query, we use CAST to convert the salary field from a String to an Integer. The result of this query is a list of integers representing the salaries of employees.

4.2. TREAT Operator

On the other hand, TREAT is used for type-safe downcasting in inheritance. It allows us to work with entities in a way that acknowledges their actual subclass type, even when referenced through a base class.

Suppose we have an entity Vehicle with subclasses Car and Bike, and we want to retrieve only Car entities from a query that selects from Vehicle. We can use TREAT to ensure type safety:

@Entity
public class Vehicle {
    @Id
    private Long id;
    private String type;
    // getters and setters
}

@Entity
public class Car extends Vehicle {
    private Integer numberOfDoors;
    // getters and setters
}

To achieve this, we use the TREAT operator in our JPQL query to cast Vehicle instances to Car instances:

Vehicle vehicle = new Vehicle();
vehicle.setId(1L);
vehicle.setType("Bike");

Car car = new Car();
car.setId(2L);
car.setType("Car");
car.setNumberOfDoors(4);

em.persist(vehicle);
em.persist(car);

Query query = em.createQuery("SELECT TREAT(v AS Car) FROM Vehicle v WHERE v.type = 'Car'");
List<Car> cars = query.getResultList();

assertEquals(4, cars.get(0).getNumberOfDoors());

In this query, TREAT allows us to treat each Vehicle as a Car where applicable. The result is a list of Car instances, even though the underlying entities in the database are of type Vehicle.

5. Exception Handling

When dealing with type conversions and entity casting. Both CAST and TREAT operators have specific behaviors regarding exception handling,

5.1. CAST Operator

When using the CAST operator, exceptions can occur if the data conversion isn’t possible. This usually happens when there’s an attempt to convert a value to a type that it can’t be transformed into due to incompatible formats or data types.

Suppose an Employee entity has a salary value of “5ooo” (which isn’t a valid integer). When we execute a query to cast this String to an Integer, the database attempts to convert this value, leading to a JdbcSQLDataException if the conversion fails:

Employee emp1 = new Employee();
emp1.setId(1L);
emp1.setSalary("5ooo");

em.merge(emp1);

try {
    Query query = em.createQuery("SELECT CAST(e.salary AS Integer) FROM Employee e");
    query.getResultList(); // This should throw an exception
    fail("Expected a JdbcSQLDataException to be thrown");
} catch (PersistenceException e) {
    assertTrue(e.getCause() instanceof JdbcSQLDataException,
      "Expected a JdbcSQLDataException to be thrown");
}

In this test, we’re asserting that a JdbcSQLDataException is thrown when attempting to cast an invalid string value.

5.2. TREAT Operator

In contrast, the TREAT operator handles type-safe downcasting in inheritance hierarchies. Unlike CAST, TREAT typically doesn’t throw exceptions when it encounters an issue with type casting. Instead, it returns an empty result set if no entities of the specified subclass type are found.

Suppose we query for Car instances among Vehicle entities, but the only vehicles available are of type Bike. In this case, the query doesn’t throw an exception but returns an empty result set instead:

Query query = em.createQuery("SELECT TREAT(v AS Car) FROM Vehicle v WHERE v.type = 'Bike'");
List<Car> cars = query.getResultList();

assertEquals(0, cars.size());

In this example, we’re querying for Car entities, but since no matching Car instances exist (only Bike instances are present), TREAT returns an empty list. This approach avoids exceptions and provides a clean way to handle type-safe casting by gracefully handling cases where no entities match the desired subclass type.

6. Criteria API

In the Criteria API, CAST isn’t directly supported. However, we can perform type conversions implicitly using expressions and the as() method, provided the types are compatible. For example, we can convert a field from one type to another if the types are directly compatible.

Here’s how we can use the as() method to cast an expression from one type to another, provided the types are compatible:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Integer> cq = cb.createQuery(Integer.class);
Root<Employee> employee = cq.from(Employee.class);

Expression<String> salaryExpression = employee.get("salary");
Expression<Integer> salaryAsInteger = salaryExpression.as(Integer.class);

cq.select(salaryAsInteger);

TypedQuery<Integer> query = em.createQuery(cq);

List<Integer> salaries = query.getResultList();
assertEquals(5000, salaries.get(0));

In this example, we retrieve the salary as a String and use the as() method to cast it to an Integer. This approach works when types are compatible, but direct CAST operations aren’t natively supported in the Criteria API.

On the other hand, TREAT is supported in the Criteria API and provides built-in functionality for type-safe downcasting. We can use TREAT to handle entity inheritance hierarchies by specifying a subtype. This example shows how to use TREAT with CriteriaBuilder:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Car> cq = cb.createQuery(Car.class);
Root<Vehicle> vehicleRoot = cq.from(Vehicle.class);

cq.select(cb.treat(vehicleRoot, Car.class))
  .where(cb.equal(vehicleRoot.get("type"), "Car"));

TypedQuery<Car> query = em.createQuery(cq);
List<Car> cars = query.getResultList();

assertEquals(1, cars.size());
assertEquals(4, cars.get(0).getNumberOfDoors());

In this example, TREAT is used to cast Vehicle entities to Car instances, ensuring type safety and allowing us to work with the Car subclass directly.

7. Summary

Here’s a table that highlights the key differences between CAST and TREAT in JPA:

Feature CAST TREAT
Purpose Converts a scalar value from one type to another Downcasts an entity or collection of entities in an inheritance hierarchy to a more specific subtype
Common Usage Primarily used for basic type conversions, such as converting a String to an Integer Used for polymorphic queries where a base class (superclass) needs to be treated as a subclass
Supported by JPA Not directly supported in JPA Fully supported in JPA
Scope Applies to basic data types Applies to entity types within an inheritance hierarchy

8. Conclusion

In this article, we’ve explored the difference between CAST and TREAT. CAST and TREAT are two distinct keywords in JPA that serve different purposes. CAST is used to convert between primitive types, while TREAT is used to treat an instance as if it were of a different type.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

Partner – Microsoft – NPI EA (cat = Baeldung)
announcement - icon

Azure Container Apps is a fully managed serverless container service that enables you to build and deploy modern, cloud-native Java applications and microservices at scale. It offers a simplified developer experience while providing the flexibility and portability of containers.

Of course, Azure Container Apps has really solid support for our ecosystem, from a number of build options, managed Java components, native metrics, dynamic logger, and quite a bit more.

To learn more about Java features on Azure Container Apps, visit the documentation page.

You can also ask questions and leave feedback on the Azure Container Apps GitHub page.

Partner – Microsoft – NPI EA (cat = Spring Boot)
announcement - icon

Azure Container Apps is a fully managed serverless container service that enables you to build and deploy modern, cloud-native Java applications and microservices at scale. It offers a simplified developer experience while providing the flexibility and portability of containers.

Of course, Azure Container Apps has really solid support for our ecosystem, from a number of build options, managed Java components, native metrics, dynamic logger, and quite a bit more.

To learn more about Java features on Azure Container Apps, visit the documentation page.

You can also ask questions and leave feedback on the Azure Container Apps GitHub page.

Partner – Orkes – NPI EA (cat = Spring)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

Partner – Orkes – NPI EA (tag = Microservices)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Partner – MongoDB – NPI EA (tag=MongoDB)
announcement - icon

Traditional keyword-based search methods rely on exact word matches, often leading to irrelevant results depending on the user's phrasing.

By comparison, using a vector store allows us to represent the data as vector embeddings, based on meaningful relationships. We can then compare the meaning of the user’s query to the stored content, and retrieve more relevant, context-aware results.

Explore how to build an intelligent chatbot using MongoDB Atlas, Langchain4j and Spring Boot:

>> Building an AI Chatbot in Java With Langchain4j and MongoDB Atlas

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Course – LSD – NPI (cat=JPA)
announcement - icon

Get started with Spring Data JPA through the reference Learn Spring Data JPA:

>> CHECK OUT THE COURSE

eBook Jackson – NPI EA – 3 (cat = Jackson)