Ice's blog

 

Using annotation metadata in EJB 3.0


The entities that map into persistent entities in EJB 3.0, are no longer complex entities with many implementing interfaces, rather just plain Java objects decorated with metadata information (either as annotation constructs or as xml descriptors)


Here's an example of such plain java object entity:


package mk.jug.domain;

import javax.persistence.*;

@Entity
public class Cabin implements java.io.Serializable {
/*actually, you don't need to declare it as serializable, but it will allow this class to be passed as a parameter or return values or remote interface methods of session beans.*/


private int id;
private int deckLevel;
private int shipID;
private int bedCount;

@Id
@GeneratedValue
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

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

public int getDeckLevel() {
    return deckLevel;
}

public void setDeckLevel(int deckLevel) {
    this.deckLevel = deckLevel;
}

public int getShipID() {
    return shipID;
}

public void setShipID(int shipID) {
    this.shipID = shipID;
}

public int getBedCount() {
    return bedCount;
}

public void setBedCount(int bedCount) {
    this.bedCount = bedCount;
}



o Few Notes:

- The bean class is annotated with @javax.persistence.Entity metadata information. This tells the EJB container that this class is mapped to a persistent (db) entity and that will be managed by an EntityManager service.


- @javax.persistence.Id annotation identifies the property via it's following accessor method as the identification to be used by the EntityManager service to identify and manage the persistent entity


- @javax.persistence.GeneratedValue annotation specifies that either the container or the persistence provider will generate the value when storing/persisting it


- @javax.persistence.Table annotation construct can be also used to specify the name of the table to which this entity class is pairing, it they are different. For example: we can map the Cabin class to BOAT_CABIN table using @Table(name="BOAT_CABIN") annotation


- @javax.persistence.Column metadata information can be used to specify the name of the column to which the class property is mapping, if they are different. For example: we can map the id property to the CABIN_ID db table column, using the @Table(name="CABIN_ID")  annotation

o All of the above annotation constructs can be replaced by a xml descriptor
    - both situations are valid and are left at your disposal. It is a matter of preference    whether to use annotations of xml descriptors.
    - my opinion is that annotations can be very effectively utilized, if used at appropriate places and in a balanced manner.
    
o In a more productive development environment, you can have generators that by the given information in a single xml file can generate both the db schema and the entity classes that pair up together in a synchronized way
   
Have a nice day.

Ice

 
 
 
 
 

« август 2008 »
нед.пон.вт.сре.чет.пет.саб.
     
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
24
25
26
27
28
29
30
31
      
Today

Valid XHTML or CSS?

[This is a Roller site]
Theme by Rowell Sotto.
 
© ipenov