tags:

views:

27

answers:

1

This is something I'd really like to be able to do - resolve names based on id values without fetching the whole child model.

Here is an example of what I have, a Table say Employee and a Name_Details table The Employee may look like this

Create Table Employee {
  emp_idinteger not null generated by default as identity; -- generated pk
  Department varchar(44);
  emp_name_id  Integer;  -- fk to Name_Details table
  ...other details such as hire_date etc..
}

now emp_id is a foreign key to the name_details table which may look like this:-

Create Table Name_Details {
  id Integer;
  Name varchar(32);
  Address Varchar(127);
  Postcode Varchar(10);
  --other details..
}

My question is that I'd like to model the first table Employee with a Java class but I am not interested in setting up a one-to-one relationship between the Employee class and the Name_Details table to fetch that whole Name_details table (as its quite large) the only information I want from this second class is just the Name field (found by joining the emp_name_id column with the Name_Details.id column. So is it possible in JPA to say declare something like a transient variable in my Employee class called say String employeeName and have this retrieved by JPA based on the above relationShip? This is a simplified example of what I have wgere there are several tables with name-value pairs and the master table has the values. I need this for reading and not updating/deleting etc. I am not using JPA v1.x with EJB3 (and not hibernate) on WPS 6.1

Thx G

A: 

There are a few options.

The first is to just create a Name class that maps to Name_Details but only maps the name and id fields. Employee would then have a OneToOne to Name, and only the name would be read.

A second option is define Name_Details as a @SecondaryTable in Employee and map only the name from it. The JPA spec restricts the secondary table join to have to share the same id, but depending on what JPA provider you are using, using the 1-1 foreign key may be possible (EclipseLink does support this). You could also define a view the does the join and map to the view.

A third solution is to still map all of the Name_Details fields but define them as LAZY. If your JPA provider supports LAZY basics (or fetch groups), then these will not be fetch unless accessed.

James