JBoss 3.0 and CMP 2.0 Relation Issue
jtorres Jul 14, 2002 7:06 PMJBoss 3.0
JDK1.4.0
MySQL 3.23.49
Overview
I have created a test application for testing JBoss 3.0. I have created a one-to-many relation between two entities, Person and IpLog, respectively. I have the results printed via a test JSP: I retrieve a collection of Persons, retrieve their associated IpLog collection and display the data. Everything works great; the Persons are being retrieved from the database correctly, and the correct number of IpLog entries are being returned. However, when I user a java.util.Iterator to iterate over the IpLog collection (which is a relation defined), I just get the FIRST element in the collection ONLY. The correct number of iterations occur, but each one only displays the first IpLog.
I have attached the SQL representing the tables, the ejb-jar.xml relation entry, the jbosscmp-jdbc.xml relations entry, and the code used to iterate. Any suggestions?
SQL:
#
# Table structure for table 'ip_log'
#
CREATE TABLE `ip_log` (
`person_id` int(11) NOT NULL default '0',
`ip_log_id` int(10) NOT NULL auto_increment,
`ip_log_address` varchar(50) NOT NULL default '',
`ip_log_last_updt_date` timestamp(14) NOT NULL,
PRIMARY KEY (`ip_log_id`,`person_id`)
) TYPE=MyISAM;
#
# Table structure for table 'person'
#
CREATE TABLE `person` (
`person_id` int(11) NOT NULL auto_increment,
`person_first_name` varchar(50) NOT NULL default '',
`person_last_name` varchar(50) NOT NULL default '',
`person_email` varchar(50) NOT NULL default '',
`person_URL` varchar(100) NOT NULL default '',
`person_username` varchar(20) NOT NULL default '',
`person_password` varchar(15) NOT NULL default '',
`person_last_updt_date` timestamp(14) NOT NULL,
PRIMARY KEY (`person_id`),
KEY `person_id` (`person_id`)
) TYPE=MyISAM;
ejb-jar.xml entity relation:
[pre]
<ejb-relation>
<ejb-relation-name>person-ipLog</ejb-relation-name>
<ejb-relationship-role>
person
<ejb-relationship-role-name>PersonRelationshipRole</ejb-relationship-role-name>
One
<relationship-role-source>
person
<ejb-name>Person</ejb-name>
</relationship-role-source>
<cmr-field>
ipLog
<cmr-field-name>ipLog</cmr-field-name>
<cmr-field-type>java.util.Collection</cmr-field-type>
</cmr-field>
</ejb-relationship-role>
<ejb-relationship-role>
ipLog
<ejb-relationship-role-name>IpLogRelationshipRole</ejb-relationship-role-name>
Many
<relationship-role-source>
ipLog
<ejb-name>IpLog</ejb-name>
</relationship-role-source>
</ejb-relationship-role>
</ejb-relation>
[/pre]
jbosscmp-jdbc.xml entity relation:
[pre]
<ejb-relation>
<ejb-relation-name>person-ipLog</ejb-relation-name>
<foreign-key-mapping></foreign-key-mapping>
<ejb-relationship-role>
<ejb-relationship-role-name>PersonRelationshipRole</ejb-relationship-role-name>
<key-fields>
<key-field>
<field-name>personId</field-name>
<column-name>person_id</column-name>
</key-field>
</key-fields>
</ejb-relationship-role>
<ejb-relationship-role>
<ejb-relationship-role-name>IpLogRelationshipRole</ejb-relationship-role-name>
</ejb-relationship-role>
</ejb-relation>
[/pre]
Code to iterate
[pre]
try{
//initialize JNDI context
ctx = new InitialContext();
//Get the local home which contains the life cycle methods...
PersonLocalHome home = (PersonLocalHome)ctx.lookup("test.Person");
Collection persons = home.findAllPersons();
for(Iterator i=persons.iterator(); i.hasNext();){
PersonLocal person = (PersonLocal)i.next();
//get IPLog entries associated with person. See ejb-jar.xml for entity relationships
String firstName = person.getPersonFirstName();
String lastName = person.getPersonLastName();
out.println("Person Id: "+person.getPersonId()+"");
out.println("IP Log entries for "+firstName+":");
/**
* NOTE: The Iterator is retrieved and then iterated over in a while loop.
* You'll receive an IllegalState exception if you try to initialize an
* iterator within a for loop!
*/
Collection logs = person.getIpLog();
Iterator j = logs.iterator();
out.println("There are "+logs.size()+" IP Log entries");
while(j.hasNext()){
IpLogLocal log = (IpLogLocal)j.next();
out.println("Log id: "+log.getIpLogId()+"");
out.flush();
}
}catch(Exception e){
out.println("Caught Exception: "+e);
e.printStackTrace();
}
[/pre]