Flushing process with transaction

Report a typo

Select all correct statements after executing this piece of code:

Java
@SpringBootApplication
public class App implements CommandLineRunner {

    private final Logger log = LoggerFactory.getLogger(App.class);

    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }

    @Autowired
    private LocalContainerEntityManagerFactoryBean entityManagerFactoryBean;

    @Override
    public void run(String... args) {
        Post post = new Post("Post title!", "Post description!");

        EntityManager entityManager1 = entityManagerFactoryBean.getObject().createEntityManager();
        EntityManager entityManager2 = entityManagerFactoryBean.getObject().createEntityManager();

        EntityTransaction transaction = entityManager1.getTransaction();
        transaction.begin();
        entityManager1.persist(post);
        transaction.commit();


        Post post1 = entityManager1.find(Post.class, post.getId());
        Post post2 = entityManager2.find(Post.class, post.getId());

        log.info("Post1: {}", post1);
        log.info("Post2: {}", post2);
        log.info("Post1 is equal to post2: {}", post1 == post2);
    }
}
Kotlin
@SpringBootApplication
class App(
    @Autowired
    private val entityManagerFactoryBean: LocalContainerEntityManagerFactoryBean
) : CommandLineRunner {
    private val log: Logger = LoggerFactory.getLogger(App::class.java)


    override fun run(vararg args: String) {
        val post = Post("Post title!", "Post description!")

        val entityManager1 = entityManagerFactoryBean.getObject()!!.createEntityManager()
        val entityManager2 = entityManagerFactoryBean.getObject()!!.createEntityManager()

        val transaction = entityManager1.transaction
        transaction.begin()
        entityManager1.persist(post)
        transaction.commit()

        
        val post1 = entityManager1.find(Post::class.java, post.id)
        val post2 = entityManager2.find(Post::class.java, post.id)
        
        log.info("Post1: {}", post1)
        log.info("Post2: {}", post2)
        log.info("Post1 is equal to post2: {}", post1 == post2)
    }
}


fun main(args: Array<String>) {
    runApplication<App>(*args)
}
Select one or more options from the list
___

Create a free account to access the full topic