This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1. Obtaining an Entity Manager and Persisting an Entity | |
EntityManagerFactory emf = Persistence.createEntityManagerFactory("EmployeeService"); | |
EntityManager em = emf.createEntityManager(); | |
Employee emp = new Employee(130); | |
em.persist(emp); | |
persist가 완료되면 emp 객체는 entity manager의 persistence context에의해 관리된다. | |
2. Finding an Entity | |
Employee emp = em.find(Employee.class, 130); | |
못찾으면 null return. | |
3. Removing an Entity | |
Employee emp = em.find(Employee.class, 130); | |
em.remove(emp); | |
emp가 managed instance여야함. | |
emp가 null이면 IllegalArgumentException 발생. | |
4. Updating an Entity | |
Employee emp = em.find(Employy.class, 130); | |
emp.setSalary(emp.getSalary() + 1000); | |
emp가 managed instance여야함. 아니면 persistance provider가 값의 변경을 인지하지 못 함. | |
5. Transaction | |
em.getTransaction().begin(); | |
Employee emp = new Employee(130); | |
em.persist(emp); | |
em.getTransaction().commit(); | |
6. Queries | |
TypedQuery<Employee> query = em.createQuery("SELECT e FROM Employee e", Employee.class); //JPQL임 | |
List<Employee> emps = query.getResultList(); | |
7. persistence.xml Example | |
<persistence> | |
<persistence-unit name="EmployeeService" transaction-type="RESOURCE-LOCAL"> | |
<class>examples.model.Employee</class> | |
<properties> | |
<property name="javax.persistence.jdbc.driver" value="org..." /> | |
... | |
</properties> | |
</persistence> | |
in the META-INF directory |