Jpa tutorial

전승원·2020년 12월 20일
0

강의 출처- 인프런 김영한 강사님
https://www.inflearn.com/course/ORM-JPA-Basic

This is for taking notes on what I learned today. Please let me know if there's any trouble or wrong information.

@Table, @Column annotation

@Entity
@Table(name = "User")
public class Member {

    @Id
    private Long id;
    @Column(name = "username")
    private String name;
    .
    .
    .
}

You can set the db table's name with @Table annotation. And also can set the name which is saved on the table column with @Column annotation.


Transaction

EntityManager em = emf.createEntityManager();

EntityTransaction tx = em.getTransaction();
tx.begin(); // Jpa should get transaction every single time it executes query or command

Jpa should get transaction every single time it executes query or command


EntityManager.close(), EntityManagerFactory.close()

em.close(); // should be closed every time user request is done.

emf.close(); // similar with destroying the whole connection pool when the app is closed.

EntityManager should be closed every time user request is done.

Closing EntityManagerFactory is similar with destroying the whole connection pool when the app is closed.


JPQL(object-focused Query Language)

List<Member> resultList = em.createQuery("select m from Member m", Member.class) // object-focused query.(Member Entity)
                    .setFirstResult(5)
                    .setMaxResults(8)
                    .getResultList();
select
            member0_.id as id1_0_,
            member0_.name as name2_0_ 
        from
            Member member0_ limit ? offset ?

There's a difference from SQL like above.

profile
No pleasure, No gain

0개의 댓글