-->
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.  [ 9 posts ] 
Author Message
 Post subject: Child/parent same class
PostPosted: Tue Jan 24, 2006 5:14 pm 
Newbie

Joined: Tue Jan 24, 2006 5:03 pm
Posts: 5
Hello,

i´m new to hibernate, so i ran into some problems i could not solve even by reading the faq/documents.

I have parent class 'A' which can also be the child class. 'A' has a Hashset with all childs. How can i map this ? Every example i found has at least two mapping classes. Level up - i want this mapping inverse to get the parent of a child. I tried lots of combinations using the hibernate referenz, but still no *click*
Could anyone show me the right direction ?

Kind regards
jefrie


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 25, 2006 4:45 am 
Newbie

Joined: Wed Dec 14, 2005 6:50 am
Posts: 17
As you are new (me too) have you did the child/parent example from the documentation (capture 21) got to work? (Ok it's with 2 tables, but if you're familiar with that, your primary goal is some steps nearer)

Let me know if and how you solved the problem...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 4:48 am 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
I'm writing this code in the window so there may be some errors, but you'll get the idea.

What you are describing is pretty much a "tree", as a node (parent) has a set of children (of same type) - this should help you get started:

Code:
package com.tim;
/**
* A tree item has a set of tree items
* You can go down the tree, or up the tree (e.g. bi-navigatable)
*/
public class TreeItem {
  Long id;
  String name;
  Set<TreeItem> children; // this is it's children
  TreeItem parent; // and a link to go back up the tree

  // do the getters and setter
}


Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
   <class name="com.tim.TreeItem" table="Tree_Item">
      <id name="id" column="id" type="long">
         <generator class="native"/>
      </id>
      <property name="name" type="java.lang.String" column="name"/>
      <many-to-one name="parent" column="parent_id"
         class="com.tim.TreeItem"
         not-found="ignore"/> <!-- needed because the top level item will not have a parent-->
      <set name="children" lazy="true" inverse="true">
         <key column="parent_id"/>
         <one-to-many class="com.tim.TreeItem"/>
      </set>
   </class>
</hibernate-mapping>


The DB table Tree_Item therefore will have a id (bigdecimal, Primary Key), name (varchar) and parent_id (foreign key to id and also nullable)

Hope this helps mate - as I said it was hand typed so may have errors. Note that the children are got lazily - this is kindof a must as if it's a big tree you don't want to load the whole lot in one go I suppose. Thus remember that calls to getChildren() will require the session to be open.

:o)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 26, 2006 9:30 am 
Newbie

Joined: Tue Jan 24, 2006 5:03 pm
Posts: 5
Thanks in advance timrobertson100, never thought of a tree ...
I´m sure it will help me alot. Try it as soon as possible.

kind regards
JeFrie


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 02, 2006 2:54 pm 
Newbie

Joined: Tue Jan 24, 2006 5:03 pm
Posts: 5
hi timrobertson100,

i tried your mapping, but still errors to get rid of.
No clue what this exception is caused by. (first thought - wrong getters and setters)
Maybe you can help me with this again.
Thank you very much

regards
JeFrie

p.s. storing them is no problem ...

Log:
Code:
CollectionLoadContext - creating collection wrapper:[de.hiddenfeature.soto.Todo.childTodos#1]


Stacktrace:
Code:
org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of de.hiddenfeature.soto.Todo.setChildTodos



My class:
Code:
public class Todo implements Serializable {
   
    //hibernate id
    private Long id;
   
    private Date startTime;
    private Date endTime;
    private Date lastModified;
    private Date notifyTime;
    private Date creationTime;
    private String name;
    private String description;
    private int priority;
    private HashSet childTodos;
    private Todo parentTodo;
    private boolean complete;
    private User worker;

[...]



My mapping:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="de.hiddenfeature.soto">

<class name="Todo" table="TODOS">

    <id name="id" column="todo_id">
            <generator class="increment" />
    </id>

    <property name="startTime" type="timestamp" column="startTime" />
    <property name="endTime" type="timestamp" column="endTime" />
    <property name="lastModified" type="timestamp" column="lastModified" />
    <property name="notifyTime" type="timestamp" column="notifyTime" />
    <property name="creationTime" type="timestamp" column="creationTime" />
    <property name="name" column="name" />
    <property name="description" type="text" column="description" />
    <property name="priority" type="integer" column="priority" />
    <property name="complete" type="boolean" column="complete" />

   <many-to-one    name="parentTodo"
         column="parent_id"
         class="de.hiddenfeature.soto.Todo"
         not-found="ignore"
   />
   
   <set name="childTodos" lazy="true" inverse="true" >
      <key column="parent_id" />
      <one-to-many class="de.hiddenfeature.soto.Todo" />
   </set>

    <many-to-one   name="worker"
         column="worker_id"
         class="de.hiddenfeature.soto.User"
         not-found="ignore"
    />

</class>
</hibernate-mapping>



Here some additional files/infos
hibernate logfile
hibernate.cfg.xml
Todo.hbm.xml
the class to map


Top
 Profile  
 
 Post subject:
PostPosted: Sat Feb 04, 2006 2:38 pm 
Regular
Regular

Joined: Wed Jun 29, 2005 11:14 pm
Posts: 119
Location: København
Hi, I've only had time to look quickly but I notice that Todo.java does not have an equals() implementation. This is definitely a must here. I'll be able to have a better look on Momnday if this doesn't fix it.
Can you implement a decent equals() method please and then post a full stace trace? e.g. log.error(e) or e.printStackTrace(). Would be worth adding hibernate.cglib.use_reflection_optimizer=false to your config too...

Hope the equals helps though, but even if it doesn't, it's good practice to always have it!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 05, 2006 8:06 am 
Newbie

Joined: Tue Jan 24, 2006 5:03 pm
Posts: 5
servus timrobertson100,

i added equals() and got a more detailed error.
Code:
ERROR TodoManager - org.hibernate.PropertyAccessException: IllegalArgumentException occurred while calling setter of de.hiddenfeature.soto.Todo.childTodos


I googled for similar problems but could not find anything. I also tested some settings in the hibernate.cfg.xml and todo.hbm.xml, but still no "light at the end of the tunnel"

I updated the configs and files here:
hibernate logfile
hibernate.cfg.xml
Todo.hbm.xml
the class to map

Thank you very,very much for your efforts


Top
 Profile  
 
 Post subject:
PostPosted: Sun Feb 05, 2006 8:34 am 
Senior
Senior

Joined: Tue Aug 23, 2005 8:52 am
Posts: 181
Change
private HashSet childTodos;
to
private Set childTodos;

Hibernate replaces its own implementations of Set(and Map) at runtime. Always use the interface in these cases.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 06, 2006 3:57 pm 
Newbie

Joined: Tue Jan 24, 2006 5:03 pm
Posts: 5
Ah great !
Thank you rajasaur !
One step closer =)

Everywhere in the hibernate_reference you can find Set instead of HashSet - but no explanation (or at least i could not find anything).

Thank you all for your help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 9 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.