Java Tutorial - Java Scipt : Object Relational Mapping and the jbosscmp-jdbc.xml File

Java Tutorial - Java Scipt :

Object Relational Mapping and the jbosscmp-jdbc.xml File


As we have discussed previously, the container generates most of the code for a CMP entity bean. The process of mapping columns in a relational database to object attributes is called object relational mapping or O/R Mapping. The container needs to have some means to know how the columns in the database correspond to get and set methods in the entity bean. Unfortunately, the EJB standard does not prescribe a uniform way to do this. Each EJB container normally has its own container-specific way to map database columns to class attributes. For CMP 2.0, JBoss uses an XML file named jbosscmp-jdbc.xml to describe this mapping. This file must appear in the META-INF directory of the EJB .jar file along with the ejb-jar.xml file described in the previous section. The following code shows the first fragment containing the document
description and the root element:

<?xml version=”1.0” encoding=”UTF-8”?>
<!DOCTYPE jbosscmp-jdbc PUBLIC
‘-//JBoss//DTD JBOSSCMP-JDBC 3.0//EN’
‘file://C:\jboss-3.0.6_tomcat-4.1.18\docs\dtd\jbosscmp-jdbc_3_0.dtd’>
<jbosscmp-jdbc>
<enterprise-beans>
Instead of using the remote URL for the DTD, we have elected to use the local copy installed by JBoss. The <jbosscmp-jdbc> element is the root element, and the <enterprise-beans> element provides a container for the entity elements to follow.
<entity>
<ejb-name>UserEJB</ejb-name>
<datasource>java:/jdbc/MySqlDS</datasource>
<datasource-mapping>mySQL</datasource-mapping>
<create-table>false</create-table>
<remove-table>false</remove-table>
<table-name>users</table-name>

The value of the <ejb-name> element must match the corresponding name in the ejb-jar.xml file. The <datasource> element contains the JNDI (Java Naming and Directory Interface) name for the DataSource that will be used to retrieve and store the data. This is the same DataSource that we set up earlier in the chapter. The <datasource-mapping> element is used by JBoss to perform database-specific conversions. JBoss provides the ability to automatically create and destroy the appropriate tables in the database as the bean is deployed or undeployed. In general, we find that this is a bad idea. The database will be dropped and lose all of its data whenever the bean is undeployed from the container if <remove-table> is set to true. We find that we are too likely to undeploy my beans\  without consideration for the database so this is too dangerous for our tastes. Finally, the <table-name> tag must be an exact match of the table name in the database. The following code shows the mapping between the columns of the table and the attributes of the EJB:

<cmp-field>
<field-name>userid</field-name>
<column-name>userid</column-name>
</cmp-field>
<cmp-field>
<field-name>email</field-name>
<column-name>email</column-name>
</cmp-field>
<cmp-field>
<field-name>firstName</field-name>
<column-name>first_name</column-name>
</cmp-field>
<cmp-field>
<field-name>gradYear</field-name>
<column-name>grad_class</column-name>
</cmp-field>
<cmp-field>
<field-name>lastName</field-name>
<column-name>last_name</column-name>
</cmp-field>
<cmp-field>
<field-name>loginId</field-name>
<column-name>login_id</column-name>
</cmp-field>
<cmp-field>
<field-name>maidenName</field-name>
<column-name>maiden_name</column-name>
</cmp-field>
<cmp-field>
<field-name>mi</field-name>
<column-name>mi</column-name>
</cmp-field>
<cmp-field>
<field-name>password</field-name>
<column-name>password</column-name>
</cmp-field>

Each attribute with a get or set method in the local (or remote) interface must be covered here. The <field-name> element corresponds to the get and set method names exposed in the interface. For example, password corresponds
to the getPassword and setPassword methods in the UserLocal and UserRemote interfaces. The value of the <column-name> attribute must be an exact match with the column name in the database. The three tags shown below close the open tags in the file and complete the
jbosscmp-jdbc.xml file.

</entity>
</enterprise-beans>
</jbosscmp-jdbc>

JBoss also supports the older CMP 1.0 persistence mechanism. For CMP 1.0, JBoss requires a different file, named jaws.xml,to handle the O/R mapping for CMP 1.0 beans. Because CMP 1.0 is being depricated, we do not cover it here.

 The file jbosscmp-jdbc.xml is not required for bean-managed persistence (BMP) beans.

There is still one more JBoss-specific file that is needed for our bean, as discussed in the next section.