Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » Secondary table with join on constant value
Secondary table with join on constant value [message #632035] Mon, 11 October 2010 10:08 Go to next message
Matti Hansson is currently offline 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

Re: Secondary table with join on constant value [message #632291 is a reply to message #632035] Tue, 12 October 2010 13:24 Go to previous message
Chris Delahunt is currently offline Chris Delahunt
Messages: 1023
Registered: July 2009
Senior Member
Hello,

One possible solution is to have a Unit class that has a List of sizes, with the first entry being the total size. This can be mapped using an ElementCollection and OrderColumn annotations which are described here:
http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/new_ collection_mappings
http://wiki.eclipse.org/EclipseLink/Development/JPA_2.0/orde red_lists

Something like:
@ElementCollection
@CollectionTable(name="AREA", joinColumns=@JoinColumn(name="UNIT_ID"))
@Column(name="SIZE")
@OrderColumn(name="AREA_NUMBER")
@OrderCorrection(EXCEPTION)
List<Integer> size;


Best Regards
Chris

[Updated on: Tue, 12 October 2010 13:25]

Report message to a moderator

Previous Topic:OneToOne mapping on same table
Next Topic:Is it possible to use to_number function in JPQL
Goto Forum:
  


Current Time: Sat Sep 20 20:16:37 GMT 2014

Powered by FUDForum. Page generated in 0.01558 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software