Hibernate / JPA (1)

류홍규·2023년 7월 5일
0

JPA

목록 보기
1/8

Hibernate / JPA and JDBC

  • Hibernate / JPA uses JDBC for all database communications

💎 Object-To-Relational Mapping(ORM)

  • The developer defines mapping between Java class and database table

💎 What is JPA?

  • Jakarta Persistence API
    - Standard API for ORM
  • Only a specification
    - Defines a set of interfaces
    • 사용하기 위해서는 구현체가 필요하다.

⚾ Saving a Java Object with JPA

// create Java Object
Student theStudent = new Student("abc, "xyz");

// save it to database
entityManager.persist(theStudent);
  1. entityManager: Special JPA helper object
  2. .persist: The data will be stored in the database SQL insert

⚾ Retrieving a Java Object with JPA

// now retreive from database using the primary key
int theId = 1;
Student myStudent = entityManager.find(Student.class, theID);
  1. find: Query the database for given id or the pK

⚾ Querying for Java Objects

// gives a list of all of those student objects
TypedQuery<Student> theQuery = entityManager.createQuery("from student", Student.class);

List<Student> students = theQuery.getResultList();
  1. getResultList(): Returns a list of Student Objects from the database
profile
공대생의 코딩 정복기

0개의 댓글