Dependant Value Objects [since JBoss 2.4]

Author: Vincent Harcq <[email protected]>

Standards compliance note: this feature is not standard or required in EJB 1.1. Therefore, EJBs that use this feature, might not be compile-free portable to other EJB containers.

JAWS store non primitive type fields as Object in the database. When the field is a JavaBean, an object with some primitive type attributes with getter/setter accessing methods, it is possible to store the value of these attributes instead of the whole object.

For example suppose in the Class bean, we have a field "lesson" representing the subject (english, mathematics,..) and the number of hours that the teacher take to give its lesson. This field is an simple JavaBean:

package org.jboss.docs.cmp.jaws.interfaces;

import java.io.Serializable;

public class LessonDetail
implements Serializable{

   private String subject;
   private Integer hours;

   public String getSubject(){ return subject; }
   public void setSubject(String subject){ this.subject = subject; }

   public Integer getHours(){ return hours; }
   public void setHours(Integer hours){ this.hours = hours; }

}

The ejb-jar.xml will still define "lesson" as the CMP field:

<ejb-jar>
  <display-name>Class</display-name>
  <enterprise-beans>
    <entity>
      ...
	  <cmp-field><field-name>lesson</field-name></cmp-field>
	  ...
    </entity>
  </enterprise-beans>
</ejb-jar>

But jaws.xml will say how to store/retrieve the object:

<jaws>
	<enterprise-beans>
		<entity>
			...
			<cmp-field>
				<field-name>lesson.subject</field-name>
				<column-name>LESSON_SUBJECT</column-name>
			</cmp-field>
			<cmp-field>
				<field-name>lesson.hours</field-name>
				<column-name>HOURS</column-name>
			</cmp-field>
			...
		</entity>
	</enterprise-beans>
</jaws>

Two database fields are now used to store the "value" of the object. The trick is to have getter/setter method corresponding to what you have to store/retrieve.

This feature is recursive, so you can have something like lesson.subject.name, lesson.subject.group,...

Notice that if the dependant object defines its attributes as "public", the getter/setter methods are not necessary. This can be used in an entity bean to store a reference to another entity bean by its Primary Key object. Indeed, in this case, the primary key fields are declared as public in the PK class.

Finally notice that even if this a non standard requirement in EJB 1.1, there is a way to program this in a standard way. This is to define the 2 CMP fields in the bean and access them through getter/setter method of "LessonDetail" type. These will map the database fields to the object and vice versa. A more performant solution is to keep a private "LessonDetail" attribute in the bean that is construct in the ejbLoad() method.