数据库表访问通过JPA Vs. EJB在Web应用程序中 [英] Database table access via JPA Vs. EJB in a Web-Application

查看:112
本文介绍了数据库表访问通过JPA Vs. EJB在Web应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个访问许多数据库表的 web应用程序
我试图找出访问这些表的首选方式是什么?
是通过JPA还是EJB?

I am designing a web-application that access many database tables. I am trying to figure out what is the preferred way to access those tables? Is it via JPA or EJB?

谢谢,
Nathan

Thanks, Nathan

推荐答案

答案是'both'。

EJB本身不访问任何DB表。与Java相关的Java中所做的一切事情都是通过Java Persistence API(JPA)进行的,或者您想通过JDBC进行低级别的操作,但是我们不能在这里进行。

EJB itself doesn't access any DB tables. Everything you do in Java that relates to the DB happens via the Java Persistence API (JPA), or if you want to do low level stuff via JDBC but let's not get into that here.

EJB带来的是一个很容易管理的事务。你总是需要那些与JPA,这是一个痛苦的手动管理这些。 EJB还使您非常容易地访问您将在JPA中使用的主类与数据库进行交互:实体管理器。

What EJB brings to the table is a very easy management of transactions. You always need those with JPA, and it's a bit of pain to manage these manually. EJB also gives you very easy access to main class that you will use in JPA to interact with the DB: the entity manager.

在实践中使用EJB是很多简单而轻量化的情况,只需将@Stateless注释添加到一个bean中:

Using EJB in practice is for a lot of simple and lightweight situations nothing more than adding the @Stateless annotation to a bean:

@Stateless
public class FooService {

    @PersistenceContext
    private EntityManager entityManager;

    public Foo getByID(Long fooID) {
        return entityManager.find(Foo.class, ID);
    }
}

没有EJB,执行此简单查找的代码将更多地更多。没有JPA,根本就不会有任何代码。如前所述,EJB没有访问DB的功能。

Without EJB, the code to do this simple find would be much more verbose. And without JPA, there simply wouldn't be any code. As said before, EJB has no functionality to access the DB.

这篇关于数据库表访问通过JPA Vs. EJB在Web应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