简体   繁体   English

我可以在EJB 3.2中获得类似于实体bean的东西吗?

[英]Can I get something like an entity bean in EJB 3.2?

I was reading a Java EE book recently, and apparently entity beans were recently removed from the EJB specification. 我最近在读一本Java EE书,显然最近从EJB规范中删除了实体bean。 You are supposed to use JPA instead. 您应该改用JPA。 But I want entity beans!! 但是我想要实体豆! What I am really looking for is a JPA persistent entity that is remotely accessible, like an EJB. 我真正在寻找的是一个可远程访问的JPA持久性实体,例如EJB。 Something like this: 像这样:

@Entity
@Remote(MyEntityRemote.class)
@LocalBean
public class MyEntityEJB implements MyEntityRemote {
    public void doSomething() {
        // actually do something
    }
}

Is this at all possible without removing the bean annotations and writing a session bean like this: 在不删除Bean注释和编写会话Bean的情况下,这完全有可能吗:

@Stateless
@Remote(StatelessInterfaceToMyEntityRemote.class)
@LocalBean
public class StatelessInterfaceToMyEntity implements StatelessInterfaceToMyEntityRemote {
    public void doSomething(MyEntity entity) {
        entity.doSomething();
    }
}

If I understand you correctly it is possible first you create an Entity: 如果我对您的理解正确,则可以先创建一个实体:

@Entity
@Table('MyEntityTable')
public class MyEntity {...}

Then you create a session bean facade for the entity exposing through it any interfaces you may need 然后,为该实体创建一个会话bean Facade,通过它暴露您可能需要的任何接口

@Stateless //Facade is a seesion bean so it can be @Stateless or @Statefull for basic CRUD it shoud be @Stateless
public class EntityFacade extends AbstractFacade<MyEntity> {
@PersistenceContext(unitName = "MyPersistanceUnit") //remember to define it first
private EntityManager em;

@Override
protected EntityManager getEntityManager() {
    return em;
}

Now u can define any methods that work with your Entity class. 现在,您可以定义与您的Entity类一起使用的任何方法。 Remember if you expose your entity via Remote Interfaces it will be in a detached state. 请记住,如果您通过远程接口公开您的实体,它将处于分离状态。 So after updating you will first nee to use the merge(object) method of EntityManager 因此,更新后,您将首先使用EntityManagermerge(object)方法

EDIT 编辑
Abstract facade is a concept that is used with JPA Entity, NetBeans in version 7.3 generates it for you automatically. 抽象外观是与JPA实体一起使用的概念,NetBeans 7.3版中会自动为您生成它。 It is used to define the most common operations on Entities so you dont have to repeat the code for every Entity. 它用于定义实体上最常见的操作,因此您不必为每个实体重复执行代码。 It goes like this 像这样

public abstract class AbstractFacade<T> {
private Class<T> entityClass;

public AbstractFacade(Class<T> entityClass) {
    this.entityClass = entityClass;
}

protected abstract EntityManager getEntityManager();

public void create(T entity) {
    getEntityManager().persist(entity);
}

public void edit(T entity) {
    getEntityManager().merge(entity);
}

public void remove(T entity) {
    getEntityManager().remove(getEntityManager().merge(entity));
}

public T find(Object id) {

    return getEntityManager().find(entityClass, id);
}
...
}

The function above do some basic CRUD operations without much effort. 上面的功能可以轻松完成一些基本的CRUD操作。 So extending the facade gives you the ability to have does operations defined you could say out of the box. 因此,扩展立面使您能够立即定义可以定义的操作。 Of course this just for basic configurations the Entity facade can use many entities and do some business logic before it persists anything. 当然,这仅用于基本配置,实体外观可以使用许多实体并在持久化任何内容之前执行一些业务逻辑。 So in your case it would go like this: 因此,在您的情况下,它将如下所示:

public class EntityFacade extends AbstractFacade<MyEntity> {
@PersistenceContext(unitName = "MyPersistanceUnit") //remember to define it first
private EntityManager em;
@Override
protected EntityManager getEntityManager() {
return em;
}
 public doSomething(MyEntity entity)
{
 entity.get(...);
  ...
 entity.set(...)
  if(iWantToPesristIt)
    edit(entity)
  else
    return;
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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