-->
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.  [ 3 posts ] 
Author Message
 Post subject: self-referencing classes (unlimited refs, non-tree), ideas?
PostPosted: Mon Sep 19, 2005 4:21 pm 
Newbie

Joined: Wed Sep 14, 2005 3:38 pm
Posts: 5
A simple table with columns "id", and "name" are used in conjunction with a mapping table (id_to, id_from) in my application to relate one entity with one or more other entities (call them associations - like marriages). There is only one class - entity.

In SQL, i'd use two inner joins (entity->mapping->entity) to list the entities names associated with a given entity name. With Hibernate, I would like to have a Set defined within the entity class that contains a group of other entities (a references back to records in the same table).

My problem is that I can't figure out how to reflect this kind of table syntax in hibernate. My mapping table is a simple id_to, id_from structure. All examples I have seen using sets always refer to another data table, not a reference back to the originating table.

Can anyone give me a jump start and post a proposed hbm.xml file for my entity class that would persist the relationship mapping discussed above?

Thanks in advance!

Ian


Top
 Profile  
 
 Post subject: perhaps like this?
PostPosted: Mon Sep 19, 2005 5:43 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Code:
  <class table="ent" name="Ent">
    <id name="id">
       <generator class="assigned"/>
    </id>
    <property name="name"/>
    <set name="linkedObjects" table="ent_relations" >
      <key column="id1"/>
      <many-to-many class="Ent" column="id2"/>
    </set>
  </class>




Code:
public class Ent {
  private int id;
  String name;
  Set linkedObjects;

  public int getId(){
    return id;
  }

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

  public String getName(){
    return name;
  }

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

  public Set getLinkedObjects(){
    return linkedObjects;
  }

  public void setLinkedObjects( Set linkedObjects ){
    this.linkedObjects = linkedObjects;
  }
}



Code:

CREATE TABLE ent (
  id int PRIMARY KEY,
  name varchar(50)
);


CREATE TABLE ent_relations (
  id1 int,
  id2 int
);


_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 19, 2005 6:13 pm 
Newbie

Joined: Wed Sep 14, 2005 3:38 pm
Posts: 5
lol, I don't know why I couldn't see that.

thanks kg


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