Secondary table with join on constant value [message #632035] |
Mon, 11 October 2010 10:08 |
Matti Hansson Messages: 68 Registered: July 2009 |
Member |
|
|
Hi!
My object model consists of a Unit, which can have several areas. I'm not interested in the individual areas, but they each have a size and I want Unit to have the attribute totalSize. totalSize may differ from the sum of the sizes of the areas but, luckily, the correct value is stored in the database. The database has two tables, "Unit" and "Area" where the Area table has a compound primary key, consisting of "unit_Id" (the primary key of the Unit table) and "area_number", which resets at 1 for each unit_id. The correct total size for each Unit lies in the Area table with the area_number = 0.
So, with these tables:
Unit
+---------+
| unit_id |
+---------+
| 0100325 |
+---------+
| ... |
+---------+
Area
+---------+-------------+------+
| unit_id | area_number | size |
+---------+-------------+------+
| 0100325 | 0 | 42 |
+---------+-------------+------+
| 0100325 | 1 | 21 |
+---------+-------------+------+
| 0100325 | 2 | 18 |
+---------+-------------+------+
| ... | ... | ... |
+---------+-------------+------+
And this class:
@Entity
public class Unit {
@Id
private String unitId;
private int totalSize;
...
}
The totalSize attribute of the Unit object with unitId '0100325' should be 42.
How can this be done? Thanks!
/Matti
[Updated on: Mon, 11 October 2010 11:45] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.03122 seconds