-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: one instance variable used by to two different properties
PostPosted: Tue Dec 09, 2003 7:15 pm 
Newbie

Joined: Tue Dec 09, 2003 2:27 am
Posts: 7
Location: Sydney -
I'm trying to setup a relationship where a composite class can have one of two parents. I have inadvertently created a situation where "one instance variable which is used by to two different properties, one on the superclass, one on the subclass" (thought I not sure which instance variable this is).

This causes the child to parent reference get set to null when an existing graph is retrieved from the DB and then saveOrUpdate is called with no changes made to the graph.

I can work around the issue if I disable the setParent method if the parent provided is null. But I'm not sure of the consequences of doing this neither in my code or in hibernate. Does work around effect performance.

Is it possible to get a bit of an explanation why this doesn't work and any suggestions on how I should refractor this would be greatly appreciated


Regards
Gary

I have the following:

Code:
Class hierarchy
==========
     Model
       |
   ---------
   |         |
  Root   NodeElem
            |
        -----------
        |            |
       Node       Leaf

Object Graph
========
  root -> nodes
  node -> nodeelems
All these relationships are bidirectional
Therefore a node's parent can either be a Root or a NodeElem.

Mapping file
========
<hibernate-mapping>
    <class name="net.sf.hibernate.examples.Root" table="eg_root" dynamic-update="false" dynamic-insert="false">
        <id name="id" column="id" type="java.lang.String" unsaved-value="null">
            <generator class="uuid.hex"/>
        </id>
        <version name="version" type="int" column="version"/>
        <bag name="nodes" lazy="false" inverse="true" cascade="save-update">
            <key column="root_id"/>
            <one-to-many class="net.sf.hibernate.examples.Node"/>
        </bag>
    </class>
         
    <class name="net.sf.hibernate.examples.NodeElem" table="eg_nodeelem" dynamic-update="false" dynamic-insert="false">
        <id name="id" column="id" type="java.lang.String" unsaved-value="null">
            <generator class="uuid.hex"/>
        </id>
        <version name="version" type="int" column="version"/>
        <many-to-one name="parentNodeElem" column="node_id" not-null="false" cascade="save-update"/>

        <joined-subclass name="net.sf.hibernate.examples.Node" table="eg_node" dynamic-update="false" dynamic-insert="false">
            <key column="id"/>
            <many-to-one name="parentRoot" column="root_id" not-null="false" cascade="save-update"/>
            <bag name="nodeElem" table="NodeElem" lazy="false" inverse="true" cascade="save-update">
                <key column="node_id"/>
                <one-to-many class="net.sf.hibernate.examples.NodeElem"/>
            </bag>
        </joined-subclass>

        <joined-subclass name="net.sf.hibernate.examples.Leaf" table="eg_leaf" dynamic-update="false" dynamic-insert="false">
            <key column="id"/>
            <property name="name" type="java.lang.String" update="true" insert="true" column="name"/>
        </joined-subclass>
    </class>
</hibernate-mapping>

