-1

There is some code in front of me, And I am expected to find a mistake.

My team leader said that there is a procedure that gets data and fills the db. And told me to find the mistake

I am given a completed project, i found the procedure, its here ->

@Procedure(name = "SP_Order_Series_Multiplier")
double getMultiplierByCariKod(@Param("CariKod") String cariKod);

I found what it does here.

@Transactional
public Double getMultiplierByCariKod(String cariKod) {
    StoredProcedureQuery query = em.createNamedStoredProcedureQuery("SP_Order_Series_Multiplier");
    query.registerStoredProcedureParameter("CariKod", String.class, ParameterMode.IN);
    query.registerStoredProcedureParameter("Multiplier", Double.class, ParameterMode.OUT);
    query.setParameter("CariKod", cariKod);
    query.execute();
    return (Double) query.getOutputParameterValue("Multiplier");
}

I looked if em was created before but its only written as

@Autowired
EntityManager em;

I thought we should say something like that

EntityManager em = emf.createEntityManager(); 
em.getTransaction().begin();

And there is no code like

em.getTransaction().commit();
em.close();

Do you have any idea? Am i wrong?

Thomas
  • 87,414
  • 12
  • 119
  • 157

2 Answers2

0

The getMultiplierByCariKod method is declaratively marked as @Transactional, i.e. it will be intercepted by the Spring container to add transactional aspect (default propagation, isolation, TransactionManager... you can read through all the properties by checking the Transactional annotation sources).

With a method marked as Transactional and the bean (@Component) appropriately DI-ted, the method will operate within a transaction (created or retrieved depending on the execution context) boundary.

tmarwen
  • 15,750
  • 5
  • 43
  • 62
0

There are two types of EntityManager:

  • Container-Managed - the container injects EntityManager for us.

    @PersistenceContext
    EntityManager entityManager;
    

    BTW, you shouldn't use @Autowire to get the EM -

    @Autowired vs @PersistenceContext for EntityManager bean.

  • Application-Managed:

      EntityManager em = emf.createEntityManager(); 
    

    We should manage the lifecycle of the EntityManager we've created.

Also, the @Transactional already takes care of beginning the transaction, as well as committing.

Most Noble Rabbit
  • 2,728
  • 2
  • 6
  • 12
  • Hello, thank you for your reply.. I just got a job, its my first experience. I dont understand these codes tbh. What can I do to understand them? My friend who is senior developer said i cant learn from videos or tutorials, but those projects given at work. I am given some projects but i dont understand:( –  Mar 31 '21 at 10:28
  • 1
    @Semih It's natural that a new project can be overwhelming at first, but it might help to debug and make notes of everything that you have learnt, so you don't repeat the same mistakes. – Most Noble Rabbit Mar 31 '21 at 10:35
  • 1
    This website helped me at the beginning of my career - https://www.baeldung.com/. It's not overly detailed, short and to the point. Also, don't be afraid to ask your co-workers questions, you are new, it's natural.. Good luck :) – Most Noble Rabbit Mar 31 '21 at 10:39
  • Thanks a lot.. I want to be hardworking and understand anything im given.. Do you recommend anything to me? I go courses on udemy etc, but most of them are not helpful i think. Because the codes on projects at work are much more different than courses. –  Mar 31 '21 at 10:41
  • 1
    I would recommend focusing on concepts rather than on specific codes. That way, you will be able to cope better with problems. Also it's could be helpful to learn how to find best practices on Google/SO. – Most Noble Rabbit Mar 31 '21 at 10:48
  • Thanks for your help –  Mar 31 '21 at 11:02