POJO、EJB、EJB 3

发布于 2024-07-27 04:49:38 字数 102 浏览 6 评论 0原文

有谁有 Java 类作为 POJO、EJB 和 EJB 3 的示例吗? 我正在尝试了解这些 java 技术,但遇到了麻烦。 我希望如果我能看到这三种方法的实现会是什么样子,将会有所帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

再可℃爱ぅ一点好了 2024-08-03 04:49:38

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

class MyService {

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

}

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

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

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

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

正如您所看到的,它与 POJO 非常相似。 事实上,大多数为 EJB3 编写的应用程序都可以轻松转换为与 Spring 一起使用,通常其他方式也可以工作。

POJO stands 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.

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 - Spring is a good example of a framework that can work directly with POJOs.

EJB2 is 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.

EJB3 is 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.

@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.

土豪 2024-08-03 04:49:38

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

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

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

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

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

via: 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).

for the example: 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.

遗心遗梦遗幸福 2024-08-03 04:49:38

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;
    }

}

这也可以用作EJB3

关于EJB2,请忘记它的存在,并且不要在其上投入任何时间,除非您绝对必须这样做(例如处理遗留代码)。

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 EJB3 as well.

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

深者入戏 2024-08-03 04:49:38

即使上面提到的这个类(@Stateless class MyService)与POJO类似,它也不是传统定义的POJO,因为它依赖于javax.ejb包。 我希望这种依赖关系就像软引用(数据库概念)一样,而不是必需的。 本文提到了一些与此相关的想法:如何修复Java POJO 注解

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文