Giter Site home page Giter Site logo

jpa's Introduction

JPA


λ³Έ λ ˆν¬μ§€ν† λ¦¬λŠ” κΉ€μ˜ν•œμ˜ μŠ€ν”„λ§ λΆ€νŠΈμ™€ JPA 싀무 μ™„μ „ 정볡 λ‘œλ“œλ§΅μ„ μ‹œμ²­ν•˜λ©° μž‘μ„±λ˜μ–΄μ§€κ³  μžˆμŠ΅λ‹ˆλ‹€.

링크 : https://www.inflearn.com/roadmaps/149

StartCode

public class JpaStartMain {
    public static void main(String[] args) {
        EntityManagerFactory emf =  Persistence.createEntityManagerFactory("hello"); // 1. μ—”ν‹°ν‹° λ§€λ‹ˆμ € νŒ©ν† λ¦¬.  ν•˜λ‚˜λ§Œ μƒμ„±ν•΄μ„œ μ• ν”Œλ¦¬μΌ€μ΄μ…˜ μ „μ²΄μ—μ„œ 곡유
        EntityManager em = emf.createEntityManager(); // 2. μ—”ν‹°ν‹° λ§€λ‹ˆμ €. μ“°λ ˆλ“œ 간에 곡유 X ( μ‚¬μš©ν•˜κ³  버렀야 ν•œλ‹€. )
        EntityTransaction tx = em.getTransaction(); //  3. νŠΈλžœμž­μ…˜. JPA 의 λͺ¨λ“  데이터 변경은 νŠΈλžœμž­μ…˜ μ•ˆμ—μ„œ μ‹€ν–‰λ˜μ–΄μ•Ό ν•œλ‹€.
        tx.begin(); // 3-1. νŠΈλžœμž­μ…˜ μ‹œμž‘
        try {
            // CRUD.. Code.. etc...

            tx.commit();   // νŠΈλžœμž­μ…˜ μ •μƒμ‹œ 컀밋(반영)
        }catch (Exception e){
            tx.rollback(); // νŠΈλžœμž­μ…˜ 문제 λ°œμƒμ‹œ λ‘€λ°±
        }finally {
            em.close();    // μ—”ν‹°ν‹° λ§€λ‹ˆμ €λŠ” κΌ­ 닫아주어야함.
        }
        emf.close();
    }
}

CRUD

...
        tx.begin();
try {
// INSERT
MemberVO insertMember = new MemberVO;
        insertMember.setId("1L");
        insertMember.setName("name");
        entityManager.persist(insertMember);
// READ
MemberVO readMember = entityManager.find((Mamber.class, 1L);
// DELETE
MemberVO deleteMember = entityManager.find((Mamber.class, 1L);
        entityManager.remove(deleteMember);
// UPDATE
MemberVO updateMember = entityManager.find((Mamber.class, 1L);
        updateMember.setName("changeName");

        tx.commit();
}catch (Exception e){
        tx.rollback();
}finally {
        entityManager.close();
}       

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.