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: parent-child relationship within the same class
PostPosted: Thu Sep 18, 2003 3:37 am 
Newbie

Joined: Thu Sep 18, 2003 3:34 am
Posts: 9
Hi,

I've problems with defining parent-child type relationship regarding objects of the same class. I got error: "Schema text failed: duplicate import: ProductGroup" when generating db schema. What's wrong? Could someone post sample definition that must work?

wojtek


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 18, 2003 4:56 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
This is a parent/child bidirectional relationship for nested Groups:

Code:
    <class name="test.Group"
           table="GROUP">

        <id name="OID"
            column="GROUP_ID"
            type="string"
            unsaved-value="null"
            length="32">
            <generator class="uuid.hex"/>
        </id>

        <property name="name" column="GROUP_NAME" not-null="true"/>

        <many-to-one name="parentGroup"
                     class="test.Group"
                     column="PARENT_GROUP_ID"
                     cascade="none"
                     not-null="false"/>

        <set name="childGroups"
               table="GROUP"
               cascade="all"
               inverse="true">
            <key column="PARENT_GROUP_ID"/>
            <one-to-many class="test.GROUP"/>
        </set>

    </class>

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


Top
 Profile  
 
 Post subject: christian, your codes do not work
PostPosted: Thu Sep 18, 2003 8:22 pm 
Newbie

Joined: Thu Sep 18, 2003 10:52 am
Posts: 4
christian,

hello, I tried your codes thousands of time. But it could not work.
The follows are the java codes generated by
net.sf.hibernate.tool.hbm2java.CodeGenerator

Code:
package airline.hibernate;

import java.io.Serializable;
import java.util.Set;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;

/** @author Hibernate CodeGenerator */
public class Group implements Serializable {

    /** identifier field */
    private String id;

    /** nullable persistent field */
    private String myname;

    /** nullable persistent field */
    private airline.hibernate.Group parentGroup;

    /** persistent field */
    private Set childGroups;

    /** full constructor */
    public Group(java.lang.String myname, airline.hibernate.Group parentGroup, Set childGroups) {
        this.myname = myname;
        this.parentGroup = parentGroup;
        this.childGroups = childGroups;
    }

    /** default constructor */
    public Group() {
    }

    /** minimal constructor */
    public Group(Set childGroups) {
        this.childGroups = childGroups;
    }

    public java.lang.String getId() {
        return this.id;
    }

    public void setId(java.lang.String id) {
        this.id = id;
    }

    public java.lang.String getMyname() {
        return this.myname;
    }

    public void setMyname(java.lang.String myname) {
        this.myname = myname;
    }

    public airline.hibernate.Group getParentGroup() {
        return this.parentGroup;
    }

    public void setParentGroup(airline.hibernate.Group parentGroup) {
        this.parentGroup = parentGroup;
    }

    public java.util.Set getChildGroups() {
        return this.childGroups;
    }

    public void setChildGroups(java.util.Set childGroups) {
        this.childGroups = childGroups;
    }

    public String toString() {
        return new ToStringBuilder(this)
            .append("id", getId())
            .toString();
    }

    public boolean equals(Object other) {
        if ( !(other instanceof Group) ) return false;
        Group castOther = (Group) other;
        return new EqualsBuilder()
            .append(this.getId(), castOther.getId())
            .isEquals();
    }

    public int hashCode() {
        return new HashCodeBuilder()
            .append(getId())
            .toHashCode();
    }

}



I got the error like

NoClassFoundError:airline/hibernate/Group (wrong name: airline/hibernate/Group)
at java.lang. Classloader.defineClass0(native method)

This error is not the "common class not fond error",
I deleted many-to-one and set tag in hbm.xml. It Works!!!!

Can you tell me why?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 19, 2003 3:28 am 
Newbie

Joined: Thu Sep 18, 2003 10:52 am
Posts: 4
I got the answer, the error is

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


it should be <one-to-many class="test.Group"/>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 19, 2003 5:13 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Sorry, I only wrote that example down without testing. Thats why I'm usually writing my Hibernate mappings in IntelliJ IDEA. The editor not only automatically completes the XML, but also the class names :)

_________________
JAVA PERSISTENCE WITH HIBERNATE
http://jpwh.org
Get the book, training, and consulting for your Hibernate team.


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.