Skip to main content

Posts

The relationship between Spring Data repositories methods and `Optional`

 In Spring Data repositories, the relationship between repository methods and `Optional` can vary depending on the specific use case and method signature. 1 . `Optional` as a return type:    When a repository method is defined to return a single entity or an optional result, you can use the `Optional` type as the return type. This indicates that the method may or may not find a matching entity in the database.    Example:    ```java    Optional<YourEntity> findById(Long id);    ```    In this case, the `findById` method is defined to return an `Optional<YourEntity>`. If the entity exists in the database, the `Optional` will contain the entity. Otherwise, it will be empty. 2. `Optional` as a parameter type:    You can also use `Optional` as a parameter type to indicate an optional input value for a repository method. This allows you to handle scenarios where a parameter may or may not be present.    Example:    ```java    List<YourEntity> findByCategory(Optional<Str

What is Sealed Classes in Java 17

Java 17, released in September 2021, introduced several new features and enhancements. While some of the most prominent changes in Java 17 have received widespread attention, here's a lesser-known unique feature: 1. Sealed Classes: Sealed classes are a feature that allows developers to control the extent to which other classes can inherit from them. By declaring a class as sealed, you can specify which other classes are allowed to subclass it. This feature enhances encapsulation and provides more control over class hierarchies and extensions. Sealed classes are declared using the `sealed` keyword, and the permitted subclasses are specified using the `permits` keyword. By default, a sealed class allows subclasses only from within the same module. However, you can also explicitly specify other modules that are permitted to subclass the sealed class. This feature enables developers to create more secure and maintainable code by restricting inheritance to a defined set of classes, prev

Java 8 introduced several new features and enhancements:

  Java 8 introduced several new features and enhancements: 1. Lambdas: Lambdas enable functional programming in Java and simplify the process of writing code that handles collections, concurrency, and other tasks. 2. Stream API: The Stream API is a new API for processing collections in a functional style, which makes it easy to filter, transform, and aggregate data. 3. Default Methods: Default methods allow interface evolution by providing a mechanism to add new methods to an interface without breaking the code that already implements the interface. 4. Date and Time API: The Date and Time API provides a more comprehensive and flexible API for working with dates and times. 5. Optional: Optional is a new class that helps avoid null pointer exceptions by providing a way to represent a value that may or may not be present. 6. Nashorn JavaScript Engine: Nashorn is a new JavaScript engine that allows developers to embed JavaScript in Java applications and execute it on the JVM. 7. Type Annot

JPA implemntation many to many parent child relation of using one table and one mapping table

  JPA implemntation many to many parent child relation of using single table and one mapping table, with save api and fetch api by the root id. In JPA (Java Persistence API), a many-to-many relationship on a single class can be modeled using a self-referencing join table. This is done by creating two entities, one for the class and another for the join table, and using the @ManyToMany annotation on the class with the @JoinTable annotation on the join table. The @JoinTable annotation is used to specify the name and columns of the join table, and the @ManyToMany annotation is used to specify the mapping between the class and the join table. Here is an example of implementing a many-to-many parent-child relationship using JPA, with a Category table and a mapping table, as well as a save API and a fetch API by root ID: Create Category and Mapping entities. @Entity  public class Category { @Id@GeneratedValue(strategy = GenerationType.IDENTITY)   private Long id;   p