简体   繁体   中英

Does @EJB in JAVA EE load models to be used on the page?

In this tutorial: http://www.objectdb.com/tutorial/jpa/eclipse/ee/servlet

Is @EJB GuestDao guestDao; merely a way of loading the model on the page so it can be used? If I wasn't in a servlet and didn't have access to @EJB how would I load the model? Can I load models from other models?

@EJB GuestDao guestDao;

With that you are defining your servlet's (could be another ejb) dependecy on the ejb GuestDao . When the servlet is instantiated by the container, it makes sure that it injects the ejb (servlet's dependency) into the servlet.

It works on local ejbs, whereas if it were a remote ejb then you would have to do a JNDI lookup.

Check out the API Doc .

Alternative to that would be to use something called EntityManager . Which can be injected into the servlet by by using the annotation @PersistenceContext if you are using container managed persistence. But you can also create it programmatically (when you are not in a servlet and don't have access to @EJB ) by using EntityManagerFactory .

See example of how you would do that in a desktop application here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM