Closed kwonslog closed 1 month ago
@Entity
@Table(name="user")
@Id
@Column : 테이블의 컬럼명을 직접 지정할때 사용. 사용하지 않으면 필드명을 이름으로 사용.
@Column
@Entity @Table(name = "Employee") public class Employee { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "first_name", length = 50, nullable = false) private String firstName; @Column(name = "last_name", length = 50, nullable = false) private String lastName; @Column(unique = true) private String email; //인자가 없는 기본 생성자 필수 public Employee() { } // getter/setter 메서드들... }
## 사용법 - 트랙잭션 안에서 사용해야 하며 객체의 값을 변경하면 트랜잭션이 끝나기 전에 자동으로 쿼리를 만들어서 실행한다. ### insert
EntityManagerFactory emf = Persistence.createEntityManagerFactory("example-unit"); EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction(); tx.begin();
try { User newUser = new User(); newUser.setUsername("새로운유저"); newUser.setEmail("newuser@example.com");
em.persist(newUser); tx.commit();
} catch (Exception e) { if (tx.isActive()) { tx.rollback(); } e.printStackTrace(); } finally { em.close(); emf.close(); }
### update
try { // 업데이트할 User 엔티티를 조회합니다. Long userIdToUpdate = 1L; // 업데이트할 User의 ID User userToUpdate = em.find(User.class, userIdToUpdate);
if (userToUpdate != null) { // 필드 값을 업데이트합니다. userToUpdate.setUsername("새로운유저이름"); userToUpdate.setEmail("newemail@example.com"); // 엔티티 매니저가 자동으로 업데이트를 감지하고 데이터베이스에 반영합니다. } tx.commit();
### delete
try { // 삭제할 User 엔티티를 조회합니다. Long userIdToDelete = 1L; // 삭제할 User의 ID User userToDelete = em.find(User.class, userIdToDelete);
if (userToDelete != null) { // 엔티티 매니저를 사용하여 엔티티를 삭제합니다. em.remove(userToDelete); } tx.commit();
기본
@Entity
: DB 테이블과 맵핑할 대상 클래스 설정. 필수!@Table(name="user")
: 맵핑할 대상 테이블(user) 설정.@Id
: 기본키(primary key) 컬럼을 지정할때 사용. 필수!@Column
: 테이블의 컬럼명을 직접 지정할때 사용. 사용하지 않으면 필드명을 이름으로 사용.EntityManagerFactory emf = Persistence.createEntityManagerFactory("example-unit"); EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction(); tx.begin();
try { User newUser = new User(); newUser.setUsername("새로운유저"); newUser.setEmail("newuser@example.com");
} catch (Exception e) { if (tx.isActive()) { tx.rollback(); } e.printStackTrace(); } finally { em.close(); emf.close(); }
EntityManagerFactory emf = Persistence.createEntityManagerFactory("example-unit"); EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction(); tx.begin();
try { // 업데이트할 User 엔티티를 조회합니다. Long userIdToUpdate = 1L; // 업데이트할 User의 ID User userToUpdate = em.find(User.class, userIdToUpdate);
} catch (Exception e) { if (tx.isActive()) { tx.rollback(); } e.printStackTrace(); } finally { em.close(); emf.close(); }
EntityManagerFactory emf = Persistence.createEntityManagerFactory("example-unit"); EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction(); tx.begin();
try { // 삭제할 User 엔티티를 조회합니다. Long userIdToDelete = 1L; // 삭제할 User의 ID User userToDelete = em.find(User.class, userIdToDelete);
} catch (Exception e) { if (tx.isActive()) { tx.rollback(); } e.printStackTrace(); } finally { em.close(); emf.close(); }