Hi!
I try to use user-defined collections to implement 1:n associations. I have read
tutorial3.html, and
code a sample program almost like as ArticleCollection class. Its name
MapCollection1.class :
<--------------------------------------------------------------------------------------->
package businessclass;
import org.apache.ojb.broker.ManageableCollection;
import java.util.*;
public class MapCollection1 extends HashMap implements ManageableCollection,
{
public void add(SimpleUserForMap pMember)
{
this.put(pMember.getOID(),pMember);
}
/**
* add a single Object to the Collection. This method is
* used during reading Collection elements from the
* database. Thus it is is save to cast anObject
* to the underlying element type of the collection.
*/
public void ojbAdd(java.lang.Object anObject)
{
if (anObject instanceof SimpleUserForMap){
add((SimpleUserForMap) anObject);
}
}
/**
* adds a Collection to this collection. Used in reading
* Extents from the Database.
* Thus it is save to cast otherCollection to this.getClass().
*/
public void ojbAddAll(
ManageableCollection otherCollection)
{
this.putAll(
((MapCollection1) otherCollection);
}
/**
* returns an Iterator over all elements in the collection.
* Used during store and delete Operations.
*/
public java.util.Iterator ojbIterator()
{
return this.values().iterator();
}
}
<--------------------------------------------------------------------------------------->
Then I declare two objects SimpleUserForMap and SimpleOrgForMap.
SimpleOrgForMap has a MapCollection1 type property named "users" .Its elements are
SimpleUserForMap object.
They are:
<--------------------------------------------------------------------------------------->
package businessclass;
import persistentlayertest.PersistentObject;
public class SimpleUserForMap extends PersistentObject{
/** name*/
private String name = null;
public String getName() {
return this.name;
}
public void setName(String pNewValue) {
this.name = pNewValue;
}
private String organizationOID = "";
public String getOrganizationOID(){
return this.organizationOID;
}
public void setOrganizationOID(String pNewValue){
this.organizationOID = pNewValue;
}
private SimpleOrgForMap organization = null;
public SimpleOrgForMap getOrganization(){
return this.organization;
}
public void setOrganization(SimpleOrgForMap pNewValue){
this.organization = pNewValue;
setOrganizationOID(pNewValue.getOID());
}
public SimpleUserForMap() {
}
}
<--------------------------------------------------------------------------------------->
package businessclass;
import persistentlayertest.PersistentObject;
import java.util.Collection;
import java.util.Vector;
import java.util.Map;
import java.util.HashMap;
import org.odmg.DMap;
import org.apache.ojb.odmg.collections.DMapImpl;
public class SimpleOrgForMap extends PersistentObject{
private MapCollection1 users ;
public void addUsers(SimpleUserForMap pUser){
System.out.println("***************Add Users For MapCollection.");
if (users == null)
{
users = new MapCollection1();
}
this.users.add(pUser);
}
public MapCollection1 getUsers(){
System.out.println("***************Get Users For MapCollection.");
return this.users;
}
public void setUsers(MapCollection1 pMembers){
this.users = pMembers;
}
public SimpleOrgForMap() {
}
}
<--------------------------------------------------------------------------------------->
The repository is:
<--------------------------------------------------------------------------------------->
<class-descriptor class="businessclass.SimpleOrgForMap" table="SimpleOrg">
<field-descriptor id="1"
name="OID"
column="OID"
jdbc-type="CHAR"
primarykey="true"
/>
<collection-descriptor
name="users"
element-class-ref="businessclass.SimpleUserForMap"
collection-class="businessclass.MapCollection1"
>
<inverse-foreignkey field-id-ref="3"/>
</collection-descriptor>
</class-descriptor>
<class-descriptor class="businessclass.SimpleUserForMap" table="SimpleUser">
<field-descriptor id="1"
name="OID"
column="OID"
jdbc-type="CHAR"
primarykey="true"
/>
<field-descriptor id="2"
name="name"
column="name"
jdbc-type="VARCHAR"
/>
<field-descriptor id="3"
name="organizationOID"
column="organizationOID"
jdbc-type="CHAR"
/>
<reference-descriptor
name="organization"
class-ref="businessclass.SimpleOrgForMap"
>
<foreignkey field-id-ref="3"/>
</reference-descriptor>
</class-descriptor>
<--------------------------------------------------------------------------------->
I new a SimpleOrgForMap object that there are several SimpleUserForMap elements in its
users property.
When I presistent SimpleOrgForMap , always raise the error message:
11:40:27,125 INFO [STDOUT] Locking obj [EMAIL PROTECTED] with lock mode 4 failed
11:40:27,140 INFO [STDOUT] businessclass.MapCollection1
11:40:27,156 ERROR [STDERR] org.odmg.ClassNotPersistenceCapableException:
businessclass.MapCollection1
11:40:27,171 ERROR [STDERR] at org.apache.ojb.odmg.ObjectEnvelope.manage(Unknown
Source)
11:40:27,187 ERROR [STDERR] at org.apache.ojb.odmg.ObjectEnvelope.<init>(Unknown
Source)
11:40:27,203 ERROR [STDERR] at
org.apache.ojb.odmg.TransactionImpl.register(Unknown Source)
11:40:27,218 ERROR [STDERR] at org.apache.ojb.odmg.TransactionImpl.lock(Unknown
Source)
11:40:27,234 ERROR [STDERR] at org.apache.ojb.odmg.NarrowTransaction.lock(Unknown
Source)
:
:
What wrong with my code or repository setting? May I set OJB.properties some attribute
specially??
Thanks in advance
Dogie Tsai
ps.I use odmg to persistent objects, and use it with JBoss3.0.4.
The 1:n associations can work well with Collection,List and Vector.