[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 09, 2003 7:22 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
you forgot to supply the code of your JavaBeans, which shows that setParentNodeElem() and setParentRoot() both delegate to setParent()


Top
 Profile  
 
 Post subject: JavaBean Code
PostPosted: Tue Dec 09, 2003 7:44 pm 
Newbie

Joined: Tue Dec 09, 2003 2:27 am
Posts: 7
Location: Sydney -
Any suggestions on how I should refactor this would be appreciated.

Code:
Test Classes
sf.net.hibernate.example.Main
-----------------------------
package net.sf.hibernate.examples;

import java.util.Iterator;

import net.sf.hibernate.Query;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
import net.sf.hibernate.cfg.Environment;

/**
* @author garym
* Main
*/
public class Main {

    static void print( Root r ) {
        System.out.println( r );
        for( Iterator it = r.getNodes().iterator() ; it.hasNext() ; ) {
            Node n = (Node) it.next();
            recurseNode( n , "\t" );
        }
    }
     
    static void recurseNode( Node n , String pretty ) {
        System.out.println( pretty + n.toString() + "\t parent:" + n.getParent() );
        for( Iterator it2 = n.getNodeElem().iterator(); it2.hasNext() ; ) {
            NodeElem ne = (NodeElem) it2.next();
            if( ne instanceof Node )
                recurseNode( (Node) ne , pretty + "\t" );
            else
                System.out.println( pretty + "\t" + ne.toString() + "\t parent:" + ne.getParent()  );
        }
    }
     
    public static void main(String[] args)
        throws Exception
    {
        SessionFactory sessionFactory =
            new Configuration()
                .configure( "/net/sf/hibernate/examples/hibernate.cfg.xml" )
//                .setProperty(Environment.HBM2DDL_AUTO, "create")
                .buildSessionFactory();
//
        for( int i = 0 ; i < 1 ; i++ ) {
            Session session = sessionFactory.openSession();
//
            Root r = null;
            Query query = session.createQuery("select r from Root as r");
            Iterator it = query.iterate();
            if( it.hasNext() ) {
                r = (Root) it.next();
                System.out.println("Retrieved" );
                Iterator it2 = r.getNodes().iterator();
                if( it2.hasNext() ) {
                    Node n = (Node) it2.next();
                    Node n2 = new Node();
                    n2.addNodeElem( new Leaf("new") );
                    n.addNodeElem( n2 );
                }
                print(r);
                 
            } else {
                System.out.println("No Root found" );
                r = new Root();
                Node n = new Node();
                n.addNodeElem( new Leaf("Moe")   );
                n.addNodeElem( new Leaf("Curly") );
                n.addNodeElem( new Leaf("Larry") );
                r.addNode( n );
                print( r );
            }

            Transaction xaction= session.beginTransaction();
            System.out.println("Started Trans" );
            session.saveOrUpdate(r);
            System.out.println("Saved" );
            xaction.commit();
            System.out.println("Committed" );
            session.close();
        }
        sessionFactory.close();
    }
}

Model
-----
package net.sf.hibernate.examples;

/**
* @author garym
* Model
*/
public class Model
{
    String id;
    int ver;
    Model parent;
     
    void setParent( Model p ) {
// THIS IS THE WORK AROUND
//        if( p == null ) {
//////            parent = null;
//            return;
//        }
        if( parent != null && !p.equals(parent) )
            throw new RuntimeException("Parent can only be set twice \nthis:'"+this+"'\nparent:'" + p + "'");
        parent = p;
    }
     
    Model getParent( ) {
        return parent;
    }
     
    void setVersion( int v ) {
        ver = v;
    }
    int getVersion() {
        return ver;
    }
     
    void setId( String id ) {
        this.id = id;
    }
    String getId() {
        return id;
    }
}


Root
----
package net.sf.hibernate.examples;

import java.util.ArrayList;
import java.util.List;

/**
* @author garym
* Root
*/
public class Root
extends Model
{
    List nodes;
         
    Root() {};
     
    List getNodes() {
        return nodes;
    }
     
    void setNodes( List nodes ) {
        this.nodes = nodes;
    }
     
    void addNode( Node c ) {
        if(nodes == null) nodes = new ArrayList();
        nodes.add(c);
        c.setParent( this );
    }
     
//    public String toString() {
//        return nodes == null ? "[]" : nodes.toString();
//    }
}


NodeElem
--------
package net.sf.hibernate.examples;

/**
* @author garym
*/
public class NodeElem
extends Model
{
    void setParentNodeElem( NodeElem p ) {
        setParent(p);
    }
    NodeElem getParentNodeElem() {
        return (NodeElem) getParent();
    }
}

Node
----
package net.sf.hibernate.examples;

import java.util.ArrayList;
import java.util.List;

/**
* @author garym
*/
public class Node
extends NodeElem
{
    List nodeElems;

    Node() {};
     
    void setParentRoot( Root p ) {
        setParent(p);
    }
    Root getParentRoot() {
        Model p = getParent();
        if( p instanceof Root )
            return (Root) p;
        return null;
    }
     
    NodeElem getParentNodeElem() {
        Model p = getParent();
        if( p instanceof NodeElem )
            return (NodeElem) p;
        return null;
    }
     
    List getNodeElem() {
        return nodeElems;
    }
     
    void setNodeElem( List nodeElems ) {
        this.nodeElems = nodeElems;
    }
     
    void addNodeElem( NodeElem c ) {
        if(nodeElems == null)
            nodeElems = new ArrayList();
        nodeElems.add(c);
        c.setParent( this );
    }

//    public String toString() {
//        return nodeElems == null ? super.toString() + "[]" : super.toString() + nodeElems.toString();
//    }
}

Leaf
----
package net.sf.hibernate.examples;

/**
* @author garym
* Child
*/
public class Leaf
extends NodeElem
{
    String name;
    Leaf() {}
     
    Leaf( String name  ) {
        setName(name);
    }
     
    void setName( String name ) {
        this.name = name;
    }
     
    String getName() {
        return name;
    }
     
    public String toString() {
        return "name:'" + name + "'";
    }
}
[/code]


Top
 Profile  
 
 Post subject: single parent family
PostPosted: Tue Dec 09, 2003 8:31 pm 
Newbie

Joined: Tue Dec 09, 2003 2:27 am
Posts: 7
Location: Sydney -
get-it got-it gone.
Now I know why my model code smelt.
The question is do I code my model frame work to allow for muplti parent families and allow for shared care (as the government would want me to do).

Could any one suggest any good lite weight composite model frameworks what work well with hibernate.

Gary


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.