Using JSF, JPA and DAO. Without Spring?

自闭症网瘾萝莉.ら 提交于 2019-12-04 08:27:53

If your container doesn't inject the EntityManager for you, you can get one with:

EntityManagerFactory factory;
factory = Persistence.createEntityManagerFactory("jpatest");
EntityManager em = factory.createEntityManager();

Where "jpatest" from the unit defined in your persistence.xml

Java EE 5 doesn't support injection in non managed component so without Spring you'll have to use an application-managed entity manager here (and consequently to manage its lifecycle at the application level).

Actually, Java EE 5+ doesn't really advocates using the DAO pattern (Has JPA Killed the DAO? is a nice article on this topic) and wrapping the entity manager which implements the Domain Store pattern, which does pretty much of what DAO does, in a DAO doesn't really make sense in my opinion.

Another option for you is to implement your DAO itself as a SLSB. That way you can inject the EntityManger rather than creating it. But it has it's own bad effects like too many session beans. chaining of the beans etc which is a sort of bad parctice.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!