Entity Lifecycle
1.
package com.appsdeveloperblog.ws.demo_transactional;
import com.appsdeveloperblog.ws.demo_transactional.entity.Account;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@RequiredArgsConstructor
public class DemoTransactionalApplication implements CommandLineRunner {
private final EntityManagerFactory emf;
public static void main(String[] args) {
SpringApplication.run(DemoTransactionalApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// Transient mode: because it does not exist in DB
Account account = new Account();
// Managed mode: because it is in realm of Persistence Context
em.persist(account);
em.getTransaction().commit();
em.close();
}
}
2.
package com.appsdeveloperblog.ws.demo_transactional;
import com.appsdeveloperblog.ws.demo_transactional.entity.Account;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@RequiredArgsConstructor
public class DemoTransactionalApplication implements CommandLineRunner {
private final EntityManagerFactory emf;
public static void main(String[] args) {
SpringApplication.run(DemoTransactionalApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// Managed mode: because it retrieves it from DB
Account account = em.find(Account.class, 3L);
account.setBalance(100);
// Detached mode: not in realm of Persistence Context
em.remove(account);
account.setName("Parvin");
// em.persist(account);
em.getTransaction().commit();
em.close();
}
}
3. flush()
package com.appsdeveloperblog.ws.demo_transactional;
import com.appsdeveloperblog.ws.demo_transactional.entity.Account;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@RequiredArgsConstructor
public class DemoTransactionalApplication implements CommandLineRunner {
private final EntityManagerFactory emf;
public static void main(String[] args) {
SpringApplication.run(DemoTransactionalApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// Managed mode: because it retrieves it from DB
Account account = em.find(Account.class, 4L);
account.setBalance(500);
// Detached mode: not in realm of Persistence Context
em.flush();
em.detach(account);
em.getTransaction().commit();
em.close();
}
}
4. *
ther is great difference between:
package com.appsdeveloperblog.ws.demo_transactional;
import com.appsdeveloperblog.ws.demo_transactional.entity.Account;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@RequiredArgsConstructor
public class DemoTransactionalApplication implements CommandLineRunner {
private final EntityManagerFactory emf;
public static void main(String[] args) {
SpringApplication.run(DemoTransactionalApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Account account = em.find(Account.class, 4L);
account.setName("first");
em.flush();
em.detach(account);
em.merge(account);
account.setName("second");
em.flush();
em.getTransaction().commit();
em.close();
}
}
and
package com.appsdeveloperblog.ws.demo_transactional;
import com.appsdeveloperblog.ws.demo_transactional.entity.Account;
import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@RequiredArgsConstructor
public class DemoTransactionalApplication implements CommandLineRunner {
private final EntityManagerFactory emf;
public static void main(String[] args) {
SpringApplication.run(DemoTransactionalApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
Account account = em.find(Account.class, 4L);
account.setName("first");
em.flush();
em.detach(account);
account = em.merge(account);
account.setName("second");
em.flush();
em.getTransaction().commit();
em.close();
}
}
Комментарии
Отправить комментарий