2017-03-16 6 views
0

Eclipselink 및 객체 유지를위한 몇 가지 간단한 예제를 혼동합니다. 이것에 아주 새롭다. 데이터베이스에 저장할 개체를 만들고 변경하는 것이 중요한 시점입니까? 예 : 객체로 수행 한 모든 작업은 em.getTransaction(). begin(); 라고? 또는 당신이 그 객체로 무엇을 필요로하고 나서 시작할 수 있습니까?JPA EntityTransaction, 새 객체 생성 및 유지

이 예제는 Tomcat에서 사용하고 있습니다. 비 JTA RESOURCE_LOCAL 아래 문제

의 차이 :

EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); 
    EntityManager em = factory.createEntityManager(); 

// Creating and mutating object BEFORE begin() is called 
    Todo todo = new Todo(); 
    todo.setSummary("sum"); 
    todo.setDescription("desc"); 

    // create new todo 
    em.getTransaction().begin(); 
    em.persist(todo); 
    em.getTransaction().commit(); 

대 당신이 개체를 만들 때 차이가 없다

EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); 
EntityManager em = factory.createEntityManager(); 


em.getTransaction().begin(); 

// Creating and mutating object AFTER begin() is called 
Todo todo = new Todo(); 
todo.setSummary("sum"); 
todo.setDescription("desc"); 

em.persist(todo); 
em.getTransaction().commit(); 
+0

아니요, 아무 것도 변경되지 않습니다. –

+0

좋아요. EntityTransaction begin 메소드의 소스 코드를 읽을 때 어떻게 됐는지 알았습니다. 내가 본 모든 예가 동일하기 때문에 나는 미쳐 가고 있다고 생각했다. – JonnyD91

답변

0

. 시작하기 전이나 시작하기 전에.

0

실제로 차이점은 없습니다. 트랜잭션은 지속 된 엔티티의 데이터베이스에 저장하는 것으로 만 처리되기 때문에 (엔티티는 Java 객체 : 사용자의 경우 todo입니다). 엔터티에서 마지막으로 엔터티를 데이터베이스에서 플러시 (commit())