-->
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.  [ 5 posts ] 
Author Message
 Post subject: one class - one table - how?
PostPosted: Wed Dec 10, 2003 3:42 pm 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
I have one class MyDirectory.java which is supposed to represent something like a usual directory.
Every directory might have several subdirectories.

I have one database table directories.

I'd like to create some objects of MyDirectory with one parent directory and a structure below, e.g.:

Code:
-parentDir
   -childDir1
   -childDir2
   -childDir3


Finally I'd like to save all of them by using:
session.save(parentDir);

How is this possible (which mapping?)?
Is it possible without having to tell each childDir its parent?

Thanks a lot for your help!


Here is some code:

MyDirectory.java
Code:
package test;

import java.util.HashSet;
import java.util.Set;

public class MyDirectory {

    private Long id;
    private String name;
    private Set subDirs = new HashSet();

    public MyDirectory() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set getSubDirs() {
        return subDirs;
    }

    public void setSubDirs(Set subDirs) {
        this.subDirs = subDirs;
    }
}


TestMyDirectory.java
Code:
[...]
MyDirectory parent = new MyDirectory();
parent.setName("AAA");

//create child directories
MyDirectory child1 = new MyDirectory();
child1.setName("BBB");
MyDirectory child2 = new MyDirectory();
child2.setName("CCC");
MyDirectory child3 = new MyDirectory();
child3.setName("DDD");

//add children to parent list
parent.getSubDirs().add(child1);
parent.getSubDirs().add(child2);
parent.getSubDirs().add(child3);

// save parent directory
session.save(parent);
session.flush();
[...]


MyDirectory.hbm.xml
Code:
<?xml version="1.0"?>

<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
    <class
        name="test.MyDirectory"
        table="directories"
    >

        <id
            name="id"
            column="id"
            type="long"
        >
            <generator class="native"/>
        </id>

        <property
            name="name"
            type="string"
            column="name"
        />

        <set
            name="subDirs"
            table="directories"
            lazy="false"
            inverse="false"
            cascade="all"
            sort="unsorted"
        >

              <key
                  column="parent"
              />

              <one-to-many
                  class="test.MyDirectory"
              />
        </set>

    </class>
</hibernate-mapping>


Database
Code:
CREATE TABLE directories (
  id bigint(20) NOT NULL auto_increment,
  name varchar(255) NOT NULL default '',
  parent bigint(20) default '0',
  PRIMARY KEY  (id)
) TYPE=InnoDB;


Top
 Profile  
 
 Post subject: Re: one class - one table - how?
PostPosted: Wed Dec 10, 2003 5:51 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
sven wrote:
How is this possible (which mapping?)?

Just the one you post
sven wrote:
Is it possible without having to tell each childDir its parent?

Yes, this is the purpose of inverse="false" in your set.

beware the default value in parent bigint(20) default '0'
It works because you don't bi-direct your association.

I must have missed something, because you're the first guy who post a question and the appropriate answer the same time ;-)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 3:48 am 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
Some kind of "Configuration().add()" and ClassLoader-error occurs with these files. Sorry, I don't have the exact error message here. I'll post it later.

When I leave the part with the set out of the mapping it works (of course not the way I'd like it to work, but no error occurs).

That's why I think that something has to be wrong with the set...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 7:28 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Copy/pastle the exact mapping and POJO for my first post and it worked with hibernate 2.1rc1 and HSQLDB (I let schema export doing the DDL)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 11, 2003 1:47 pm 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
Unbelievable, it works (using Hibernate 2.1rc1 and MySQL 4)!

Don't really know what went wrong, but thanks a lot for your help though! :)


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