Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Spring Data JPA and ORM

Following are my questions about Spring Data JPA.

Q1 Is Spring Data JPA, ORM? If not then, what it is?

Q2 What is the advantage of spring data JPA?

like image 244
mubeen Avatar asked Feb 02 '16 10:02

mubeen


1 Answers

Q1 Is Spring Data JPA, ORM? If not then, what it is?

No. It is a system to create "automatic" Data Access Objects (DAOs) for you at compile time, and uses an ORM (like Hibernate) in these DAOs.

Q2 What is the advantage of spring data JPA?

You don't need to write your own DAOs

An example, you create an entity like this:

@Entity
public class Foo {

  @Id
  private Long id;

  private String name;

  ...
}

and a repository definition like this:

public interface FooRepository extends CrudRepository<Foo, Long> {
  //that's it, nothing else. no code
}

Spring Data JPA will then create a real repository class at compile-time that you can use to select, insert, update, and delete your Foo objects.

@Controller
public class FooController {
  
  @Autowired
  private FooRepository fooRepository;

  @RequestMapping("/")
  @ResponseBody
  Foo getFoo() {
    return fooRepository.findOne(1L); //look, you didn't need to write a DAO!
  }
}

This repository class uses your JPA EntityManager at run-time.

like image 169
Neil McGuigan Avatar answered Oct 06 '22 00:10

Neil McGuigan