db-ojb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jys <...@fr.st>
Subject object initilisation question ?
Date Fri, 05 Aug 2005 19:00:21 GMT
Hello,

First i would like to thank all the OJB developers, i'am using it to 
develop an opensource picture management web application that i hope 
will be usable one day,  i really don't get
 why OJB is almost never mentioned in talks about object persistencers 
but i hope it will change one day. It kind of scares me to see that 
everyone uses Hibernate, i hope OJB will catch up in terms
of audience.

I'am getting trouble retrieving an instance of a class representing a 
tree structure with ojb (last release), in the database the record is 
good :

parentid                                                        id    
                                    name    description   

hJ}Tfk7o>#S[Om?\AP:X}T1y    hJ}Tfk7o>#S[Om?\AP:X}T1y    terre    <NULL>   
hJ}Tfk7o>#S[Om?\AP:X}T1y    OK?`L_igc>JlU:m66mH^",@:    france    <NULL>   
OK?`L_igc>JlU:m66mH^",@:    (ZjHVf">aQvtj%6H)$NC$7hl    paris    <NULL>   
(ZjHVf">aQvtj%6H)$NC$7hl    )f[u|G'za6-&!P]OUlp-=z>n    toureiffel    
<NULL>   
(ZjHVf">aQvtj%6H)$NC$7hl    Wz:YyW,u?Bm@_VkEX.WZr[;A    bnf    <NULL>   
Wz:YyW,u?Bm@_VkEX.WZr[;A    BWZ[_/i>Oq{Kj48VXBPOe.~&    salle1    <NULL>   

So OJB was able to persists it right.

But whem i'am trying to retrieve the structure, it's kind of messed up, 
as "terre" wich is the root of the tree is the child of itself, like that :

terre
     terre france
     ......

I don't know what i'am doing bad, but i tried to secure my getter and 
setters so it's not possible to add a child which is the same object.
I was surprised to not get an exception, so i tried to add breakpoints 
to all my getters and setters to see when OJB is calling them, but
it never stop at my breakpoints.  So how does OJB set the attributes of 
the instances (my instances attributes are private) ? i really don't get it.

Thank you.

Jean-Yves

Here is the code of my class if someone can understand the problem :

/*
 * Created on 6 déc. 2004
 *
 */
package org.jys.jphotowiki.data.locate;
 
import java.io.Serializable;
import java.util.List;
import java.util.Vector;
 
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jys.jphotowiki.data.DataIntegrityException;
import org.jys.jphotowiki.data.IDataObject;
import org.jys.jphotowiki.test.pers.TestBaseCreation;
 
/**
 * Class representing some location, has a parent and some childs of 
same type
 * to make a tree of locations. Location with itself has parent is the root
 * location (ancestor of all other locations within the context of an
 * {@link org.jys.jphotowiki.data.DocumentBase}, a location with no 
child is a
 * leaf of the location tree.
 * 
 * @ojb.class
 * @ojb.field name="parentId" jdbc-type="VARCHAR" nullable="false"
 * @author jysu_cb
 */
public class Location implements IDataObject, Serializable {
 
    protected Log _log = LogFactory.getLog(Location.class);
 
    
    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 6009335074727417445L;
 
    /**
     * internal id
     * 
     * @ojb.field primarykey="true"
     */
    private String id;
 
    /**
     * name
     * 
     * @ojb.field
     */
    private String name;
 
    /**
     * description
     * 
     * @ojb.field length="200"
     */
    private String description;
 
    /**
     * parent location
     * 
     * @ojb.reference foreignkey="parentId" auto-update="none"
     *                auto-delete="none"
     */
    private Location parent;
 
    /**
     * @ojb.collection 
element-class-ref="org.jys.jphotowiki.data.locate.Location"
     *                 foreignkey="parentId"
     */
    private List<Location> childs = new Vector<Location>();
 
    public void addChild(Location newChild) {
        if (!newChild.isNew()) {
            throw new IllegalArgumentException("Can only insert a new 
Location");
        }
        childs.add(newChild);
        newChild.setParent(this);
    }
 
    public int size() {
        int result = 1;
        for (Location child : getChilds()) {
            result += child.size();
        }
        return result;
    }
 
    /**
     * @return Returns the childs.
     */
    public List<Location> getChilds() {
        return childs;
    }
 
    /**
     * @param childs
     *            The childs to set.
     */
    public void setChilds(List<Location> childs) {
        _log.debug("Setting childs : " + childs);
        
        if (childs.contains(this)) {
            throw new IllegalArgumentException("One of my son is myself");
        }
        
        this.childs = childs;
    }
 
    /**
     * @return Returns the id.
     */
    public String getId() {
        return id;
    }
 
    /**
     * @param id
     *            The id to set.
     */
    public void setId(String id) {
        this.id = id;
    }
 
    /**
     * @return Returns the description.
     */
    public String getDescription() {
        return description;
    }
 
    /**
     * @param description
     *            The description to set.
     */
    public void setDescription(String description) {
        this.description = description;
    }
 
    /**
     * @return Returns the name.
     */
    public String getName() {
        return name;
    }
 
    /**
     * @param name
     *            The name to set.
     */
    public void setName(String name) {
        this.name = name;
    }
 
    /**
     * @return Returns the parent.
     */
    public Location getParent() {
        return parent;
    }
 
    /**
     * @param parent
     *            The parent to set.
     */
    private void setParent(Location parent) {
        if (parent.equals(this)
                || (findDescendant(parent.getId()) != null)) {
            throw new IllegalArgumentException("Could not set self or 
child has parent");
        }
        
        this.parent = parent;
    }
 
    public boolean isAncestor(Location l) {
        for (Location child : getChilds()) {
            if (child.equals(l) || child.isAncestor(l)) {
                return true;
            }
        }
        return false;
    }
 
    public Location findDescendant(String descendantId) {
        //FIXME ca boucle
        if (descendantId.equals(this.getId())) {
            return this;
        }
        for (Location child : getChilds()) {
            if (descendantId.equals(child.getId())) {
                return child;
            }
            Location back = child.findDescendant(descendantId);
            if (back != null) {
                return back;
            }
        }
        return null;
    }
 
    public void insertParent(Location l) {
        if (!l.isNew()) {
            throw new IllegalArgumentException("Can only insert a new 
Location");
        }
 
        this.getParent().addChild(l);
        this.getParent().removeChild(this);
        l.childs.add(this);
    }
 
    private void removeChild(Location location) {
        this.getChilds().remove(location);
    }
 
    private boolean isNew() {
        return childs.isEmpty() && getParent() == null && getId() == null;
    }
 
    public void remove() throws DataIntegrityException {
        if (isRoot()) {
            throw new DataIntegrityException("Can not delete root 
location");
        }
        for (Location child : getChilds()) {
            getParent().getChilds().add(child);
        }
        getParent().getChilds().remove(this);
    }
 
    public boolean isLeaf() {
        return getChilds().isEmpty();
    }
 
    public boolean isRoot() {
        return this.equals(getParent());
    }
 
    public boolean equals(Object object) {
        if (!(object instanceof Location)) {
            return false;
        }
        Location rhs = (Location) object;
        return this.id.equals(rhs.getId());
    }
 
    public String toString() {
        return this.getId() + "@" + this.getName();
    }
 
    public int hashCode() {
        return getId().hashCode();
    }
 
    public Location(String name, String description, Location parent) {
        super();
        this.name = name;
        this.description = description;
        this.parent = parent;
    }
 
    public Location() {
        super();
    }
 
    public Location(String name, Location parent) {
        super();
        this.name = name;
        parent.addChild(this);
    }
 
    public void setRoot() {
        if (this.parent != null) {
            throw new IllegalStateException(
                    "Could not set root, because already has parent");
        }
        this.parent = this;
    }
 
    public List<Location> getAll() {
        List<Location> back = new Vector<Location>();
        back.add(this);
        for (Location child : getChilds()) {
            back.addAll(child.getAll());
        }
        return back;
    }
 
}



It's another unrelated question but i always get this error when 
starting my tests : is it normal ?

org.apache.ojb.broker.platforms.PlatformPostgresImpl
java.lang.ClassNotFoundException: 
org.apache.ojb.broker.platforms.PlatformPostgresImpl
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:242)
    at org.apache.ojb.broker.util.ClassHelper.getClass(Unknown Source)
    at org.apache.ojb.broker.util.ClassHelper.getClass(Unknown Source)
    at 
org.apache.ojb.broker.platforms.PlatformFactory.getPlatformFor(Unknown 
Source)
    at 
org.apache.ojb.broker.accesslayer.ConnectionManagerImpl.<init>(Unknown 
Source)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at 
sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Mime
View raw message