#80 Dummy Spring Service 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
Sign in to start the dialogue
The mentor keeps the context of the current question and your previous messages.
Sign in
Available with Premium
This is just one of the solutions available with Premium. And more:
- 250+ advanced lectures
- 1300+ practice exercises
- 500+ solutions to problems from real interviews
- Personalized Java Hub certificates
- Expanded access to the AI mentor