I would like to have in Java SE
@Stateless
public class CarDAO {
@Inject
private EntityManager em;
public Car findById(Long id) {
return em.find(Car.class, id);
}
}
@Singleton
public class Application {
@Inject
private CarDAO carDAO;
public void run() {
Car car = carDAO.findById(44);
System.out.println(car);
}
}
public class EntryPoint {
public static void main(String[] args) {
Application application = // missing code
application.run();
}
}
What I have to do to achieve that? I'm using postgres database, and maven in my project.
I was already reading something about Weld (but it looks only like CDI). I don't know how to add to Weld possibilty to inject Entity Manager. I know that I can obtain Entity Manager with
EntityManagerFactory emf = Persistence.createEntityManagerFactory("mgr");
EntityManager em = emf.createEntityManager();
but it's not so convenient as injecting.
It would be great if there is any tutorial about that. Anyway, thanks for any help!