Как получить текущий EntityManager в JPA без весны?JAVA

Программисты JAVA общаются здесь
Anonymous
Как получить текущий EntityManager в JPA без весны?

Сообщение Anonymous »

I'm working on a homework that using JPA but without spring.
It has service layer and repository layer.
I try to begin transaction in service and call save or update from repository and then commit in service.
But how to get Current EntityManager in repository?
My cod is like this:
Service:
public void save(Entity entity){
var em = factory.createEntityManager();
var t = em.getTransaction();
try {
t.begin();

repository1.save(entity);

// For saving one to many relation
repository2.save(entity.getChildEntity());

t.commit();
} catch (Exception e) {
t.rollback();
}
}
< /code>
Repository: < /p>
// I don't want to pass EntityManager to method

public void save(T entity) {
var em = ? // How can I get EntityManager here?

em.persist(entity);
}


Подробнее здесь: https://stackoverflow.com/questions/714 ... out-spring

Вернуться в «JAVA»