java POJO 与 EJB 与 EJB 3

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1163139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:27:12  来源:igfitidea点击:

POJO vs EJB vs EJB 3

javaejbpojo

提问by hello_world_infinity

Does anyone have any example of what a Java Class might look like as a POJO, EJB, and EJB 3? I'm trying to understand these java technologies but am having trouble. I was hoping it would help if I could see what an implementation of all three would look like.

有没有人有任何关于 Java 类作为 POJO、EJB 和 EJB 3 的示例?我正在尝试了解这些 Java 技术,但遇到了麻烦。我希望如果我能看到这三个的实现会是什么样子会有所帮助。

回答by Gregory Mostizky

POJOstands for Plain-Old-Java-Object - just a normal Java class as opposed to older technologies that required changing the class in specific ways to make it work with their framework.

POJO代表 Plain-Old-Java-Object - 只是一个普通的 Java 类,而不是旧的技术,它需要以特定的方式更改类以使其与他们的框架一起工作。

class MyService {

    public String sayHello() { return "hello world"; }

}

As such POJOs can be used anywhere a normal class can be used. However if you want to build an enterprise application out of them you still need some framework - Springis a good example of a framework that can work directly with POJOs.

因此,POJO 可以在任何可以使用普通类的地方使用。然而,如果你想用它们构建一个企业应用程序,你仍然需要一些框架——Spring是一个很好的框架示例,它可以直接与 POJO 一起工作。

EJB2is no longer relevant so you can ignore it - unless you need to maintain some legacy code. To satisfy your curiosity the same example as above would require several classes and xml descriptors to make it run - it's easy to see why it became obsolete.

EJB2不再相关,因此您可以忽略它——除非您需要维护一些遗留代码。为了满足您的好奇心,与上面相同的示例需要多个类和 xml 描述符才能运行 - 很容易看出为什么它变得过时了。

EJB3is the latest standard for developing enterprise applications which replaces EJB2 and is based on concept of taking POJOs and annotating them so that they can be used in enterprise app.

EJB3是用于开发企业应用程序的最新标准,它取代了 EJB2,它基于采用 POJO 并对其进行注释的概念,以便它们可以在企业应用程序中使用。

@Stateless
class MyService {
    public String sayHello() { return "hello world"; }
}

As you can see it's very similar to POJOs. In fact most application written for EJB3 can easily be converted to work with Spring, and usually the other way works too.

如您所见,它与 POJO 非常相似。事实上,大多数为 EJB3 编写的应用程序可以很容易地转换为与 Spring 一起使用,通常其他方式也可以。

回答by b0x0rz

via: http://swik.net/POJO+ejb3

通过:http: //swik.net/POJO+ejb3

EJB3 entities are plain POJOs. Actually they represent the exact same concept as the Hibernate persistent entities. Their mappings are defined through JDK 5.0 annotations (an XML descriptor syntax for overriding is defined in the EJB3 specification). Annotations can be split in two categories, the logical mapping annotations (allowing you to describe the object model, the class associations, etc.) and the physical mapping annotations (describing the physical schema, tables, columns, indexes, etc). We will mix annotations from both categories in the following code examples. EJB3 annotations are in the javax.persistence.* package. Most JDK 5 compliant IDE (like Eclipse, IntelliJ IDEA and Netbeans) can autocomplete annotation interfaces and attributes for you (even without a specific "EJB3" module, since EJB3 annotations are plain JDK 5 annotations).

EJB3 实体是普通的 POJO。实际上,它们代表与 Hibernate 持久实体完全相同的概念。它们的映射是通过 JDK 5.0 注释定义的(用于覆盖的 XML 描述符语法在 EJB3 规范中定义)。注解可以分为两类,逻辑映射注解(允许你描述对象模型、类关联等)和物理映射注解(描述物理模式、表、列、索引等)。我们将在以下代码示例中混合来自这两个类别的注释。EJB3 注释位于 javax.persistence.* 包中。大多数符合 JDK 5 的 IDE(如 Eclipse、IntelliJ IDEA 和 Netbeans)都可以为您自动完成注释接口和属性(即使没有特定的“EJB3”模块,

for the example: http://www.laliluna.de/ejb-3-tutorial-jboss.html

例如:http: //www.laliluna.de/ejb-3-tutorial-jboss.html

@Entity
@Table(name="book")
@SequenceGenerator(name = "book_sequence", sequenceName = "book_id_seq")
public class Book implements Serializable {

Entity defines that this is an entity bean. The second defines the table name. The last one defines a sequence generator.

Entity 定义这是一个实体 bean。第二个定义表名。最后一个定义了一个序列生成器。

回答by cherouvim

POJO

POJO

public class Person {

    private String name;
    private int age;

    public Person() {
    }

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    // setter & getter for age omitted

    public String toString() {
        return "Person " + name;
    }

}

This can be used as an EJB3as well.

这也可以用作EJB3

Regarding EJB2please forget that it exists and do not invest any time on itunless you absolutely have to (e.g work on legacy code).

关于EJB2,忘记它的存在,除非您绝对需要(例如处理遗留代码)否则不要在它上面投入任何时间

回答by Mike

Even this class (@Stateless class MyService) mentioned above is similar to POJO, it is not a traditionally-definied POJO since it has dependency on the javax.ejb package. I wish this dependency was just like a soft reference (DB concept) instead of being required. This article mentioned some ideas regarding this: How to Fix Java POJO Annotations

甚至上面提到的这个类(@Stateless 类 MyService)也类似于 POJO,它不是传统定义的 POJO,因为它依赖于 javax.ejb 包。我希望这种依赖就像一个软引用(DB 概念)而不是必需的。这篇文章提到了一些关于这个的想法:How to Fix Java POJO Annotations