|
|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV |
TOP THREE LINKS YOU MUST CLICK ON Enterprise
EJB 3.0 Preview
The advanced features Part 2
By: Bill Burke
Digg This!
Last month's article on EJB 3.0 (Vol. 9, issue 11) focused primarily on the basic features of the specification. Part 2 dives much deeper into the specification to talk about more advanced features like dependency injection, dependent objects, secondary tables, and inheritance. Dependency Injection
@Stateful
public class ShoppingCartBean implements ShoppingCart
{
@Inject private UserTransaction userTx;
@Inject private EntityManager entityManager;
private Petstore store;
@EJB(name="petstore") public void setPetstore(Petstore store) {
this.store = store;
}
When an instance of ShoppingCartBean is allocated, the EJB container will look up the UserTransaction service and set the userTx variable. It will also get a reference to the EJB whose ejbName is "petstore" and call the setPetstore() method. Values can be injected either with an explicit field set or by calling a setter method. A great side effect of injection is that it becomes possible to test beans outside the context of a container. @Resource is another annotation for injecting things like DataSources and JMS connections. Dependent Objects
@DependentObject(access=AccessType.PROPERTY)
public class Address implements java.io.Serializable {
private String street;
private String state;
private String city;
public String getState() { return state; }
...
}
Your dependent value class can either have its properties defined as get/set methods or directly as fields. Next, define the mapping of a @DependentObject within your entity bean (see Listing 1). Multi-Table Mappings
@Entity
@SecondaryTable(name="ADDRESS", join={@JoinColumn(name="address_id")})
public class Customer {
...
@Column(name = "street", secondaryTable = "ADDRESS")
public String getStreet()
{
return street;
}
...
The @SecondaryTable is defined as a class annotation and specifies the table's name as well as the columns to use to join the main and subtable together. The @JoinColumns of the secondary table must map directly to the primary key of the entity. To map a specific property to the secondary table, specify the secondaryTable annotation member value from the @Column annotation. Entity Inheritance SINGLE_TABLE Strategy The table mapping would be one gigantic table: create table Animal ( All entities that subclass from Animal can be queried polymorphically: Query query = entityManager.createQuery("from Animal a where a.averageWeight > 10"); This query could return an instance of a Dog, Cat, Elephant, whatever entities that are currently defined in the Animal hierarchy. Finally Usable With the use of annotations, XML deployment descriptors that have long been the bane of EJB developers can be completely removed if so desired by the developer. All in all, EJB has come a long way since the 1.0 days and is morphing itself to the specifications of the community for which it was written. References LATEST JAVA STORIES & POSTS
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK SPONSORED BY INFRAGISTICS
BREAKING JAVA NEWS
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||