Random interview preparation subjects

Mihai PopescuMihai Popescu
3 min read

JPA Repository: What It Is Used For and Main Features

What is JPA Repository?

JpaRepository is a part of Spring Data JPA, which provides a convenient way to interact with a relational database using JPA (Java Persistence API). It extends PagingAndSortingRepository and CrudRepository, offering additional functionality.

Main Features of JpaRepository

  1. CRUD Operations – Provides built-in methods like save(), findById(), findAll(), deleteById(), etc.

  2. Pagination and Sorting – Supports Pageable and Sort for efficient query handling.

  3. Custom Query Methods – Supports method naming conventions like findByName(), findByEmailContaining(), etc.

  4. @Query Annotation Support – Allows custom JPQL and native SQL queries.

  5. Transaction Management – Works with Spring’s transactional support.

  6. Lazy and Eager Fetching – Can control data fetching strategies.

  7. Integration with Spring Boot – Works seamlessly with Spring Boot’s @EnableJpaRepositories.

Example Usage

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
    List<User> findByLastName(String lastName);

    @Query("SELECT u FROM User u WHERE u.email = :email")
    User findByEmail(@Param("email") String email);
}

Branching Strategy in Git

Choosing a Git branching strategy depends on the project's needs. Below are some commonly used strategies:

1. Git Flow (Vincent Driessen Model)

Best for: Large projects with scheduled releases.

  • main (stable production code).

  • develop (integration branch).

  • feature/* (for new features).

  • release/* (for preparing new releases).

  • hotfix/* (for urgent fixes in production).

Example:

git checkout -b feature/new-feature develop
git merge develop

2. GitHub Flow

Best for: Continuous Deployment and smaller projects.

  • Only main branch is used.

  • Developers create feature branches and merge them via Pull Requests (PRs).

Example:

git checkout -b feature-branch
git commit -m "New feature"
git push origin feature-branch

3. Trunk-Based Development

Best for: Fast-paced CI/CD environments.

  • Developers work directly on main with short-lived feature branches.

  • Encourages frequent merges to avoid long-lived branches.

4. GitLab Flow

Best for: Projects with multiple environments (staging, production).

  • Uses environment-based branches: main, pre-production, production.

  • Developers merge into main, which gets tested before being pushed to production.


Which One to Use?

  • For large teams with scheduled releases: Use Git Flow.

  • For CI/CD and fast releases: Use GitHub Flow or Trunk-Based Development.

  • For environment-specific releases: Use GitLab Flow.

Difference Between ArrayList and LinkedList in Java

Both ArrayList and LinkedList implement the List interface, but they have different underlying data structures and performance characteristics.

FeatureArrayList 🟢LinkedList 🔵
Underlying StructureDynamic arrayDoubly linked list
Access Time (get(index))O(1) – Direct access via indexO(n) – Traverses from head/tail
Insertion (add at end)O(1) amortized – If resizing needed, O(n)O(1) – Just updates pointers
Insertion (add in middle)O(n) – Needs shifting elementsO(1) if at head/tail, O(n) otherwise
Deletion (remove(index))O(n) – Needs shifting elementsO(1) if at head/tail, O(n) otherwise
Memory UsageLess (only stores elements)More (stores elements + pointers)
Iteration SpeedFaster (cache-friendly, continuous memory)Slower (cache-unfriendly, scattered memory)

When to Use Which?

Use ArrayList when:

✔️ You need fast random access (get(index)).
✔️ Insertions and deletions happen mostly at the end.
✔️ Memory usage should be minimal.
✔️ You need a cache-friendly structure (sequential memory).

Use LinkedList when:

✔️ You need fast insertions or deletions at the beginning or middle.
✔️ Frequent insertions/removals, and you want to avoid array shifting.
✔️ You don’t need random access often (get(index)).


Example Code

Using ArrayList (Better for fast access)

List<Integer> arrayList = new ArrayList<>();
arrayList.add(1);
arrayList.add(2);
arrayList.get(1);  // O(1) - Fast access

Using LinkedList (Better for frequent insertions/removals)

List<Integer> linkedList = new LinkedList<>();
linkedList.add(1);
linkedList.addFirst(0); // O(1) - Fast insertion at head
linkedList.remove(1);   // O(1) - Fast deletion from head
0
Subscribe to my newsletter

Read articles from Mihai Popescu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Mihai Popescu
Mihai Popescu