-->
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.  [ 7 posts ] 
Author Message
 Post subject: mapping a special hierarchy
PostPosted: Sat Nov 18, 2006 8:37 am 
Newbie

Joined: Fri Nov 17, 2006 11:32 am
Posts: 13
Hi, I need some help with a mapping, How would you map this?

public class Shape{
private id
}
public class Arrow extends Shape{
private Shape fromShape;
private Shape toShape;
}
public class Circle extends Shape{
private List<Shape> arrows;
}

My actual mapping is this one:
<class name="Shape" table="shape" abstract="true">
<id name="id" type="integer" column="idShape">
<generator class="native" />
</id>
<joined-subclass name="Arrow" table="arrow">
<key column="idShape" />
<many-to-one name="fromShape" unique="true"
column="idShape" />
<many-to-one name="toShape" unique="true" column="idShape" />
</joined-subclass>
<joined-subclass name="Circle" table="circle">
<key column="idShape" />
<list name="arrows" table="arrows_of_circle">
<key column="idShape" />
<list-index column="position" />
<one-to-many class="Arrow" />
</list>
</joined-subclass>
</class>
the problem is the duplicated idShape mapping
Thanks,
Neuquino

_________________
Neuquino
"Keep on trying and get better every day" (Queen)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 18, 2006 12:02 pm 
Newbie

Joined: Fri Nov 17, 2006 5:18 pm
Posts: 4
Location: Quilmes, Argentina
I think the problem is you design. An arrow is a shape and have two shapes? A circle is a shape and have a list of arrows that are a shapes too and have two shapes inside? It's strange....


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 18, 2006 1:57 pm 
Newbie

Joined: Fri Nov 17, 2006 11:32 am
Posts: 13
now they are arrows and circles, tomorrow may be triangles, lines or any other shape... What about the mapping, do you have a solution?

_________________
Neuquino
"Keep on trying and get better every day" (Queen)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 18, 2006 1:57 pm 
Newbie

Joined: Thu Nov 10, 2005 6:05 am
Posts: 16
hi, you should give the toShape and fromShape properties different column names than idShape.

Code:
...
<many-to-one name="fromShape" unique="true"
column="fromShapeId" />
<many-to-one name="toShape" unique="true" column="toShapeId" />
...



Ivan


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 18, 2006 2:11 pm 
Newbie

Joined: Thu Nov 10, 2005 6:05 am
Posts: 16
another thing, I don't know your object model, but maybe the many-to-many relation is suitable fr you in the Circle - Arrow relationship... this assumption is based on that that you have defined a table name (arrows_of_circle). in this mapping the Circle-Arrow relation will be stored in the Arrow table's position column, you have to use many-to-many if you want a link table...

Ivan


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 18, 2006 7:10 pm 
Newbie

Joined: Fri Nov 17, 2006 11:32 am
Posts: 13
Thank you, your solution worked fine!!

Now I have another problem that it's driving me crazy.

Here is the actual mapping:

<class name="Shape" table="shape" abstract="true">
<id name="id" type="integer" column="shape_id">
<generator class="native" />
</id>
<joined-subclass name="Arrow" table="arrow">
<key column="arrow_id" />
<many-to-one name="fromShape" unique="true"
column="from_shape_id" />
<many-to-one name="toShape" unique="true" column="to_shape_id"/>
</joined-subclass>
<joined-subclass name="Circle" table="circle">
<key column="circle_id" />
<list name="arrows" table="arrows_of_circle" >
<key column="circle_id" />
<list-index column="position" />
<many-to-many class="Arrow" column="shape_id"
foreign-key="arrow_id" />
</list>
</joined-subclass>
</class>

And the actual message of the exception:

Cannot add or update a child row: a foreign key constraint fails (`editor/arrow`, CONSTRAINT `FK58C7409502969AA` FOREIGN KEY (`to_shape_id`) REFERENCES `shape` (`shape_id`))

The problem is that I first persist the arrow, and later the circles. So when I save the arrow, the two circles don't exist in the DB. I tried with not-null="false" and not-found="ignore" in both many-to-one tags, but didn't work.
Please, I have no idea what more to try...
Thanks

_________________
Neuquino
"Keep on trying and get better every day" (Queen)


Top
 Profile  
 
 Post subject:
PostPosted: Sun Nov 19, 2006 3:12 pm 
Newbie

Joined: Thu Nov 10, 2005 6:05 am
Posts: 16
I think there are some problems with your model. If I'm right you want something like this:

There are Shapes. There are special (!) Shapes which can be linked by Arrows (which are Shapes).
If this is what you want then try something like this:

public class Shape{
private id
}

public class Arrow extends Shape{
private Circle fromShape;
private Circle toShape;
}


public class Circle extends Shape{
private List<Arrow> fromArrows;
private List<Arrow> toArrows;
}

The main difference that in the Circle class there is two lists, one for the Arrows pointing FROM, and one for pointing TO the Circle.
In this case you can have the follwing mapping:

Code:
<hibernate-mapping >
   <class name="Shape" table="shape" abstract="true">
      <id name="id" type="long" column="idShape">
         <generator class="native" />
      </id>
      <property name="name" type="string" not-null="true"
         unique="true" />
      <joined-subclass name="Arrow" table="arrow">
         <key column="idShape" />
         <many-to-one name="fromShape" not-null="true"
            column="fromShape" insert="false" update="false" />
         <many-to-one name="toShape" not-null="true" column="toShape"
            lazy="false" insert="false" update="false" />
      </joined-subclass>
      <joined-subclass name="Circle" table="circle">
         <key column="idShape" />
         <list name="fromArrows">
            <key column="fromShape" not-null="true" />
            <list-index column="fromIx" />
            <one-to-many class="Arrow" />
         </list>
         <list name="toArrows">
            <key column="toShape" not-null="true"/>
            <list-index column="toIx" />
            <one-to-many class="Arrow" />
         </list>

      </joined-subclass>
   </class>
</hibernate-mapping>


and you made an association something like this
Code:
Arrow a1 = new Arrow();
Circle c1 = new Circle();
Circle c2 = new Circle();
a1.fromShape(c1);
a1.toShape(c2);
c1.addFrom(a1);//adds to from list
c2.addTo(a1);// adds to to list
session.save(c1);
session.save(c2);
session.save(a1);


as you can see first the circles are saved, and then the arrow between them

The relation between Circle from -Arrow from is bidirectional one-to-many (I was wrong in my previuos reply with many-to-many... sry)

I think this works and maybe this is what you want...
For further info you should read the Hibernate tutorial especially Chapter 7 about Association mappings and bidi one-to-many/many-to-one...

http://www.hibernate.org/hib_docs/v3/re ... tional-m21

I hope you want something like this...

Ivan


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