EJB 3.0和JPA之间的关系? [英] Relationship between EJB 3.0 and JPA?

查看:107
本文介绍了EJB 3.0和JPA之间的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能看起来很明显,但我看到了矛盾的陈述:JPA是EJB 3.0的一部分吗?我不是专家,这对我来说很混乱。



如果是这样,JPA操纵实体豆?这些实体bean是持久层和实现无状态bean的逻辑的业务层之间的接口?



我的根本问题是如何实现搜索用户基于各种标准功能,其中搜索请求 - 将字符串表示 - 应该被构建?我的意思是,如果JPA不是EJB的一部分,我的bean不应该知道数据模型,对吧?



边界在哪里?

解决方案


JPA是EJB 3.0的一部分吗?


是和否... ,因为声称实现EJB 3.0规范的每个应用程序服务器还必须提供JPA实现。 ,因为JPA可以容易地在EJB之外,在独立应用程序或Spring管理的应用程序中。


JPA操纵实体Bean?


实体bean 在3.0之前的EJB中是一个可怕的想法。 JPA使用术语实体将其与可耻的历史区分开来。但是,JPA实体是将数据库表映射到纯Java对象的一种方法。原则上,对对象的更改传播到数据库,反之亦然(过度简化)。



正如我所说,JPA对EJB没有任何依赖(被视为无状态,有状态会话bean),另一方面。但是没有什么可以阻止您在EJB中使用JPA。



在您的方案中,您将有一个无状态EJB构建查询并通过JPA与数据库进行交互。从技术上讲,你会调用注入到你的bean的 EntityManager 方法:

  @Stateless 
public class SearchService {

@PersistenceContext
private EntityManager em;

public List< User> findUsersBornAfter(Date date){
return em。
createQuery(SELECT u FROM User u WHERE u.birthDate>:birthDate ORDER BY name)。
setParameter(birthDate,date)。
getResultList();
}
}

如您所见,业务层知道数据模型(显然),但就实体而言,EJB /业务服务没有依赖。在此示例中,JPQL(查询)在服务层中形成,而 User 是JPA实体。调用 getResultList()导致JPA提供程序将JPQL转换为SQL,运行查询并将结果转发回用户对象实例。



EJB和JPA之间的边界现在是否清除?


That may seem obvious but I've seen contradictory statements: Is JPA part of EJB 3.0? I'm no specialist and it's quite confusing for me.

If so, JPA manipulates Entity Beans? These entity beans being the interface between the persistence layer and the business layer implementing the logic with stateless beans?

The underlying question for me is how to implement a "search for user based on various criteria" function, where the "search" request -its string representation- should be built? I mean, if JPA is not part of EJB, my beans shouldn't be aware of the data model, right?

Where is the boundary?

解决方案

Is JPA part of EJB 3.0 ?

Yes and no... Yes because every application server claiming to implement EJB 3.0 spec must also provide JPA implementation. No because JPA can be easily outside of EJB, in standalone applications or Spring-managed ones.

JPA manipulates Entity Beans ?

Entity beans was a scary idea in pre-3.0 EJBs. JPA uses the term entities to distinguish itself from the disgraceful history. But yes, JPA entities are a way to map database tables to plain Java objects. In principle changes made to object are propagated to database and vice-versa (oversimplification).

As I said, JPA does not have any dependency on EJB (considered as stateless and stateful session beans) and the other way around. But there is nothing preventing you from using JPA in EJB.

In your scenario you'll have a stateless EJB constructing the query and interacting with the database via JPA. Technically speaking you will call methods on EntityManager injected to your bean:

@Stateless
public class SearchService {

    @PersistenceContext
    private EntityManager em;

    public List<User> findUsersBornAfter(Date date) {
        return em.
            createQuery("SELECT u FROM User u WHERE u.birthDate > :birthDate ORDER BY name").
            setParameter("birthDate", date).
            getResultList();
    }
}

As you can see the business layer is aware of the data model (obviously), but there is no dependency on EJB/business services as far as entities are concerned. In this example JPQL (query) is formed in the service layer and User is a JPA entity. Calling getResultList() causes the JPA provider to translate JPQL to SQL, run the query and transalte the results back to User object instances.

Is the border between EJB and JPA clear now?

这篇关于EJB 3.0和JPA之间的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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