Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Table-per-concreteclass mapping
PostPosted: Tue Aug 26, 2003 4:53 pm 
Newbie

Joined: Tue Aug 26, 2003 2:49 pm
Posts: 11
Hi everyone,

I Would like to create a java mapping for these tables:

Circle (Integer Id, Integer X, Integer Y, Double Radius);
Rectangle (Integer Id, Integer X, Integer Y, Double width, Double height);
Note: Circle.Id != Rectangle.Id any way.

Picture (Integer Id, Integer topItem, Integer bottomItem, VarChar pageFormat);

Note: topItem & bottomItem point on a Circle or a Rectangle.

I'd like to Create 4 Classes to map my dataBase:
Shape (abstract) Note: All code below
Circle Extends Shape Note: All code below
Rectangle Extends Shape Note: All code below
Picture Note: All code below
private Shape topItem;

What the best way to do that?
I guess <any> mapping does not respond my Database mapping.


Thanks for your help,

public abstract class Shape {
private int x, y;
private int id;
public int getId() {
return id;
}
public void setId(int i) {
id = i;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int i) {
x = i;
}
public void setY(int i) {
y = i;
}
}

public class Circle extends Shape {
private double radius;
private int id;
public int getId() {
return id;
}
public void setId(int i) {
id = i;
}
public double getRadius() {
return radius;
}
public void setRadius(double d) {
radius = d;
}
}

public class Rectangle extends Shape {
private double width, height;
private int id;
public int getId() {
return id;
}
public void setId(int i) {
id = i;
}
public double getHeight() {
return height;
}
public double getWidth() {
return width;
}
public void setHeight(double d) {
height = d;
}
public void setWidth(double d) {
width = d;
}
}

public class Picture {
private Shape topItem;
private Shape bottomItem;
private String pageFormat;
private int id;
public int getId() {
return id;
}
public void setId(int i) {
id = i;
}
public Shape getBottomItem() {
return bottomItem;
}
public String getPageFormat() {
return pageFormat;
}
public Shape getTopItem() {
return topItem;
}
public void setBottomItem(Shape shape) {
bottomItem = shape;
}
public void setPageFormat(String string) {
pageFormat = string;
}
public void setTopItem(Shape shape) {
topItem = shape;
}
}

This is the Hibernate Mapping:
<?xml version="1.0" ?>
<!DOCTYPE hibernate-mapping (View Source for full doctype...)>
- <hibernate-mapping default-cascade="none">
- <!-- Shape root : no Table!!
-->
- <!-- Rectangle root
-->
- <class name="Rectangle" table="Rectangle" mutable="true" polymorphism="implicit">
- <id name="id" type="int" column="id" unsaved-value="any">
<generator class="hilo.long" />
</id>
<property name="x" column="x" type="int" not-null="false" unique="false" />
<property name="height" column="height" type="double" not-null="false" unique="false" />
<property name="width" column="width" type="double" not-null="false" unique="false" />
<property name="y" column="y" type="int" not-null="false" unique="false" />
<property name="id" column="id_1" type="int" not-null="false" unique="false" />
</class>
- <!-- Circle root
-->
- <class name="Circle" table="Circle" mutable="true" polymorphism="implicit">
- <id name="id" type="int" column="id" unsaved-value="any">
<generator class="hilo.long" />
</id>
<property name="x" column="x" type="int" not-null="false" unique="false" />
<property name="radius" column="radius" type="double" not-null="false" unique="false" />
<property name="y" column="y" type="int" not-null="false" unique="false" />
<property name="id" column="id_1" type="int" not-null="false" unique="false" />
</class>
- <!-- Picture root
-->
- <class name="Picture" table="Picture" mutable="true" polymorphism="implicit">
- <id name="id" type="int" column="id" unsaved-value="any">
<generator class="hilo.long" />
</id>
<many-to-one name="bottomItem" column="bottomItem" class="Shape" not-null="false" unique="false" outer-join="auto" />
<many-to-one name="topItem" column="topItem" class="Shape" not-null="false" unique="false" outer-join="auto" />
<property name="pageFormat" column="pageFormat" type="string" not-null="false" unique="false" />
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 7:58 am 
Newbie

Joined: Tue Aug 26, 2003 2:49 pm
Posts: 11
It is Possible to do so with Hibernate 2?
does any work around could help?

I realy need a feedback,
Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 8:43 am 
Newbie

Joined: Tue Aug 26, 2003 9:59 am
Posts: 19
Location: Atlanta, GA
Obviously you have put some thought and effort into your work. However, if you describe a little more specifically any issue that you are having it would help. I am new to Hibernate, myself; however, I have found that the best way to figure is to dive into creating my mappings and classes -- then generate the schema to see if it looks reasonable. Finally, I will create some JUnit tests to test the persistence.

_________________
Bill Siggelkow


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 12:19 pm 
Newbie

Joined: Tue Aug 26, 2003 2:49 pm
Posts: 11
it is perhaps a little confused.
The main issu is that I'd like to have the following java class for Picture:

Code:
Class Picture {
  private Shape topItem;         // NO table for Shape
  private Shape bottomItem;   // NO table for Shape
  private String pageFormat;
  private int id;
  public int getId() {
     return id;
  }
.....etc
}


instead of this:

Code:
Class Picture {
  private Rectangle topItem1;
  private Rectangle bottomItem1;
  private Circle topItem2;
  private Circle bottomItem2;
  private String pageFormat;
  private int id;
  public int getId() {
     return id;
  }
.....etc
}


Voila.....

Who can Help?


Thanks,


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 1:49 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Is the <any> mapping what yo are looking for?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 2:15 pm 
Newbie

Joined: Tue Aug 26, 2003 2:49 pm
Posts: 11
I red the documentation and example "Custom Type for Polymorphic Associations" provide with Hibernate2.
But what I understood is that <any> mapping have the following constraint:
add 2 columns to Picture's Table:
- ShapeType
- ShapeId

So Picture Table has to look like:

Code:
Picture (Integer Id, Integer topItemType,Integer topItemId, Integer bottomItemType, Integer bottomItemId,VarChar pageFormat);


Am I right?
If so, this table doesn


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 2:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Well, you could use a UserType. But note that your relational model is considered "Bad". (A real datamodeller would not create a table structure like that.)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 27, 2003 3:00 pm 
Newbie

Joined: Tue Aug 26, 2003 2:49 pm
Posts: 11
I am agree with you.
But it is my legacy dataBase.
I can't modify it any way.

I don't know what exacly is a "userType"
so I have to spend some time to read the doc.

I'll be right back.

Thanks for your responds,


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 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.