#80 Dummy Spring Service Review

Companies:
OTR
Themes:
SpringSpring Data JPATransactionsCode Review

Perform a review:

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class DummyService
{
    @Getter
    @Setter
    private static class Item
    {
        private String id;
    }

    private final EntityManager em;

    private Map<String, Item> cache = new HashMap<>();

    @Transactional
    public Item getItem(String id)
    {
        return cache.get(id);
    }

    public void addItem(Item item)
    {
        if (!cache.containsKey(item.getId()))
        {
            addItemInternal(item);
        }
    }

    @Transactional
    private void addItemInternal(Item item)
    {
        cache.put(item.getId(), em.merge(item));
    }
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in