際際滷

際際滷Share a Scribd company logo
??? ????? ?? JPA
2015.12.12
???(woniper)
???(woniper)
http://blog.woniper.net
https://github.com/woniper
leekw3747@gmail.com
??
JPA ??
JDBC?? JPA??
??? ????
????
JPA(Java Persistence API) ??
Java ORM(Object-Relational Mapping) ?? F/W
RDB? ??? ??(??), ??
Hibernate, Eclipse Link, TopLink Essensials ???
JDBC?? JPA??
JDBC
Connection
Query ??
Resource Close
xBatis
<select id="select" parameterType="java.lang.Integer" resultType=^User ̄>
select userId, username, password from User where userId = #{userId}
</select>
Query ?? ??? ????
JPA
EntityManager em = entityManagerFactory.createEntityManager();
1. Insert
em.persist(user);
2. Select
User user = em.find(User.class, 1);
3. Update
user.setUsername("update Name");
user.setPassword("1111");
4. Delete
em.remove(user);
Connection, close
Query ?? ?? ? ??
??? ?? ??
???(Entity) ????
@Entity
@Table(name = "User")
public class User {
@Id @GeneratedValue
private Integer userId;
@Column(name = "username", nullable = false)
private String username;
@Column(name = "password", nullable = false)
private String password;
@Column(name = "nickName", nullable = true)
private String nickName;
}
USER
???(NEW)
USER
??(Managed)
??? ????
persist()
USER
??(Removed)
remove()
USER
???(Detached)
detach()
??? ????
??
?? ?? SQL(SQL ???)
?? ?? ??
1? ??
??? ???
?? ??
EntityManagerFactory emf = Persistence.createEntityManagerFactory(^persistence");
1? ?? ??
@ID Entity Snapshot
SQL ???
EntityManager em1 = emf.createEntityManager();
em1
1? ?? ??
@ID Entity Snapshot
SQL ???
EntityManager em2 = emf.createEntityManager();
em2
1? ?? ??
@ID Entity Snapshot
1 User User
SQL ??
?
??? ????
J
D
B
C
??? ????? ??? ????
?
??(persist)
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// ? ?? ??
User user = new User("wons", "12345", "woniper");
// ?? ??
// 1? ?? ??, Query ?? ? ??
em.persist(user);
// ??? ??
// SQL ??? Query DB? ??
em.getTransaction().commit();
em.close();
????????? ???????????? ????? JPA
????????? ???????????? ????? JPA
????????? ???????????? ????? JPA
??(find)
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// ?? ???
User user = em.find(User.class, 1);
// ??? ??
em.getTransaction().commit();
em.close();
????????? ???????????? ????? JPA
????????? ???????????? ????? JPA
????????? ???????????? ????? JPA
??(?? ?? ??)
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// ?? ??
// 1? ?? ??
User user = em.find(User.class, 1);
// ?? ?? ??
user.setUserName("updateName");
user.setPassword("1111");
user.setNickName("updateNick");
// ??? ??
em.getTransaction().commit();
em.close();
????????? ???????????? ????? JPA
????????? ???????????? ????? JPA
????????? ???????????? ????? JPA
??(remove)
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// ?? ??
// 1? ?? ??
User user = em.find(User.class, 1);
// ?? ??, Query ?? ? ??
em.remove(user);
// SQL ??? Query DB? ??
em.getTransaction().commit();
em.close();
????????? ???????????? ????? JPA
????????? ???????????? ????? JPA
????????? ???????????? ????? JPA
???? ???? ??
commit() ?? ? flush()
merge
? ?? ??? -> ?? ???
merge? ???? ??
?? ??? -> ? ?? ???
em.clear();
em.detach(user);
em.close();
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// 1. ?? ??, username == woniper
User user1 = em.find(User.class, 1);
// 2. ??? ??
em.detach(user1);
// 3. username ?? ??
user.setUserName(^lee-kyung-won");
// 4. ?? ??
em.merge(user1);
em.getTransaction().commit();
em.close();
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// 5. username ?? ???
User user2 = em.find(User.class, 1);
em.getTransaction().commit();
em.close();
user2? username?
lee-kyung-won
????????? ???????????? ????? JPA
????????? ???????????? ????? JPA
????????? ???????????? ????? JPA
??? ??? ??? ??? merge :
update
??? ??? merge : insert
????
????????? ???????????? ????? JPA
http://www.tutorialspoint.com/jpa/
http://www.objectdb.com/
https://en.wikibooks.org/wiki/Java_
Persistence
http://goo.gl/xzpTdK
https://goo.gl/sqmO9p
https://goo.gl/GhsI4Q
https://goo.gl/GpQzeL
?????.

More Related Content

????????? ???????????? ????? JPA

Editor's Notes

  • #2: ??
  • #3: ??, 3??, ??? ??? ???
  • #4: 2?? ?? JDBC,xBatis,JPA ?? JPA?? ?? ??? ??? ????
  • #5: ?? ??
  • #6: ?? ORM ORM == RDB? ??? ?? ? ?? (?? ??) ?? interface ?? -> ??? Hibernate? ???? ??
  • #8: ?? ??? ?? X 3?? ?? ?? X
  • #9: JDBC? ?? -> Connection, Close, ?? ??? ??? ?? S??? Map<> ???
  • #10: ??? JPA? ?? ?? X ORM? ???? ??? ??? ?? ??(?? X)
  • #12: EntityManager ??? ?? ?? update(?? ?? ??)
  • #13: Connection, Close, ?? ?? ?? JPA? CRUD? ??? ?? ?? X
  • #14: ??? ?? ??? ???? ???? ?? ??? ????
  • #15: ??? -> DTO, VO ????? ?? ??? ?? X
  • #16: 4?? ??? ?? new ???(???) -> ?? ?? ??? ???? ?? ?? ??? ?? -> ?? ??? (persist) ?? ?? -> remove ??? ?? -> ??? ???(detach) ?? ??? ??? ?? ??
  • #17: ??? ??? ???? ??!!!
  • #19: ?? ?? X
  • #20: EntityManagerFactory persistence ?? -> META-INF/persistence.xml -> ??? ?? X EntityManager ?? -> ??? ???? ?? ?? se, spring?? ?? ???(????)
  • #21: ? ??? ???? ?? ??? ? ??? ??? X, EntityManager? ?? JDBC? ??? SQL ???(?? ?? SQL? ??), DB?? ?? ??? ?? 1? ?? ?? @ID == ??? Entity == ??? ??? Snapshot == ?? ???
  • #22: CRUD ??? ??
  • #24: ????? ?? ?? ?? ?? ??
  • #26: 1? ?? ??? ??? ?? ??
  • #27: flush? Commit?? ?? ?? ??(??? ??)
  • #31: 1? ??? ??? DB?? X
  • #32: ?? ? ??
  • #34: update ??? X ?? ?? ??
  • #35: commit?? ? flush ??(??? ???, ?? ???)
  • #36: ??? ?? update ?? ?? ? ??
  • #37: ?? ??? ??? DB? ??
  • #40: 1? ?? ?? ??
  • #41: ?? ?? ? ??
  • #42: ?? ??? DB? ??
  • #43: ???? ????? ?? flush? ??? ????? DB? ??? commit? ??? DB? ??
  • #44: ?? ?? ?? ?? ??? ? ?? ?? ????
  • #48: clear : ?? ?? -> ?? ?? ?? ?? detach : ?? ??? ?? close : EntityManager ??
  • #49: username == woniper?? ?? ?? ??
  • #50: ?? ?? ?? ?? -> username? ???? ?????
  • #52: User1 == lee-kyung-won Entity? username == woniper ???? ?? ? ??
  • #53: User1? Entity??? ??(?? ???)
  • #54: update
  • #55: ? ??? update insert? ?? ?? ???? ?? ? 3?? ??
  • #56: ?? ??? ??!!!
  • #57: ??? ?? ? ???? ??. ???? ??. A/S? ????
  • #58: ???
  • #59: ???
  • #60: ?? ??? ?? ?? ???? ?? ??? ??? ??????!!