tags:

views:

1445

answers:

3

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.

+5  A: 

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.

b0x0rz
+3  A: 

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

cherouvim
+1  A: 

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.

Gregory Mostizky